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!

Print | posted @ Wednesday, August 22, 2007 4:43 PM

Comments on this entry:

Gravatar # re: Accessing Sharepoint Data through Web Services
by Narasimha Rao at 11/12/2008 3:32 AM

Good stuff and you save my day or two
Gravatar # re: Accessing Sharepoint Data through Web Services
by Harikrishnan at 11/20/2008 12:57 AM

hello narasimha
my aim is to fetch contact list data using asp.net application.for that i have done this.

the steps i followed are
1.created a new lists called contacts in sharepoint server 2007 portal.
2.add some data to it.
3.creata a asp.net website.
4.add web referance to http://server_name/_vti_bin/Lists.asmx
5.add u'r code in the page load event.
but i got a exception like this
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

what should i do?can you help me to achieve this objective.i'm not a expert in this field.so will u b kind enf to give me detailed step
by step instruction.
THANKS IN ADVANCE :)
Gravatar # re: Accessing Sharepoint Data through Web Services
by harikrishnan at 11/20/2008 1:03 AM

mr Cassell
can you please tell me how can i bring the contact list data to a list box in asp.net.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 7 and 4 and type the answer here: