SharePoint: Writing Custom Web Service for SharePoint in Supported Mode (part-1)
English
If you have read my comments on the Writing Custom Web Service for SharePont, Is it supported here (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”>/_services/mywebservice.asmx”>http://
/_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 doint 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
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.
Part-2 is how to start create custom web services. I will need help from my buddy Denni , he will code for me. –
References
1. Writing Custom Web Services for SharePoint Products and Technology (http://msdn.microsoft.com/en-us/library/dd583131(v=office.11).aspx.aspx”))
2. SharePoint Architecture (http://msdn.microsoft.com/en-us/library/bb892189(v=office.12).aspx.aspx”) )
3. Architectural Overview of Windows SharePoint Services (http://msdn.microsoft.com/en-us/library/dd583133(v=office.11).aspx.aspx”))
4. Modifying Built-In SharePoint Files (http://msdn.microsoft.com/en-us/library/bb803457(v=office.12).aspx.aspx”))