Thursday, November 5, 2015

Create and Host Custom Web Service on SharePoint 2010, Using Visual Studio 2010



This Document will describe how to create a custom web service and deploy it in SharePoint Site.
Create an empty SharePoint 2010 project named [Project Name] selecting language as Visual C#.

Specify the local site to be used for debugging as [Server URL] selecting the option “Deploy as a farm solution”.

Right click on the project and select ‘Unload Project’.

Right click on the project again and select “Edit [Project Name].csproj”


Under the <SandboxedSolution>False</SandboxedSolutiontype in:

<TokenReplacementFileExtensions>asmx</TokenReplacementFileExtensions>

This will be the result: [Code Here]




Save and close out of this xml file.  This will allow Visual Studio to insert the solution information where necessary.  Therefore, this is a crucial step!  Finally, reload the project file.



 Next, we will be creating the web service.  Right click on the project, and select “Add” and then select “New Class…”  This will be the code behind.  Let’s just call this <Class Name>.cs.
Right click on ‘references’ in the Solution Explorer, and select, ‘Add Reference’.  Select System.Web.Services from the .NET tab.  Then, in <ClaccName>.cs, add, ‘using System.Web.Services’.  This should look like this:



Open <ClaccName>.cs, and make the following changes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
 namespace SPWebService
{     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1), WebService(Namespace = "http://tempuri.org/")]    class SPWebService : System.Web.Services.WebService    {         [WebMethod(EnableSession = true, Description = "Typical Web Method.")]        public string HelloWorld()        {            return "Hello World";        }    } }







Right click on the project item in the solution explorer, and select add -> SharePoint “Layouts” Mapped Folder.

Inside the Project, under Layouts, we add a new file of type xml and name it as <Name>.asmx.




The contents of SPWebService.asmx will be a single line:

<%@ WebService Language="C#" Debug="true" Class="[Class path], $SharePoint.Project.AssemblyFullName$"  %>




Where [class path] is the full namespace name of the SPWebService class in SPWebService.cs.  In my case, the line will be:


Finally, save everything, and then deploy the solution.


If everything went right, you should see this using Internet Explorer:




To use the web service we have just developed we need to create discovery (.disco) and WSDL (.wsdl) files. To generate the disco and wsdl you need to open the visual studio command prompt.
disco http://myserver/myservice.asmx /out:c:\webservicefiles
Here the out parameter defines where the output disco and wsdl file will be placed. Change the url http://myserver/myservice.asmx to one where you deployed your service temporarily.



Customize disco file: After successfully running the disco command you'll find two files in the output directory. Open the disco file in Notepad:
  • replace the line
<?xml version="1.0" encoding="utf-8"?>
with
<%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<% Response.ContentType = "text/xml"; %>
  • replace the following lines in the disco file
  <contractRef ref="http://server1/RepositoryService.asmx?wsdl" docRef="http://server1/RepositoryService.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
  <soap address="http://server1/RepositoryService.asmx" xmlns:q1="http://server1.org/" binding="q1:RepositoryServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
  <soap address="http://server1/RepositoryService.asmx" xmlns:q2="http://server1.org/" binding="q2:RepositoryServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

with
    <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://tempuri.org/" binding="q1:HelloWorld" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
    <soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> xmlns:q2="http://tempuri.org/" binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

Customize wsdl file: Open the wsdl file.
  • replace the following line:
<?xml version="1.0" encoding="utf-8"?>
with
<%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<% Response.ContentType = "text/xml"; %>
  • find the following two lines in the wsdl file
<wsdl:service name="RepositoryService">
    <wsdl:port name="RepositoryServiceSoap" binding="tns:RepositoryServiceSoap">
      <soap:address location="http://server1/RepositoryService.asmx" />
    </wsdl:port>
    <wsdl:port name="RepositoryServiceSoap12" binding="tns:RepositoryServiceSoap12">
      <soap12:address location="http://server1/RepositoryService.asmx" />
    </wsdl:port>
  </wsdl:service>

and in the above section replace the location with value
<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>
So the finally the section will look like:
<wsdl:service name="RepositoryService">
    <wsdl:port name="RepositoryServiceSoap" binding="tns:RepositoryServiceSoap">
      <soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />
    </wsdl:port>
    <wsdl:port name="RepositoryServiceSoap12" binding="tns:RepositoryServiceSoap12">
      <soap12:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />
    </wsdl:port>
  </wsdl:service>

Rename the wsdl and disco file:
  • Rename the CustomWebServiceDemo.disco file to CustomWebServiceDemodisco.aspx
  • Rename the CustomWebServiceDemo.wsdl file to CustomWebServiceDemowsdl.aspx
Add the Mapped Folder:



Select the ISAPI Folder and inside that create a folder “CustomWebServiceDemo” to copy files



Now copy three files yourservice.asmx, yourservicedisco.aspx and yourservicewsdl.aspx in the location like “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\CustomWebServiceDemo".



Open the file spdisco.aspx on location "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\CustomWebServiceDemo ". Now add the following two lines inside <discovery></discovery> tag. Remember to replace the MyService.asmx with your service name in the following code snippet.
     <contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/CustomWebServiceDemo/ CustomWebServiceDemo.asmx?wsdl"), Response.Output); %> docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/CustomWebServiceDemo/CustomWebServiceDemo.asmx"), Response.Output); %> xmlns=" http://schemas.xmlsoap.org/disco/scl/ " /> 
    <discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/ CustomWebServiceDemo/CustomWebServiceDemo.asmx?disco"),Response.Output); %> xmlns="http://schemas.xmlsoap.org/disco/" />


Finally Deploy the Solution and test your Service is working fine host in the _vti_bin.




1 comment: