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.

Advertisements

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

5 Comments »

  1. Creating Web Service for SharePoint « Improve Your Technology…

    Thank you for submitting this cool story – Trackback from Blue Ray Plus – Latest Technology News…

    Trackback by Blue Ray Plus - Latest Technology News | January 10, 2012 | Reply

  2. Hi to all, for the reason that I am actually eager of reading this
    website’s post to be updated daily. It consists of pleasant data.

    Comment by casquette monster energy | April 25, 2013 | Reply

  3. Useful info. Fortunate me I discovered your site by accident,
    and I am surprised why this accident didn’t happened in advance! I bookmarked it.

    Comment by green tea with jasmine | May 1, 2013 | Reply

  4. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog?
    My blog is in the exact same area of interest as yours and my
    users would definitely benefit from some of the information you
    provide here. Please let me know if this ok with you.

    Appreciate it!

    Comment by http://johnswiftd.blog.com | May 23, 2014 | Reply

    • This blog is just for knowledge sharing, so you can use the same content if you want.

      Comment by srinutamada | June 4, 2014 | Reply


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

%d bloggers like this: