Friday, January 6, 2012

Use Web services to access Sharepoint List Items

The SharePoint client object model can be used in .NET, Silverlight application and ECMAScript. But the client object model cannot be used if you are working on earlier version of .NET framework or any other technologies, such as java or PHP. SharePoint offers some set of SOAP services that can be used by these applications.

These services are built using mainly the Web Services engine of ASP.NET, and are based on a set of .asmx files. They are published under the /_vti_bin/ virtual directory of every web site that maps the SharePoint14_Root\ISAPI folder.

Here I’m explaining how to use web services to access SharePoint list items in a c# console application.

Create new C# console application and name it as ‘TestSOAP’. To use these services, you need to define a reference to them, using their Web Services Definition Language (WSDL). To add this, right click on references in solution explorer and select ‘Add Services Reference..’



Add the URL of the service. I have created a site collection call ‘mcts’ and my services URL is ‘http://sharepoint-pc:3395/sites/mcts/_vti_bin/lists.asmx’. Click the Go button to see available services.

Click on Advanced button.



Click Add Web Reference button.



Enter the service URL and browse, add a name to the service reference and click Add Reference. I have put the name as ‘TestWebService’.

You have added the service reference to your project. The solution explorer will look like this.



I have created a SharePoint list call ‘Project Details’ in ‘http://sharepoint-pc:3395/sites/mcts’. Our job is access the data in Project Details list through web service. The Project Details list as this:



Following is the code to access the list items. You have to add following references:

using TestSoap.TestWebService;

using System.Xml.Linq;

using System.Xml;

string targetListName = "Project Details";

string baseUrl = "http://sharepoint-pc:3395/sites/mcts/_vti_bin/";

Lists wsLists = new Lists();

wsLists.Url = baseUrl + "Lists.asmx";

wsLists.Credentials = System.Net.CredentialCache.DefaultCredentials;

XElement listMetadata=XElement.Load(new XmlNodeReader(wsLists.GetList(targetListName)));

Guid targetListId = new Guid(listMetadata.Attribute("ID").Value);

XmlNode listItemsXmlNode = wsLists.GetListItems(

targetListId.ToString(), // ID of the target list

String.Empty, // ID of the view or String.Empty for default view

null, // CAML query or null

null, // ViewFields or null

"200", // RowLimit as a string

null, // Query options

null // ID of the web site or null for root website

);

XElement listItemsXml = XElement.Load(new XmlNodeReader(listItemsXmlNode));

var xmlItems = from x in listItemsXml.Descendants("{#RowsetSchema}row")

select x;

foreach (XElement xmlItem in xmlItems)

{

Console.WriteLine("{0} - {1} - {2}",

xmlItem.Attribute("ows_ID").Value,

xmlItem.Attribute("ows_Title").Value, xmlItem.Attribute("ows_Sponsor").Value);

//Console.WriteLine(xmlItem.Attribute("ows_Sponsor").Value);

}

Console.ReadLine();

Your output as follows:

1 comment:

  1. Hello Mohamed,
    Could you please brief on the object "wsLists" on your above code. (Lists wsLists = new Lists()) Do i need to write a separate class for "Lists" or is there any namespace available for this. Please address.

    ReplyDelete