Create ASMX Service For SharePoint

In this article I would like to share the steps to create an ASMX service for SharePoint and service Deployment steps.

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.

SharePoint

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.

SharePoint

Step 4

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

SharePoint

Step 5

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

SharePoint

Step 6

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

SharePoint

Step 7

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

SharePoint

Step 8

When you click on the new item, you will get a popup. Select "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.

SharePoint

Step 9

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

SharePoint

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.

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

 

SharePoint

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

SharePoint

Step 11

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

SharePoint

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.

SharePoint

Step 13

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Web.Services;  
  6. using Microsoft.SharePoint;  
  7. namespace SampleASMX {  
  8.     public class SampleASMX: WebService {  
  9.         [WebMethod]  
  10.         public string GetSiteListCount() {  
  11.             var web = SPContext.Current.Web;  
  12.             return (web.Title + " contains " + web.Lists.Count.ToString() + " lists.");  
  13.         }  
  14.     }  
  15. }  

Step 14

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

SharePoint

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

SharePoint

Step 16

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

SharePoint

Step 17

Click "Invoke" to test your service operation.

SharePoint

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

SharePoint

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.