Create ASMX Service For SharePoint

In this article, we will be seeing the steps to create ASMX Service for SharePoint.

Steps to create ASMX service

Follow the below-listed steps to create an ASMX service.

Step 1. Open Visual Studio on your SharePoint Server.

Visual Studio

Step 2. Then, create a new empty SharePoint project, provide the name for the project, and select the path.

SharePoint

Step 3. Enter the target web application URL for the deployment and debugging.

 URL

Step 4. Then, click on the Finish button to create a new project.

Finish button

Step 5. Right-click on the project and add the “mapped layout folder” as shown below.

Layout folder

Step 6. Under the layouts folder, you can see another subfolder with the project name as shown below.

Subfolder

Step 7. Right-click on the newly created “SampleASMX” folder and select Add >> New Item.

SampleASMX

Step 8. When you click on the new item, you will get a popup. Select the "General" category from the left navigation, select “Text File.txt”, and provide it a name in the textbox with the .asmx extension, as shown below.

Text File.txt

Step 9. Click on the “Add” button to create the .asmx file under the newly created folder.

Add

Step 10. Open the SampleASMX.asmx file and copy and paste the below markup into the file. Then, change the #assembly strong name# with the strong name of your assembly.

Here is the blog post describing how to get the strong name of your assembly by adding an External Tool into Visual Studio.

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

Assembly

After that, we will be changing the Strong Assembly Name.

Strong Assembly

Step 11. Right-click the Solution Explorer, add a class file, and provide the name for the class file, as shown below.

Solution Explorer

Step 12. After you have added the class file, add the “system.web.services” reference to the project which is available under the framework assemblies.

 Class file

Step 13. Then, open the .cs file which is newly created, and copy and paste the below code to the CS file.

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

namespace SampleASMX 
{
    public class SampleASMX : WebService 
    {
        [WebMethod]
        public string GetSiteListCount() 
        {
            var web = SPContext.Current.Web;
            return (web.Title + " contains " + web.Lists.Count.ToString() + " lists.");
        }
    }
}

Step 14. Finally, build and deploy the service by right-clicking on the project in the Solution Explorer and selecting Deploy.

Deploy

Step 15. After you have deployed the solution successfully, open the browser (i.e. IE or Chrome) and browse the below URL.

_layouts/15/SampleASMX/SampleASMX.asmx

SampleASMX

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

GetSite

Step 17. Click "Invoke" to test your service operation.

Invoke

Finally, a window should open that has XML contents, as shown below.

 XML

You can use this ASMX service on your client application.

Reference: https://msdn.microsoft.com/en-us/library/ms464040.aspx

Summary

In this article, we have explored how to create an ASMX service for SharePoint and Deployment steps.