Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

SharePoint 2010 – Web Services

Posted by JP on June 16, 2012


SharePoint Web Services provides is a good feature that allows us to access or change the SharePoint items remotely. We can use the Web Services to add more power to our application.

Following are the core features of SharePoint Web Services:

· Foundation and Server Web Services

· More Interoperability compared with Server Object Model

· Above 25 Web Services

· Manage Sites, Lists, Libraries, and Picture Libraries etc.

· Run queries against the server

· Custom Web Service creation possible

The following table contains some of the web services inside SharePoint 2010.

Service

Description

ASMX

WebSvcAdmin

Creation and Deletion of sites

Admin.asmx

WebSvcLists

List Management service

Lists.asmx

WebSvcAlerts

List Item Events service

Alerts.asmx

WebSvcAuthentication

Authentication based service

Authentication.asmx

WebSvcsites

Web sites management service

Sites.asmx

WebSvcspsearch

Searching service

Search.asmx

A more detailed list can be found here:

http://msdn.microsoft.com/en-us/library/ee705814.aspx

How to find the url of a Service?

The url of a service can be located inside _vti_bin folder of the site or site collection. Each site or site collection will be having this folder with all the web services inside it with .asmx extension.

Example: To use the search service use the following url:

http://YOURPCNAME/_vti_bin/search.asmx

clip_image002

Using http://appes-pc/_vti_bin/Lists.asmx

clip_image004

Adding Reference to Project

Now we can try adding reference to the service inside Visual Studio application. Create a new Ordinary Console Application (please note that we are not using SharePoint Console Application)

clip_image006

Now we need to Add Web Reference. In Visual Studio 2010 this option is available under the Add Service Reference option as shown below.

clip_image008

In the appearing dialog click the Advanced button.

clip_image010

In the next dialog, choose Add Web Reference option.

clip_image012

You will get the following dialog. This dialog is suitable for adding web service references.

clip_image014

Enter the url of the Lists.asmx as shown above. You need to replace the pc name with yours.

Set a Web reference name in the second highlighted box. This serves as the namespace.

Click the Add Reference button and your proxy class and related classes will be ready within a while. Inside the Program.cs enter the namespace and you can see the classes inside it as shown below:

clip_image016

Invoking the Proxy Class

Our class of interest is the Lists proxy. You can create an instance of it using the following code:

SPReference.Lists client = new SPReference.Lists();

Setting Credentials

Whenever we open a SharePoint site (without anonymous login enabled) the site will be prompting with user credentials. As we are invoking through web service the prompt dialog won’t be working. We need to pass the credentials using the property named Credentials.

client.Credentials = new NetworkCredential("appes", "password"); / // using System.Net;

Getting Reference to a List

We can try getting a reference to the Manager List inside our site. The Managers list contains items with columns:

· Title

· Name

· Address

clip_image018

Use the following code to fetch the data and display it to the console:

SPReference.Lists client = new SPReference.Lists();

client.Credentials = new NetworkCredential("appes", "PWD");

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");

viewFields.InnerXml = "<FieldRef Name=\"Title\" />" +

"<FieldRef Name=\"Name\" />" +

"<FieldRef Name=\"Address\" />";

XmlNode listItems = client.GetListItems("Manager", null, null, viewFields, null, null, null);

foreach (XmlNode node in listItems)

if (node.Name == "rs:data")

for (int f = 0; f < node.ChildNodes.Count; f++)

{

if (node.ChildNodes[f].Name == "z:row")

{

string title = node.ChildNodes[f].Attributes["ows_Title"].Value;

string name = node.ChildNodes[f].Attributes["ows_Name"].Value;

string address = node.ChildNodes[f].Attributes["ows_Address"].Value;

Console.WriteLine(title + " " + name + " " + address);

}

}

Console.ReadKey(false);

On executing the application we can see the following result:

clip_image020

Following are the steps involved in getting list item data:

1. Create List Proxy client

2. Set Credentials

3. Set the View Columns

4. Invoke GetListItems() method

5. Iterate through the nodes

6. Get value of node having name z:row

Note

Using web services, xml is used to represent the data. For advanced programming using properties please refer to the Server Object Model and SharePoint LINQ articles.

References

http://msdn.microsoft.com/en-us/library/ee705814.aspx

http://msdn.microsoft.com/en-us/library/ms583494.aspx

http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12).aspx

Summary

In this article we have seen the Web Services feature of SharePoint 2010. It provides the flexibility of interoperability compared with the Server Object Model. Developers can access and manage SharePoint site from non .Net platforms like Java. The attached source code contains the example we discussed.

Advertisements

2 Responses to “SharePoint 2010 – Web Services”

  1. Lokesh said

    I want the SharePoint search service application to be used by external .net application.Can anyone guide me in this scenario?

    Regards,
    Lokesh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s