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

Published on Author Rob Windsor32 Comments

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.

Update 2: A few people have had issues getting the service to work correctly. I recorded myself going through the walkthrough in hopes to address any confusion. See the bottom of this post for the link to the video.

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.
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
        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.



  • 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:
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/" + 
            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
    • 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




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

  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!!

  2. WOW

    Great, Great and once again, Great article!
    Thank you!
    U Rule!
    This way is awesome, way better then microsoft tutorial.


    • I’ll need more information to be able to help you. What is the error message? Do you get a correlation id? Have you tried to debug using Fiddler?

  3. There’s a problem the asmx service always defaults to the root mysite.com/_layouts/service/service.asmx instead of mysite.com/sitepath/_layouts/service/service.asmx

    • If you’re talking about the service test page, then that’s fine. The service test page isn’t really making a web service call, it just simulates doing so. Once the service is deployed, you set the site URL (i.e. the SharePoint context) using the URL property of the proxy object.

  4. Rob,

    Great article and very useful for what I am trying to do, but I keep getting the following error

    The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

    and in my .asmx file I have the following that is causing the error it seems

    Any help would be appreciated. Thanks.

  5. Thanks for this great article. The issue that I am having is when I deploy the webservice and try to run it I get a runtime error. The custom error mode in the webconfig is set to “off”, is there a way to debug the service ?

    • Are you using SharePoint 2010 or 2013? How did you deploy the solution package? Have you checked the SharePoint log files for additional error information?

  6. Thank you very much for the article.

    I followed the steps but after deployment when I try to access the sharepoint URL I am getting runtime error

    Server Error in ‘/’ Application.

    Runtime Error
    Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

    Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a “web.config” configuration file located in the root directory of the current web application. This tag should then have its “mode” attribute set to “RemoteOnly”. To enable the details to be viewable on remote machines, please set “mode” to “Off”.

    Can you please help me on this issue. I tried creating a new webconfig file in the solution but of no help.

    Thanks in advance,

    • Are you using SharePoint 2010 or 2013? How did you deploy the solution package? Have you checked the SharePoint log files for additional error information?

    • The web service is contained in a Solution Package (wsp). When you deploy the WSP using Powershell or Central Administration, SharePoint ensures that the necessary files get copied to each server in the farm.

  7. Thank you so much, having created plenty of .asmx web services in the past I suddenly found myself at a loss of how best to add one to an existing project to try to leverage JSON and a nice jquery data grid. Touch wood you have answered all my questions (add a text file and rename it to .asmx for example!).

    Thanks again.

  8. I tried your code and it work perfectly for testing machine , i tried to deploy it ,, the following error appears :
    The test form is only available for requests from the local machine.
    i googled this error , and found single solution which is by adding :

    into system.web/webservice/protocols section in host website’s web.config , but still not working ,,
    any idea ?

    • Are you using SharePoint 2010 or 2013? How did you deploy the solution package? Have you checked the SharePoint log files for additional error information?

  9. Server Error in ‘/’ Application.

    The resource cannot be found.

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /_layouts/WebServiceDemo/MyCustomWebService.asmx

  10. I am following the steps you have and deployed the file, but I am getting an unexpected error.

    The file ‘/_layouts/WebServiceDemo/MyCustomWebService.asmx’ does not exist. i
    It was deploying to the app server, so I copied the folder to the Layouts folder on the Web Server but I am still having issues.

    Any ideas?

    • It sounds like you are deploying files by manually copying them from your development box to your server. You instead need to deploy using SharePoint’s Solution Package deployment model. The YouTube video that accompanies this blog post (https://www.youtube.com/watch?v=BbCGpCklbP0) shows how to do this starting at the 17:00 minute mark.

      • yes, I figured that out right after I submitted the comment. duh :) Thanks, this post was a big help to getting my web service to work!

  11. Thank you it’s really useful artical.
    but i have some problem that this webservice can’t access by anonymous user.
    i want to access it without setting the credentials or set add specific user how can read this webservice only.
    how can i do that??
    Thanks again.

  12. Hi

    My web service works perfectly fine in local. After deployment on another server it gave me unexpected error from browser but when I called it from my local to that web service from the code it gave me correct response.
    When i use web service studio tool i got this error.System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.InvalidOperationException: Client found response content type of ‘text/html; charset=utf-8’, but expected ‘text/xml’.
    The request failed with the error message:

    with this it also gave me web service description in htm in error text.

    Please let me know why it is not running correctly from the browser.


    • How did you deploy to the other server? Did you use a Solution Package as I demonstrate in the YouTube video?

  13. Thanks Rob ! I also get an 401 issue when I consume my custom web service from an iPhone app. How can I authenticate for my request.

Leave a Reply

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