Improve Your Technology

Just another blog for techology

Creating Web Service for SharePoint

Creating CustomSerive
1. Create a Web Service using Visual Studio 2010, and name the service as “CustomService”
2. Build your service logic in CustomService.vb (app_data)
3. Test your service code.
4. Add new “Class Project” under the same solution where you created “CustomService”
5. Name new project as “ServiceModel”
6. Move “CustomService.vb” file fromCustomService/app_data to the “ServiceModel”
7. Delete class1.vb file from the “ServiceModel”
8. Delete CustomService.vb file from the “CustomService” under app_data, if available.
9. Add SNK file to (ServiceModel) class library project.
10. Build class library dll i.e. ServiceModel.dll
11. Register ServiceModel.dll to the GAC
a. Drag ServiceModel.dll from project folder to C:\windows\assembly folder
or
b. Gacutil /i <Fullpatj>\ServiceModel.dll
12. Open CustomService.asmx file
13. Remove existing @Page directive with below code

<%@ WebService Language=”C#” %>

· Go to C:\Windows\assembly
· Right click on “ServiceModel.dll” and copy PublickKeyToken and paste in the above code.
14. Run “CustomService” service and check for errors.
a. If you getting soap error then check proper class name, dll name, culture and PublicKeyToken specified properly in the “CustomeService.asmx” file page directive.
b. If you are getting error even after specifying correct information then reload entire solution.

Adding CustomService to SharePoint.
1. Open .NET command prompt
2. Execute below statement
a. Disco http://ServerName/Project_Name/CustomService.asmx
3. Three files will be created under the folder from where you executed the above command
a. CustomService.Wsdl
b. CustomService. Disco
c. CustomService.discomap
4. Rename “CustomService.Wsdl” file to “CustomServiceWsdl.aspx”
5. Rename “CustomService.disco” file to “CustomServicedisco.aspx”
6. Open above files in Visual Studio 2010
a. Delete
<?xml version=”1.0″ encoding=”utf-8″?>
b. Add
<%@ Page Language=”C#” Inherits=”System.Web.UI.Page”%>
<%@ Assembly Name=”Microsoft.SharePoint, Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %> <%@ Import Namespace=”Microsoft.SharePoint.Utilities” %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<% Response.ContentType = “text/xml”; %>

c. Save both files with above changes
7. Open CustomServicedisco.aspx
a. Modify <contractRef> and <soap> elements with below code

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + “?wsdl”),Response.Output); %> docref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> xmlns=”http://schemas.xmlsoap.org/disco/scl/” />
    <soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> xmlns:q1=”http://schemas.microsoft.com/sharepoint/soap/” binding=”q1:ListServiceSoap” xmlns=”http://schemas.xmlsoap.org/disco/soap/” />
    <soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> xmlns:q2=”http://schemas.microsoft.com/sharepoint/soap/” binding=”q2:ListServiceSoap12″ xmlns=”http://schemas.xmlsoap.org/disco/soap/” />
8. Open CustomServicewsdl.aspx
a. Modify

<soap:address location=”http://server_name:New_Port/Project_Name/CustomService.asmx” />
With

<soap:address location=<% SPEncode.WriteHtmlEncodeWithQuote(Response,
 SPWeb.OriginalBaseUrl(Request), ‘”‘); %> />

b. Replace where ever

http://server_name:New_Port/Project_Name/CustomService.asmx

exists with

<% SPEncode.WriteHtmlEncodeWithQuote(Response,
 SPWeb.OriginalBaseUrl(Request), ‘”‘); %>
9. Copy CustomService.asmx, CusomServicewsdl.aspx, CusomServicedisco.aspx to _vti_bin virtual directory path.
a. Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

Adding Service to SharePoint.
1. Open spdisco.aspx file from “Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI” path
2. Add below XML tag before < /discovery> end tag.
<% SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + “/_vti_bin/Service1.asmx?wsdl”, ‘”‘); %>
 docRef=<% SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + “/_vti_bin/CustomService.asmx”, ‘”‘);
 %> xmlns=”http://schemas.xmlsoap.org/disco/scl/” /><soap address=<%
 SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + “/_vti_bin/ CustomService.asmx”, ‘”‘); %>
 xmlns:q1=”http://schemas.microsoft.com/sharepoint/soap/directory/” binding=”q1:CustomServiceSoap”
 xmlns=”http://schemas.xmlsoap.org/disco/soap/” />

Now Custom Service is being added to SharePoint site. You can call your service from any site
e.g. <Site_Address> /_vti_bin/CustomService.asmx
Important points to remember
1. XML is case sensitive, please have some care while adding values in upper and lower case
2. When adding references make sure that version and publickeytoken specified properly or not.

April 30, 2011 Posted by | Creating Web Service for SharePoint, SharePoint, Technology | | 5 Comments