Create an ASMX Web Service in SharePoint 2010 Using Visual Studio 2010

Back in SharePoint 2007, asmx web services were quite prevalent, thanks to the WSPBuilder tool, and it’s templates.   They are useful for executing actions between multiple web applications and can be used by client applications, as well.  Furthermore, InfoPath forms, deployed to SharePoint, could also use these asmx web services.
Unfortunately, Visual Studio 2010 did not come with a template for SharePoint web services.  So, today I will be writing about how we can create asmx web services for SharePoint 2010.  All you will need is SharePoint 2010.
First, start a new Empty SharePoint 2010 project.  I will call this SPASMXService.
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010_
Make sure to deploy it as a farm solution.
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

First, you need to close this project by right clicking on the project and then selecting ‘unload project’.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
Then, right click on the project again, and select, ‘Edit SPASMXService’.
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
Under <SandboxedSolution>False</SandboxedSolution> type in:

<TokenReplacementFileExtensions>asmx</TokenReplacementFileExtensions>

This will be the result:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Then, save and close out of this xml file.  This will allow Visual Studio to insert the solution information where necessary.  Therefore, this is a crucial step!  Finally, reload the project file.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Next, we will be creating the web service.  Right click on the project, and select “Add” and then select “New Class…”  This will be the code behind.  Let’s just call this SPASMXService.cs.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Now, open SPASMXService.cs, and make the following changes:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

This is a good start for a web service with a web method.  Now, of course we have a few errors because we still have not brought in the necessary libraries.  Right click on ‘references’ in the Solution Explorer, and select, ‘Add Reference’.  Select System.Web.Services from the .NET tab.  Then, in SPASMXService.cs, add, ‘using System.Web.Services’.  This should look like this:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Finally, we have to create the service page.  I like to keep it in the _layouts folder, but you can keep it elsewhere using similar steps.  Right click on the project item in the solution explorer, and select add -> SharePoint “Layouts” Mapped Folder.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

You can also select SharePoint Mapped Folder, and then select ISAPI.  This would cause the page to go into _vti_bin instead.

For now, I’m going to stick to _layouts:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

The SPASMXService folder was automatically made.  Nice.

Inside the SPASMXService, under Layouts, we will add a new file of type xml.  We Shall call it SPASMXService.asmx.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

The contents of SPASMXService.asmx will be a single line:

<%@ WebService Language="C#" Debug="true" Class="[Class path], $SharePoint.Project.AssemblyFullName$"  %>

Where [class path] is the full namespace name of the SPASMXService class in SPASMXService.cs.  In my case, the line will be:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

From:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

Finally, save everything, and then deploy the solution.

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010

If everything went right, you should see this using Internet Explorer:

How_to_Create_an_ASMX_web_service_on_SharePoint_2010,_using_Visual_Studio_2010
If you used ISAPI instead of Layouts, _layouts in that screenshot should be _vti_bin, instead.  If you opened this from a front end server with web service, then you can further test this web service by clicking on that link.

Lastly, a bit of trouble shooting; you can check on the web service page by going to:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS

If you used ISAPI instead of LAYOUTS, then instead go to:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

If the web service does not load on Internet explorer, then you should open the asmx page from one of these two locations.  If you open the asmx page from one of these two locations, and you still find “$SharePoint.Project.AssemblyFullName$”, then you need to go back to the top of this article and follow the steps regarding unloading and reloading the project.

Reference From : http://www.thesharepointblog.net/Lists/Posts/Post.aspx?List=815f255a-d0ef-4258-be2a-28487dc9975c&ID=67

Comments

Anonymous said…
Thank you for the nice detailed walk-through. I've been trying to create a Web Service for InfoPath and thanks to your work I was finally successful.
Anonymous said…
This is amazing... Thank you!
Anonymous said…
Thank you so much..After a tedious search of one day this really helped me.

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

Change Language for current user with JSOM in SharePoint Online

SharePoint 2010 CSS Chart