Using custom web services in SharePoint

If you are developing custom SharePoint applications there is a good chance that you are going to need some custom web services at some point, especially if you are making use of AJAX and JSON.  There is a great walkthrough over on MSDN on how to do this, but if you just need a web service to enable AJAX on a page it is a bit of overkill, especially since there is no way to automate deployment on some of the steps they detail.  I also recently saw a presentation where the presenter put his custom web service in a separate web application, I’m going to show that this really isn’t necessary, and you can fully automate your web service deployments through solution packages.  In the long run this method of deployment will make your life much easier.

 

I use WSPBuilder for all my SharePoint development.  I know there are other tools out there but I’ve been using WSPBuilder since 2007 and it hasn’t failed me yet (Thanks Carsten!).  Why fix it if it ain’t broke?  For this reason I always structure my SharePoint projects in the WSPBuilder method, which mirrors the SharePoint 12 Hive.

The first thing you want to do is create a new class library and build out your folder structure.  You can use “wspbuilder – createfolders” to do this, but it will give you a bunch of folders you won’t need for this.  I opted to create mine by hand. 

You should then add a folder under “LAYOUTS” to separate your custom code from SharePoint out of the box code.

In the “LAYOUTS” folder you can add your .asmx file.  Unfortunately Visual Studio won’t let you add web services to a class library project, so I usually just add a text file, rename the extension, and add the appropriate markup inside the file.

You’ll also want to add the code behind file.  For the purposes of this sample I’ve kept them both in the same assembly, but they don’t have to be.  Here is what my project looks like so far.

image

Inside ServiceSample.asmx I have the following single line which tells the web service which code behind assembly and class to use:

 <%@ WebService Language="C#" Class="CustomWebServiceSample.ServiceSample,CustomWebServiceSample,Version=1.0.0.0,Culture=neutral" %>

Inside ServiceSample.cs I have the following code:

 public class ServiceSample : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetServerName()
        {
            return HttpContext.Current.Server.MachineName; 
        }
        [WebMethod]
        public string GetCurrentSiteUrl()
        {
            return SPContext.Current.Web.Url.ToString();
        }
        
    }

  

The important thing to note here is that your SPContext will still be valid, as long as you enter the web service through the appropriate URL.  My development machine is named wssdev, so if I go to the web service through https://wssdev, I’ll get the context for the root site collection.  If I go to the service through https://wssdev/sites/<sitename>, I’ll get the context for that site collection. 

I have a post build script that copies my assembly and .asmx file to the appropriate location, if you package this using wspbuilder you won’t have to do this step, but it helps during development.  Here is the script:

 @SET TEMPLATEDIR=c:\program files\common files\microsoft shared\web server extensions\12
xcopy /e /y bin\debug\CustomWebServiceSample.dll %BINDIR%

xcopy /e /y /q 12\* "%TEMPLATEDIR%"

Compile, build/deploy your wsp, or run the post build script, and you should be ready to go.

image

 

It really is that simple to deploy your custom SharePoint web services.  The important take aways here is that this works with standard solution package deployment and is very simple as opposed to some of the much more complex ways I have seen people doing this.

Comments

  • Anonymous
    August 31, 2009
    HI ! This post was nice, easy to understand and very helpful. Personally, even I have been using only WSP builder for deployment. Do we always need to create a seperate VS project for the webservice or can be included in the Webpart solution by including the ASPX in the /ISAPI path under 12 hive? Any kind of help appreciated ! Thanks, Akshay

  • Anonymous
    September 10, 2009
    Very good Article. I've tired the steps but getting following error : File Not Found.   at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)   at System.RuntimeType.PrivateGetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)   at System.Type.GetType(String typeName, Boolean throwOnError)   at System.Web.UI.SimpleWebHandlerParser.GetType(String typeName) I believe the error is Sharepoint service cannot find DLL. I had deployed already into GAC and also put the DLL in BIN folder. Any suggestion on this would be appreciated.

  • Anonymous
    October 20, 2009
    I have created a custom web service and deploy it in _vti_bin folder and SPContext.Current.Web.Url always return the root web url. I have deploy the same web service to the _layouts folder as well, same result. not sure what I have done wrong.

  • Anonymous
    April 14, 2010
    Hi i am trying to create a autocomplete textbox using webservice and jquery. while i render the output as json, it is not recognized by the webservice. Any ideas on how to proceed