Tuesday 15 December 2009

A c# timer stopwatch class to help analyse lengthy program execution

I have recently been developing a piece of software that uses the old Microsoft COM Interop technology to walk through a Sharepoint 2001 folder structure. I found that this was working pretty slowly, but by the time I came around to do anything about it the code had got fairly complicated and it wasn't clear where the bottleneck was occuring.

To help understand the problem I created this simple static timer class:
class Timer
    {
        static public DateTime timeStart;
        static public DateTime timeEnd;
        static public bool debugTimer = true;  //Switch to false to turn off timing messages
 
        public  Timer()
        {
        }
 
        static public void TimerStart(string msg)
        {
            if (! debugTimer) return;
            timeStart = DateTime.Now;
            Console.WriteLine("TIMER: " + msg + " BEGUN: " + timeStart.ToString(), true);
        }
 
        static public void TimerEnd()
        {
            if (!debugTimer) return;
            timeEnd = DateTime.Now;
            TimeSpan totalTime = new TimeSpan();
            totalTime = timeEnd.Subtract(timeStart);
 
            Console.WriteLine("TIMER END: " + timeEnd.ToString() + " Total time: " + totalTime.Milliseconds, true);
        }
    }

In order to use this in your code, simply copy the class and put the following lines around whatever process you wish to monitor:
Timer.TimerStart("Beginning timer");
    //...
    //do lenghtly process
    //...
    Timer.TimerEnd();

This will give a nice message, with the time elapsed in milliseconds.

Friday 27 November 2009

Creating Sharepoint list fields on the fly with web services

I've recently been trying to create Sharepoint 2007 list fields on the fly using the Lists.UpdateLists() method described here:
http://msdn.microsoft.com/en-us/library/lists.lists.updatelist.aspx

Unfortunately, after modifying the sample code to my needs, a SOAP exception kept getting thrown: "NodeType 'Element' is not an valid type":

<errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">'Element' is an invalid XmlNodeType. Line 1, position 10.</errorstring>

Using Visual Studio 2005 to debug,  I have been unable to work out what the problem was.  The CAML query built in the webservice to create the field must be incorrect, but the innerXML of the CAML query (I was only using the "updateFields" node) looked fine.  Clearly the problem was elsewhere in the call, but is not clear to me how the call is constructed once the parameters are passed to the web service.

SOAP does not seem to me to give a huge amount of debugging information when a call fails.  When I create my own web services, I can use VS to step through them and get lots of debug information, but I haven't been able to work out how to do this with the MS DLLs yet.

Anyway, I eventually solved this problem by going back to the example and creating another copy of the example Microsoft code with minimal changes in order to point the call at my webservice.  This proved to work OK, although it did create two sample fields (Field1 and Field2) that I cannot remove using the Sharepoint IE front end!.  I suspect this is becuase they are created as required fields.

By making minimal iterative changes, I finally managed to create the following c# method which successfully creates the fields on the fly, and even gives them an appropriate Sharepoint type.

(Please note this code looks better in IE than Firefox which is not wrapping the lines - click on "View Plain" to view or copy in Firefox)

