Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Archive for the ‘SharePoint Web Services’ Category

WSS 3.0 Web Services – How To Update Date/Time Values

with 3 comments

When adding or updating list items using the WSS Web Service ‘UpdateListItems’ method you need to pass data / time values as strings. The format should be passed in the form ‘yyyy-mm-ddThh:mm:ssZ”.

This specifies a ISO date/time format with time zone information. The “Z” indicates that the time is in “Coordinated Universal Time” (UTC).

The “ToString” method for a DateTime object (e.g. “dt” below) can be used to format a date/time using this format by passing the “u” format descriptor:

String s = dt.ToString(“u”);

Note that the DateTime object must first be converted from local time to UTC. The DateTime “ToUniversalTime” method will do this.

 

Written by Nick Grattan

February 22, 2008 at 11:20 am

DISCO Error creating SharePoint Web Service Reference

with 27 comments

In Visual Studio 2005 you’ll need to create a web reference before you can start programming SharePoint Web Services. You will typically use a URL like the following:

http://server:port/sites/intranet/projectx/_vti_bin/lists.asmx

However, when creating the web reference you may see the following an error reporting that the DISCO document is missing a root element:

AddRefErr

The entire error text is:

The document at the url http://moss2007:2500/sites/DevSite/ _vti_bin/lists.asmx was not recognized as a known document type.
The error message from each known type may help you fix the problem:
– Report from ‘DISCO Document’ is ‘Root element is missing.’.
– Report from ‘http://moss2007:2500/sites/DevSite/_vti_bin/lists.asmx’ is ‘The document format is not recognized (the content type is ‘text/html; charset=utf-8′).’.
– Report from ‘WSDL Document’ is ‘The document format is not recognized (the content type is ‘text/html; charset=utf-8′).’.
– Report from ‘XML Schema’ is ‘The document format is not recognized (the content type is ‘text/html; charset=utf-8′).’.

To circumvent this problem click “Service Description” in the browser view in the “Add Web Reference” dialog. This shows the WSDL for the web service and allows you to enter a Web reference name. This is equivalent to entering the following URL with the parameter “?WSDL”.

Written by Nick Grattan

July 27, 2007 at 11:24 am

More SharePoint List Web Service Calls

with 4 comments

In this post I showed how the SharePoint Web Services can be used to get a list of the lists in a SharePoint site. The code in this post shows how, once you have the GUID associated with a list, you can then retreive all the items from the list.

The method ‘FillListItems’ is passed the GUID for a SharePoint list and returns the list of items in the SharePoint list:

public void FillListItems(Guid g)

