Walkthrough: Creating a Custom ASP.NET (ASMX) Web Service in SharePoint 2010
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.
- 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).
- 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.
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.
<%@ 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.
- 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.
- Inside the MyCustomWebService.cs file, replace the code with the following.
public class MyCustomWebService : WebService
public string GetSiteListCount()
var web = SPContext.Current.Web;
return (web.Title + " contains " +
web.Lists.Count.ToString() + " lists.");
- 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.
- Click the GetSiteListCount link to navigate to a page that will allow you to test your service operation.
- Click invoke to test your service operation. A window should open that has XML contents that look something like this.
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.
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.
- In the Service Reference Settings dialog, click the Add Web Reference… button
- 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.
- 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.
- Inside Program.cs, replace the code with the following:
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();
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
- 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