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.
Create Blank Visual Studio Solution. Give name for example DemoCustomWS.
Create ASP.NET Web Services
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.