Search This Blog

Friday, March 23, 2012

How to Create an ASMX Web Service on 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.































Make
sure to deploy it as a farm solution.










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















Then,
right click on the project again, and select, ‘Edit SPASMXService’.











Under <SandboxedSolution>False</SandboxedSolution> type in:




<TokenReplacementFileExtensions>asmx</TokenReplacementFileExtensions>





This will be the result:










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.
















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.




















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














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:

























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.

















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:





















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.

































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:













From:











Finally, save everything, and then
deploy the solution.




















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
















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.

No comments:

Post a Comment