Wednesday, October 29, 2008

How to use Sharepoint Lists as Web Services

I had a sharepoint list of Agency names, that will continually be changing. I wanted this list of Agencies to show up on a different website in a drop down list. Here is what I did to make that happen:

  1. First you have to figure out the GUID for your listName and viewName. How do you do that? Well the simplest way is to open up the list in Sharepoint. Open up the List Settings by clicking on Settings -> List Settings. Now look at the URL. You should see the GUID for your list Name (note it is URL encoded) Visit this link for a quick conversion tool. Now scroll down to the bottom of the page to find the views. Click to edit the View that you want the GUID for. Now look in the URL again and you will find the GUID for the viewName.
  2. And now, here is the code:


// Declare and initialize a variable for the Lists Web Service.
servername.Lists listService = new servername.Lists();


/* Authenticate the current user by passing their default
credentials to the Web Service from the system credential cache.
*/
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;


// Set the Url property of the service for the path to a subsite.
listService.Url = http://servername.domain.org:portnumber/_vti_bin/Lists.asmx;


// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();


/* Assign values to the string parameters of the GetListItems method,
using GUIDs for the listName and viewName variables*/

string listName = "{listName GUID goes here}";
string viewName = "{viewName GUID goes here}";
string rowLimit = "150";


/*Use the CreateElement method of the document object to create elements
for the parameters that use XML*/

XmlElement query = xmlDoc.CreateElement("Query");
XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");


/* Declare an XmlNode object and initialize it with the XML response from
the GetListItems method.*/

XmlNode nodeListItems = listService.GetListItems(listName, viewName,query,viewFields,rowLimit,queryOptions, null);
/* Declare an XmlNode object and initialize it with the XML response from
* the GetListItems method. The last parameter specifies the GUID of the Web site
* containing the list. Setting it to null causes the Web site specified by the Url
* property to be used.*/

XmlDocument xd = new XmlDocument();
xd.LoadXml(nodeListItems.OuterXml);
XmlNamespaceManager nm = new XmlNamespaceManager(xd.NameTable);
nm.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
nm.AddNamespace("z", "#RowsetSchema");
XmlNode itemCount = nodeListItems.SelectSingleNode("//rs:data/@ItemCount", nm);
if (Convert.ToInt32(itemCount.Value)==0)
Label2.Text="No values returned";
else
{
nl = xd.SelectNodes("//rs:data/z:row/@ows_Title", nm);
nlEmail = xd.SelectNodes("//rs:data/z:row/@ows_Title//rs:data/z:row/@ows_Contact_x0020_Email", nm);
nlParent = xd.SelectNodes("//rs:data/z:row", nm);
ddlAgencyList.Visible = true;

// Loop through each node in the XML response and display each item.
foreach (XmlNode listItem in nlParent)
{
for (int i=0; i< listItem.Attributes.Count; i++)
//This iterates through each Agency (the count is the number of attributes of the agency
{
if (listItem.Attributes.Item(i).Name == "ows_Title")
{
ddlAgencyList.Items.Add(new ListItem(listItem.Attributes.Item(i).Value));
}
}
}

No comments: