Monday, March 31, 2008

Working with SharePoint list web services...pt. 2

Now that you have the internal list name, you can perform the standard CRUD (create, read, update, and delete) items in the list & attachments for an item. You will also be able to manage a document library remotely (i.e. check in, check out, and undo check out). In the example below, I will just return all of the items in the list. To do this, I will use the “GetListItems” method. The method will return an XML fragment that contains information about the items in the list. You can parse the XML to get the attributes for each row. Note that the attribute, “ows_ID”, is the list item ID and will be needed when referring to a specific list item (i.e. when updating, deleting, adding attachments, etc…). Once you have the list of items, you can read that XML into a DataSet, use the XML directly, or deserialize the XML into a collection. I personally prefer the later because most developers are used to working with objects/collections and you get the benefit of strongly typed data. I'll discuss this more in my next blog.

:: begin code sample ::

//setup the service
_service = new wsLists.Lists();
_service.Url = _siteUrl + "/_vti_bin/Lists.asmx";
_service.CookieContainer = _cookieJar;
XmlNode listItemsXml = _service.GetListItems(listName, null, null, null, null, null, null);

:: end code sample ::

:: begin XML sample ::

<listitems xmlns:s=“uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882” xmlns:dt=“uuid:C2F41010-65B3-11d1-A29F-00AA00C14882” xmlns:rs=“urn:schemas-microsoft-com:rowset” xmlns:z=“#RowsetSchema” xmlns=“http://schemas.microsoft.com/sharepoint/soap/”>
<rs:data ItemCount=“5”>
<z:row ows_Title=“Item 1” ows_ID=“1” ows_owshiddenversion=“2” ows_Created=“2008-03-19 18:03:47” />
<z:row ows_Title=“Item 2” ows_ID=“2” ows_owshiddenversion=“2” ows_Created=“2008-03-19 18:03:47” />
<z:row ows_Title=“Item 3” ows_ID=“3” ows_owshiddenversion=“2” ows_Created=“2008-03-19 18:03:47” />
<z:row ows_Title=“Item 4” ows_ID=“4” ows_owshiddenversion=“2” ows_Created=“2008-03-19 18:03:47” />
<z:row ows_Title=“Item 5” ows_ID=“5” ows_owshiddenversion=“2” ows_Created=“2008-03-19 18:03:47” />
</rs:data>
</listitems>

:: end XML sample ::

Future blog(s):
  1. Using data returned from a SharePoint list.

1 comment:

Mark Kovacevich said...

Sounds to me like the object/collection deserialization is the way to go. The framework becomes more flexible & less complex in the long run since you are not writing heaps of different XPath queries.