Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Saturday, June 11, 2011

How to create a custom web service in SharePoint 2010

There are numbers of out of the box web services present in SharePoint that will do most of the 
common tasks like methods for working with list ,sites and sub sites, users and groups etc. For
using these web services you need to add the reference of the web service . But if your

requirement doesn't fit with any of the web methods like parameters of the method or functionality

of the method then you need to create a custom web service.

Below are the steps how you can create a custom web service.

Step: 1
Create an web service project in the visual studio. Name the project as TestServiceProject and rename the Service1.asmx file to TestWebService.asmx.

This .asmx file will contain the web methods and the programming logic. Now we 

can go to the TestWebService.asmx.cs page and define a web method.

Let's say i want to create a web method which will do some operations on the list

public string TestMethod(string listName , SPWeb oWeb)
//Write the logic for the operation

Step: 2

In Solution Explorer, right-click TestWebService.asmx and select View Markup.
You will see the following line:

<%@ WebService Language="C#" CodeBehind="Service1.asmx.cs" %>
Change this to:
<%@ WebService Language="C#" %>
Step: 3

Now create a strong key name for the class library project and compile the project

Step: 4

Now host this application in your IIS. Now we need to generate the .disco and the .wsdl files.
To generate the .disco and .wsdl file we need to run the disco.exe command.
The disco.exe file will be present at the path "C:\Program Files\Microsoft SDKs

Now open the command prompt and run the disco command as:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin > disco http://localhost:5050

Step: 5

Now open both the TestWebService.disco and TestWebService.wsdl file which will

be present at "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin" and replace

the XML processing instruction

"<?xml version="1.0" encoding="utf-8"?>"

<%@ Page Language="C#" Inherits="System.Web.UI.Page"%>
<%@ Assembly Name="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %> <%@ Import Namespace="Microsoft.SharePoint" %>
<% Response.ContentType = "text/xml"; %>

Step: 6

In TestWebService.disco file modify the contactref and the soap address tags to:

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode
(SPWeb.OriginalBaseUrl(Request) + "?wsdl"),Response.Output="");



%>xmlns="" />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode


%>xmlns:q1="" binding="q1:TestWebServiceSoap" 

xmlns="" />
<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode
%>xmlns:q2="" binding="q2:TestWebServiceSoap12" xmlns="" />  

Step: 7
In TestWebService.wsdl file modify the soap:address and soap12:address to:
<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request))
,Response.Output); %> />
<soap12:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request))
,Response.Output); %> />

Now save the two files as TestWebServicedisco.aspx and TestWebServicewsdl.aspx.
Step: 8

Now copy the files TestWebService.asmx, TestWebServicedisco.aspx and TestWebServicewsdl.aspx to the "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI" folder

Step: 9

To include the Web service in the list of web services on the server, open the spdisco.aspx file which is present at "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI" and add the following within the discovery element.
Now save the two files as TestWebServicedisco.aspx and TestWebServicewsdl.aspx.

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/TestWebService.asmx?wsdl"), Response.Output); %> docRef=
<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/TestWebService.asmx"), Response.Output); %>
xmlns="" />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/TestWebService.asmx"), Response.Output); %>
xmlns:q1="" binding="q1:TestWebServiceSoap" xmlns="" />

Step: 10

Now deploy the TestServiceProject.dll to GAC and the bin directory of the site on which you want to use the web service. Now you can consume the web service like the out of the box web service provided by SharePoint.

0 on: "How to create a custom web service in SharePoint 2010"