Write a Custom Web Service for SharePoint in Supported Mode
Introduction
If you have read my comments on the Writing Custom Web Service for SharePoint, Is it supported? (http://blog.libinuko.com/2011/02/16/sharepoint-writing-custom-web-service-for-sharepoint-is-it-supported/) ; you may have already created standard ASPNET web services. It is working, but with some limitation:
- You can not have path virtualization
Virtualization is one of SharePoint’s technique provided by SPVirtualPath provider, that enable virtualization of your web service path. For example, list.asmx will be available for http://mysite.com**/_vti_bin/lists.asmx** , or http://mysite.com/sites/myothersitecollection**/_vti_bin/lists.asmx**. The site collection has been virtualized by SharePoint.
Without virtualization any web service consumer will have to access to the same path, usually in the root; for example /_services/mywebservice.asmx">http://<webapps>/_services/mywebservice.asmx - You can not have SPContext
SPContext is very powerful object in SharePoint development. Using this context, we can retrieve current SharePoint context without having to instantiate it. For example, you can get current SPWeb by calling SPContext.Current.Web – and you don’t need to dispose it (in fact you’re not suppose to dispose it). Still using SPContext you can have direct access to list and everything under SharePoint.
Without having direct access to SPContext, you have to instantiate SPWeb or SPSite using normal constructor with URL as parameter. It means new SPSite/SPWeb thread in the server memory, and you have to dispose it once you’ve done working with it.
So, how can we write custom web service for SharePoint in supported mode?
Before we start, we have to understand SharePoint architecture and how does the processing works for web services. I take following picture from SharePoint Architecture in MSDN. It describe how SharePoint process our request. There is SPHttpApplication which has SPRequestModule and any additional ASP.NET Module; and before the request returned back to the user SPHttpHandler is doing the job.
How SharePoint process web services
If we dig into into the process on how SharePoint process request to web services in _vti_bin.
There are 3 scenarios of http request to the web services,
- Disco request, identified by suffix ?DISCO in the web service address. For example, */_vti_bin/list.asmx?Disco
- WSDL request, identified by suffix ?WSDL in the web service address. For example, */_vti_bin/list.asmx?Wsdl
- Web service post request. For example, */_vti_bin/list.asmx?op=GetListItems
Every request will be processed by SPHttpHandler (SharePoint) and ScriptHandlerFactory (system.web.extension), but the SPHttpHandler will be selective only for Disco and Wsdl request.
On disco/wsdl request, SPHttpHandler will transfer the request to wsdisco.aspx or wswsdl.aspx using Server.Execute operation. This operation ensure that wsdisco.aspx/wswsdl.aspx is receiving same request object. wsDISCO.aspx or wsWSDL.aspx will then instantiate SharePoint Context object. Any *.aspx will be successfully instantiate SharePoint context object because they are managed by SPHttpApplication and hence it also impacted by SPVirtualPath provider from SharePoint which will activate path virtualization.
Next, wsDISCO.aspx will transfer the request to the <service>DISCO.aspx and wsWSDL.aspx will transfer to the <service>WSDL.aspx – using Server.Execute operation. So here we have seen 2 transfer operation. At the end, the result is correct WSDL/DISCO request. The correct WSDL/DISCO will point to the correct virtual path of the request. (Remember how do you create <service>disco.aspx / <service>wsdl.aspx)
When a consumer use the contract and tries to consume it. The SPHttpHandler will no longer intercept the request, but the normal ScriptHandlerFactory from System.Web.Extensions. However with the correct path in disco/wsdl, now the asmx now have the ability to look into current context from SharePoint. And hence you will be able to use SPContext.Current.Web in custom web services.
Create Custom Web Service for SharePoint
Create Visual Studio Solution
- Open Visual Studio 2010
- Create Blank Visual Studio Solution. Give name for example DemoCustomWS.
Create ASP.NET Web Services
Add ASP.NET Web Service Application project. Give name for example CustomWS
Open Service1.asmx.cs and change the implementation accordingly. For example change the namespace, class name etc.
Open CustomWS project property and sign the assembly
Build and open the new web service in the browser
Open command prompt and run disco againts the web service url.
disco http://webserviceurl/customservice.asmx
The results are CustomService.disco and CustomService.wsdl which will be useful later in this process.
Still in the command prompt, execute
*sn -T DemoCustomWS.dll
*Open Service1.asmx markuppage, and change the markup accordingly using the PublicKeyToken from previous operation.
*For example:
* <% WebService Language="C#" Class="CustomWS.CustomService, CustomWS, PublicKeyToken=2938173ce" %>
Create SharePoint 2010 Project
Add Empty SharePoint Project. Give a name for example DemoWS.
Open DemoWS project property and sign the assembly.
Inside DemoWS open package designer and add Assembly from project output.
Select CustomWS project deploy in the GAC
Inside DemoWS add SharePoint Mapped Folder, select ISAPI\CustomWS. If the folder is not available, you can create ISAPI\CustomWS under SharePoint hive.
Add CustomService.disco and CustomService.wsdl to the CustomWS folder.
Rename CustomService.disco into CustomServiceDisco.aspx and CustomService.wsdl into CustomServiceWSDL.aspx
Change the implementation of CustomServiceDisco.aspx and CustomServiceWsdl.aspx in order to support path virtualization (See Writing Custom Web Services for SharePoint Products and Technology)
Add CustomService.asmx file to the CustomWS folder in DemoWS project.
Inside DemoWS add SharePoint Mapped folder, select ISAPI
Copy spdisco.aspx from ISAPI folder in SharePoint hive into the mapped folder ISAPI in the project.
Rename spdisco.aspx into spdisco.disco.aspx.
Create copy of spdisco.disco.aspx and rename it into DemoWS.spdisco.aspx
Remove all lines from DemoWS.spdisco.aspx leaving only registration for the new web services
Add new HttpHandler class to the DemoWS , the most important is on the ProcessRequest it will merge original SPDISCO.aspx and DemoWS.SPDISCO.aspx
01.public void ProcessRequest(HttpContext context) 02. { 03. StringWriter sw1 = new StringWriter(); 04. // Original - cop spdisco.aspx 05. context.Server.Execute("spdisco.disco.aspx", sw1); 06. XmlDocument spdiscoXml = new XmlDocument(); 07. spdiscoXml.LoadXml(sw1.ToString()); 08. 09. var files = Directory.GetFiles(context.Server.MapPath(""), "*.spdisco.aspx"); 10. foreach (var file in files) 11. { 12. StringWriter sw2 = new StringWriter(); 13. context.Server.Execute(System.IO.Path.GetFileName(file), sw2); 14. 15. XmlDocument otherSPDiscoXml = new XmlDocument(); 16. otherSPDiscoXml.LoadXml(sw2.ToString()); 17. foreach (XmlNode importedNode in otherSPDiscoXml.DocumentElement.ChildNodes) 18. { 19. spdiscoXml.DocumentElement.AppendChild(spdiscoXml.ImportNode(importedNode, true)); 20. } 21. } 22. 23. context.Response.Write(String.Format("<?xml version='1.0' encoding='utf-8' ?> {0}", spdiscoXml.InnerXml)); 24. }
Inside DemoWS add SharePoint Mapped folder, select CONFIG
Add webconfig.demows.xml to the CONFIG folder. Inside webconfig.demows.xml we will register configuration changes for the web config.
Finally you can build and deploy the solution.
More Information
- Writing Custom Web Services for SharePoint Products and Technology (http://msdn.microsoft.com/en-us/library/dd583131(v=office.11).aspx)
- SharePoint Architecture (http://msdn.microsoft.com/en-us/library/bb892189(v=office.12).aspx )
- Architectural Overview of Windows SharePoint Services (http://msdn.microsoft.com/en-us/library/dd583133(v=office.11).aspx)
- Modifying Built-In SharePoint Files (http://msdn.microsoft.com/en-us/library/bb803457(v=office.12).aspx)
Source code
- SharePoint 2010 and Visual Studio 2010 Sample Code : Demo Custom WS (http://code.msdn.microsoft.com/Writing-SharePoint-Web-in-cb9de1be )