public bool CreateNewField(ListInfo listInfo, string fieldName, object fieldValue)
{
    bool canCreateField = true;
    try
    {

                //ListsService.Lists listService = new ListsService.Lists();
                //listService.Credentials= System.Net.CredentialCache.DefaultCredentials;
                //NOTE: I have already instatiated my connection (listService) and the GUID of the list (listInfo.listName)

                XmlNode ndList = listService.GetList(listInfo.listName);
                XmlNode ndVersion = ndList.Attributes["Version"];

                XmlDocument xmlDoc = new System.Xml.XmlDocument();

               

                XmlNode ndNewFields = xmlDoc.CreateNode(XmlNodeType.Element,
                    "Fields", "");
                XmlNode ndUpdateFields = xmlDoc.CreateNode(XmlNodeType.Element,
                    "Fields", "");

               
                
                // Field types
                if(fieldValue.GetType() == Type.GetType("System.String"))
                {
                    ndNewFields.InnerXml = @"<Method ID='1'>" +
                   "<Field Type='Text' DisplayName='" + fieldName + "' Required='FALSE' FromBaseType='FALSE' Description='Generated Field'/>" +
                   "</Method>";
                }
                else if (fieldValue.GetType() == Type.GetType("System.Int") || 
                         fieldValue.GetType() == Type.GetType("System.UInt16") ||
                    fieldValue.GetType() == Type.GetType("System.UInt32") ||
                    fieldValue.GetType() == Type.GetType("System.UInt64")
                    )
                {
                    ndNewFields.InnerXml = @"<Method ID='1'>" +
                  "<Field Type='Integer' DisplayName='" + fieldName + "' Required='FALSE' FromBaseType='FALSE' Description='Generated Field'/>" +
                  "</Method>";
                }
                else if (fieldValue.GetType() == Type.GetType("System.Double"))
                {
                    ndNewFields.InnerXml = @"<Method ID='1'>" +
                  "<Field Type='Number' DisplayName='" + fieldName + "' Required='FALSE' FromBaseType='FALSE' Description='Generated Field'/>" +
                  "</Method>";
                }
                else if (fieldValue.GetType() == Type.GetType("System.Date"))
                {
                    ndNewFields.InnerXml = @"<Method ID='1'>" +
                  "<Field Type='DateTime' DisplayName='" + fieldName + "' Required='FALSE' FromBaseType='FALSE' Description='Generated Field'/>" +
                  "</Method>";
                }
                else if (fieldValue.GetType() == Type.GetType("System.Array"))
                {
                    ndNewFields.InnerXml = @"<Method ID='1'>" +
                  "<Field Type='Text' DisplayName='" + fieldName + "' Required='FALSE' FromBaseType='FALSE' Description='Generated Field'/>" +
                  "</Method>";
                }
                else
                {   // Data type not handled - skip field
                    //Logger is my helper class - Remove from your code
                    Logger.LogMessageToFile("WARNING: Undefined field <" + fieldName + "> has an unhandled data type of: " + fieldValue.GetType().ToString() + "\r\n" +
                        "This field cannot be created");
                    canMigrateField = false;

                }
                Type myType = fieldValue.GetType();
                
                if (canCreateField)
                {
                    //NOTE: You need to use the GUID in the listname field.  I have this pre-populated in the listInfo.ListName class
                    XmlNode ndReturn =
                     listService.UpdateList(listInfo.listName,
                     null, ndNewFields, null, null,
                     ndVersion.Value);


             Logger.LogMessageToFile("NEW FIELD <" + fieldName + "> Created successfully.\r\n");
        }

    }

    catch (Exception ex)
    {
        Logger.LogMessageToFile("Message:\n" + ex.Message + "\nStackTrace:\n" + 
            ex.StackTrace);
        Logger.PopupMessage();
    }

    if (canCreateField) return true;
    else return false;
}


Saturday 12 September 2009

Blog Archive


Deploy Office 2003 VSTO add in to All Users using Visual Studio 2008

Sharepoint 2007 Batch Update example using web services and CAML

Binding a CheckBoxList object to an ordered Sharepoint list of reference values in a web part (c#)

Render Content Type fields as a form in Sharepoint Web Part using c#

Colour code a single column in Sharepoint 2007 list view using Jquery

Write lines to a RichTextBox control in different colours (VB .net example)

VBA code to iterate through the results of GetListCollection web service from Sharepoint 2007

Set Sharepoint meta-data from VBA using updatelist web service

SQL Server - MySQL 4 integration - openquery does not work for tables with datetime values of 0000-00-00

Use a variable as a table name in an SSIS Execute SQL Task

Blogger.com code syntax highlighting, or always remember to save your code before you start playing around with it!

Manage c# threads easily using an array of BackgroundWorker class objects

What is the difference between a URI and a URL?

Strip and manipulate a URL by breaking it into segments (.NET 2.0)

A c# timer class to help analyse lengthy program execution

Creating Sharepoint list fields on the fly with web services