Search
Close this search box.

Accessing Sharepoint Data through Web Services

I’ve been developing a web application recently which needed to access a calendar in a sharepoint installation to retrieve events and display them. Under the covers in sharepoint, a calendar with events is more or less just a list. So we access the list. Here’s how I did it:

Step 1

Add a reference to the lists sharepoint web service in Visual Studio:

Step 2

Next we want to initialize a new instance of the Lists class in the referenced web service, and set the credentials to access the sharepoint site:

sharepoint.lists.Lists l = new sharepoint.lists.Lists();

//Supply the credentials to access sharepoint

System.Net.NetworkCredential cred = new System.Net.NetworkCredential("your username here", "your password here");

l.Credentials = cred;

Step 3

In order to retrieve the calendar events, we must get a list of items that belong to the calendar’s list. We do so using this piece of code:

XmlNode n = l.GetListItems("your calendar name here", null, null, null, null, null, null);

GetListItems takes a number of parameters, but the only needed one is the name of the calendar, the rest can be null unless you need them. This method returns an XML node with a collection of XML Child Nodes each one representing a list item, and in this case calendar items. An example of the XML it returns through the InnerXml property is this:

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

  <z:row ows_EventDate="2007-08-02 20:30:00" ows_EndDate="2007-08-03 00:30:00" ows_fRecurrence="0" ows_EventType="0" ows_Attachments="0" ows_WorkspaceLink="0" ows_Title="Consultants Night" ows_Location="ObjectSharp Training Room, 1 Yonge St., 19th Floor" ows_Description="<div></div>" ows_fAllDayEvent="0" ows__ModerationStatus="0" ows__Level="1" ows_ID="1" ows_owshiddenversion="1" ows_UniqueId="1;#{3912FED0-05B0-4B80-B452-2CF04C148FEC}" ows_FSObjType="1;#0" ows_Created="2007-07-29 21:24:47" ows_FileRef="1;#Lists/Calendar/1_.000" ows_MetaInfo="1;#" xmlns:z="#RowsetSchema" />

  <z:row ows_EventDate="2007-08-22 17:00:00" ows_EndDate="2007-08-22 17:00:00" ows_fRecurrence="0" ows_EventType="0" ows_Attachments="0" ows_WorkspaceLink="0" ows_Title="Test" ows_Description="<div></div>" ows_fAllDayEvent="0" ows__ModerationStatus="0" ows__Level="1" ows_ID="2" ows_owshiddenversion="1" ows_UniqueId="2;#{282B92B3-4B9B-4F78-B3CB-E8B9633793A1}" ows_FSObjType="2;#0" ows_Created="2007-08-22 16:29:59" ows_FileRef="2;#Lists/Calendar/2_.000" ows_MetaInfo="2;#" xmlns:z="#RowsetSchema" />

  </rs:data>

Step 4

We can extract the data of each individual event through iterating through each Xml child node, and outputting the requested values. An example of doing this is this:

for(int i = 0; i < n.ChildNodes[1].ChildNodes.Count;i++)

{

if(n.ChildNodes[1].ChildNodes[i].Attributes!=null)

Console.WriteLine("Event " + n.ChildNodes[1].ChildNodes[i].Attributes["ows_Title"].InnerText + " begins on " + n.ChildNodes[1].ChildNodes[i].Attributes["ows_EventDate"].InnerText);

}

This will produce the following output in the console window:

Event Consultants Night begins on 2007-08-02 20:30:00

Event Test begins on 2007-08-22 17:00:00

Thus we are done!

This article is part of the GWB Archives. Original Author: Matthew Cassell

Related Posts