{

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

   doc.LoadXml(“<root/>”);

   XmlNode listQuery = doc.SelectSingleNode(“//Query”);

   XmlNode listViewFields = doc.SelectSingleNode(“//ViewFields”);

   XmlNode listQueryOptions = doc.SelectSingleNode(“//QueryOptions”);

   XmlNode items = lst.GetListItems(g.ToString(), string.Empty,

      listQuery, listViewFields,

      string.Empty, listQueryOptions, null);

The Web Service method “GetListItems” can be used to return items from a list based on a list selection (‘listQuery’), a field selection (‘listViewFields’) and a list of options (‘listQueryOptions’). In this case all fields and all items are returned. The method returns a XmlNode list containing the items and the fields for these items. Each item is returned as a row element with the fields returned as attributes:

<rs:data ItemCount=1 xmlns:rs=urn:schemas-microsoft-com:rowset>

  <z:row ows_Attachments=0

         ows_LinkTitle=Get Started with Windows SharePoint Services!

         ows_Modified=2007-07-13 11:50:10

         ows_MetaInfo=1;#

         ows__ModerationStatus=0

         ows__Level=1

         ows_Title=Get Started with Windows SharePoint Services!

         ows_ID=1 ows_owshiddenversion=1

         ows_UniqueId=1;#{2B29357D-4861-4FE2-A2A2-D6EF549867EE}

         ows_FSObjType=1;#0

         ows_Created_x0020_Date=1;#2007-07-13 11:50:10

         ows_Created=2007-07-13 11:50:10

         ows_FileLeafRef=1;#1_.000

       ows_FileRef=1;#sites/Intranet/ProjectX/Lists/Announcements/1_.000

         xmlns:z=#RowsetSchema />

</rs:data>

Because SharePoint 2007 allows a list to contain items with different content types, the items in a list may have different fields.

Written by Nick Grattan

July 14, 2007 at 12:31 pm

Starting Out: Programming SharePoint Services

with 12 comments

Many functions that the SharePoint web interface does you can do through calling web services provided by SharePoint – this is thanks to the SOA application architecture. Here’s a starting guide to get you going. This example uses a WinForm application but the same techniques can be used with ASP.NET applications. This program gets the names and GUIDs of all lists in the site (but not sub-sites) and displays the names in a list box. When the user selects a list name the GUID for the list will be displayed:

First, you need to create a web reference to the SharePoint web service. The URL for the web reference looks like:

http://server:port/sites/intranet/projectx/_vti_bin/lists.asmx

The URL includes the server and port, and the path to the site which you want to program against – “projectx” in this case.

Now the coding starts. First, create an instance of the web service (the proxy name is “moss2007” in this case) for the “Lists” SharePoint web service. Then set the credentials used to authenticate against the service. In the WinForm example this is the currently logged on user:

    moss2007.Lists lst = new moss2007.Lists();
  lst.Credentials = System.Net.CredentialCache.DefaultCredentials;

Now a call can be made to get the “List” – this is returned as an XmlNode list:

    XmlNode nl = lst.GetListCollection();

In this example, the list is added to a list box. Each List in SharePoint has an “ID” which is a GUID, and when using lists through web services you should use the “ID” to refer to a list and not its name. Therefore, the list of names is going to be added to a list box and the GUIDs stored against them. Then, when the user selects a list the list can be accessed through the GUID. To do this, a class is created to manage items in the list box:

public class LstDataItem
{
     public LstDataItem(string sDisplay, Guid gGuid)
     {
         display = sDisplay;
         theGuid = gGuid;
     }

     private String display;

     public String Display
     {
         get { return display; }
         set { display = value; }
     }

     private Guid theGuid;

     public Guid TheGuid
     {
         get { return theGuid; }
         set { theGuid = value; }
     }
 
     public override string ToString()
     {
         return Display;
     }
}

The list can now be loaded into the list box:

foreach (XmlNode nd in nl.ChildNodes)
{
    lstLists.Items.Add(new LstDataItem(nd.Attributes[“Title”].Value, 
        new Guid(nd.Attributes[“ID”].Value)));
}

This code iterates over the list of nodes, and extracts the name from the “Title” attribute and the GUID from the “ID” attribute. These are added to the list box Items collection. Finally, an event handler is created to respond to the user selecting an item from the list box:

private void lstLists_SelectedIndexChanged(object sender, EventArgs e)
{
    LstDataItem ldi = (LstDataItem)lstLists.SelectedItem;
    if (ldi != null)
    {
        MessageBox.Show(ldi.TheGuid.ToString());
    }
}
 

Here is the complete code for loading the list in the Form’s Load event. It includes exception handling for errors thrown by the SharePoint web service:

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        moss2007.Lists lst = new moss2007.Lists();
        lst.Credentials = System.Net.CredentialCache.DefaultCredentials;

        XmlNode nl = lst.GetListCollection();

        foreach (XmlNode nd in nl.ChildNodes)
        {
            lstLists.Items.Add(new LstDataItem(nd.Attributes[“Title”].Value,
               new Guid(nd.Attributes[“ID”].Value)));
        }
    }
    catch (System.Net.WebException wex)
    {
        MessageBox.Show(wex.Message);
    }
    catch (System.Web.Services.Protocols.SoapException ex)
    {
        MessageBox.Show(“Message:\n” + ex.Message + “\nDetail:\n” +
            ex.Detail.InnerText + “\nStackTrace:\n” + ex.StackTrace);
    }
}

Written by Nick Grattan

July 5, 2007 at 7:47 pm