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.

Saturday, March 29, 2008

Working with SharePoint list web services

SharePoint lists can be very useful to increase collaboration from within applications across the enterprise. SharePoint exposes web services that can be used to get information from the lists and add/update information in the lists. I have provided a sample of code below that gets the collection of lists on a site. You can then use this to find the internal name of a list. This value will be needed to interact with other services pertaining to the specific list.

In the sample below, I compared the title of the list to the “Title” attribute which is the generic display name. The “Name” attribute is the internal name of the list.

:: begin code sample ::
:: listName will be equal to the GUID of the list corresponding to the specified name ::

string listName;
XmlNode col = null;
XmlDocument doc = null;
XmlElement elem = null;

//setup the service
_service = new wsLists.Lists();
_service.Url = _siteUrl + "/_vti_bin/Lists.asmx";

//get the collection of lists from the service
col = _service.GetListCollection();
//load the collection into a document
doc = new XmlDocument();
doc.LoadXml(col.OuterXml.Trim());
//the document will have one child node that will contain all children
if ((doc.HasChildNodes) && (doc.ChildNodes.Item(0).HasChildNodes))
{
//look through the children to find the desired node
foreach (XmlNode node in doc.ChildNodes.Item(0).ChildNodes)
{
if (node.NodeType == XmlNodeType.Element)
{
elem = (XmlElement)node;
if (elem.GetAttribute("Title") == "My List Name")
{
listName = elem.GetAttribute("Name");
break;
}
}
}
}

:: end code sample ::

Future blog(s):
  1. Part 2 of this entry discussing how to get a collection of the items in the list.

Friday, March 21, 2008

Authenticate via a web service in SharePoint

I thought that I would spend a little time talking about authenticating against SharePoint via a web service. This is something that most people will encounter if they wish to access information from a SharePoint list where SharePoint is secured via forms authentication.

Steps to authenticate (see example code below):

  1. Create a web reference to one of the authentication web service instances within SharePoint. It does not matter which one, so I usually just use the base one. In my example, I called the web reference “wsAuth”. The URL will follow the format “http(s)://mysharepointsite/_vti_bin/Authentication.asmx”.
  2. Create an instance of a cookie container and set it in the service. It will be used to maintain stateful communication with the web service and will be populated with the authentication information when you call the “Login” method.
  3. Set the URL if you wish to login to a specific section of the site. Note that the URL of the web reference will always default back to the base instance (regardless of where you create the web reference to).
  4. Call the “Login” method and get the result.

If successful, the cookie container will now have all of the necessary information for future calls to services that require authentication. You will need to set this object to the cookie container of the instance of a service (similar to below) and you should be able to successfully call the services on your SharePoint site. Note that the access to the site will be limited to that of the username/password being authenticated.

::begin code sample::

internal const string URL_AUTHENTICATION = "/_vti_bin/Authentication.asmx";

internal static CookieContainer Authenticate(string siteUrl, string username, string password)
{
CookieContainer cookieJar = new CookieContainer();
wsAuth.Authentication service = null;
wsAuth.LoginResult result;
try
{
service = new wsAuth.Authentication();
service.Url = siteUrl + URL_AUTHENTICATION;
service.CookieContainer = cookieJar;
result = service.Login(username, password);
switch (result.ErrorCode)
{
case wsAuth.LoginErrorCode.NoError:
//authentication was a success
break;
case wsAuth.LoginErrorCode.NotInFormsAuthenticationMode:
//there was an error with the authentication mode
break;
case wsAuth.LoginErrorCode.PasswordNotMatch:
//the username and/or password were invalid
break;
}
}
finally
{
if (service != null)
service.Dispose();
}
return cookieJar;
}

::end code sample::