Walkthrough: Creating a Custom ASP.NET (ASMX) Web Service in SharePoint 2010

Published on: Author: Rob Windsor 1 Comment
There is a walkthrough on this subject on MSDN. I’ve seen a few people comment that they have had issues trying to follow the walkthrough so I thought I’d create an alternate version.

Update: A couple of people have asked for information on how to consume the service so I’ve added an extra section at the bottom.

Creating the ASP.NET Web Service


  • In Visual Studio, click File | New | Project…
  • In the New Project dialog, select the Visual C# > SharePoint > 2010 node under Installed Templates.
  • Select the Empty SharePoint Project template, set the Name to WebServiceDemo and click OK.

Capture601

 
  • In the SharePoint Customization Wizard, enter the site you want to use for debugging, select Deploy as a farm solution, and click Finish. We are creating a farm solution because we need to deploy files to the SharePoint system folders (14 hive).

Capture602

 
  • In the Solution Explorer, right-click on the project and select Add | SharePoint “Layouts” Mapped Folder. You should see two new folders added to your project.

Capture603

 

Note: Normally web services go into the ISAPI folder. However, if we do that we will need go through all of the pain steps in the Generating and Modifying Static Discovery and WSDL Files section of the original walkthrough. Instead we are going to put the web service in a folder under the Layouts folder where these steps are not required. I have used this technique in the past have have not found any reason why custom ASP.NET web services should not be deployed under the layouts folder.
  • Right-click on the newly created WebServiceDemo folder, and select Add | New Item…
  • In the Add New Item dialog select Visual C# > General from Installed Templates, set Name to MyCustomWebService.asmx and click Add.

Capture604

 

<%@ WebService Class="WebServiceDemo.MyCustomWebService, #assembly strong name#" %>

  • In the Solution Explorer, right-click on the project and select Add | Class…
  • In the Add New Item dialog, set Name to MyCustomWebService.cs and click Add.

Capture605

 
  • Before we add the code to the class, we need to add a reference. In the Solution Explorer, right-click on the project and select Add Reference…
  • In the Add Reference dialog, select the .NET tab, select System.Web.Services, and click OK.

Capture606

 
  • Inside the MyCustomWebService.cs file, replace the code with the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using Microsoft.SharePoint;

namespace WebServiceDemo
{
    public class MyCustomWebService : WebService
    {
        [WebMethod]
        public string GetSiteListCount()
        {
            var web = SPContext.Current.Web;

            return (web.Title + " contains " +
                web.Lists.Count.ToString() + " lists.");
        }
    }
}

  • Deploy the service by right-clicking on the project in the Solution Explorer and selecting Deploy
  • Open Internet Explorer and navigate to your test SharePoint site. In the address bar add /_layouts/WebServiceDemo/MyCustomWebService.asmx and press Enter. You should see a service test page like the following.

Capture607

 
  • Click the GetSiteListCount link to navigate to a page that will allow you to test your service operation.

Capture608

 
  • Click invoke to test your service operation. A window should open that has XML contents that look something like this.

Capture609

 

Creating a Sample Client Application


  • In Visual Studio, click File | New | Project…
  • In the New Project dialog, select the Visual C# node under Installed Templates.
  • Select the Console Application template, set the Name to WebServiceClient and click OK.

Capture610

 

Note: The next thing we need to do is add a reference to the service to create the service proxy class. Since we created an ASP.NET (ASMX) Web Service instead of a WCF Web Service we need to use the Add Web Reference dialog. The steps to do this may not be obvious if you do not know what to look for.
  • In the Solution Explorer, right-click on your project and select Add Service Reference…
  • In the Add Service Reference dialog, click the Advanced button.

Capture611

 
  • In the Service Reference Settings dialog, click the Add Web Reference… button

Capture612

 
  • In the Add Web Reference dialog, put the URL of your service (that is, the URL for your SharePoint site plus /_layouts/WebServiceDemo/MyCustomWebService.asmx ) in the URL bar and press Enter. You should see a page similar to the one shown below in the browser area of the form.
  • Set the Web reference name to DemoProxy and click the Add Reference button.

Capture613

 
  • You should see a few changes in the Solution Explorer. The important one is the addition of the Web References and DemoProxy nodes. This indicates your service proxy has been created.

Capture614

 
  • Inside Program.cs, replace the code with the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebServiceClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var proxy = new DemoProxy.MyCustomWebService();
            proxy.Url = "http://win7virtualbox/sites/demo/" + 
                "_layouts/WebServiceDemo/MyCustomWebService.asmx";
            proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            var response = proxy.GetSiteListCount();
            Console.WriteLine(response);
        }
    }
}

An explanation of the code is needed.
  • The first line creates an instance of the service proxy class that was generated when we added the web reference.
  • The second line sets the SharePoint context. I can call this service from the context of different SharePoint sites so it is this URL which determines the site name and list count that will be returned to me. As an example, the first of the following URLs calls the service from the Demo site while the latter calls it from the Sales site
    • http://win7virtualbox/sites/demo/_layouts/WebServiceDemo/MyCustomWebService.asmx
    • http://win7virtualbox/sites/sales/_layouts/WebServiceDemo/MyCustomWebService.asmx
  • The third line sets the credentials of the caller. In this case it uses the credentials of the currently logged in Windows user.
  • The fourth line calls the GetSiteListCount method and the last line writes out the response.

The final step is to test the client application.
  • Press F5 to run the client application. You should see results similar to the following

 

Capture615

One Response to Walkthrough: Creating a Custom ASP.NET (ASMX) Web Service in SharePoint 2010 Comments (RSS) Comments (RSS)

  1. You made this so much easier than the material Microsoft provides. Thank you so much for the detailed information. You just made my day. Thanks a lot!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>