So, you want to expose some SharePoint functionality to a web service, and you want to make sure it’s deployable. This is how we do it!
You have 3 major steps:
1. Build your web service
2. Set up the SharePoint Solution
3. Add the Solution to SharePoint and Deploy
Requirements before your start:
- Visual Studio
- Access to the SharePoint server where you want to deploy this service
Follow along below…
Build your web service
This is a great walkthrough for creating a custom sharepoint webservice: http://msdn.microsoft.com/en-us/library/ms464040.aspx
A few supplemental notes:
- if you plan on exposing SharePoint functionality, you will have to add a reference/using statement to Microsoft.SharePoint
- when changing your .asmx file, make sure you get the fully qualified name of your class and dll. For example:
<%@ WebService Language="C#" Class="CustomWebService.Service, CustomWebService, Version=220.127.116.11, Culture=neutral, PublicKeyToken=4eef54b119a62de3" %>
- Disco.exe is available in the Visual Studio 2008 > Visual Studio Tools > Visual Studio 2008 Command Prompt
- make sure you go to the _layouts directory where you’ve copied the .asmx when you run disco.exe
At the end of this process you should have:
- Access to http://myserver/_vti_bin/yourservice.asmx
- a solution like this:
- and, your CustomWebService.dll in the GAC
Set up the SharePoint solution
First, get all your necessary files into your Class Library project
1. Create folders to hold your files – in the Class Library project you created, right click > Add New > Folder, and create:
DeploymentFiles – to hold all the SharePoint solution generation files
ISAPI – to mimic the final location of all your web service files
Assembly – this is a personal preference of where I like to have my dlls deployed. If you use this, make sure you change the project properties to write the dlls here.
2. Move all the web service files into the ISAPI folder
3. Create the SharePoint Solution generation files in the DeploymentFiles folder:
- BuildSharePointPackage.ddf – this file tells makecab.exe which files in the solution will be included in the .WSP file and in what structure
; *** .ddf file for generating SharePoint solution.
.OPTION EXPLICIT ; Generate errors
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP;** All files are compressed in cabinet files
When you open up the CAB version of the WSP file, it will look like this:
- BuildSharePointPackage.Targets – tells Visual Studio how to call MakeCab.exe to create the WSP file
<Project DefaultTargets="BuildSharePointPackage" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Exec Command="C:\Windows\System32\MAKECAB.EXE /F DeploymentFiles\BuildSharePointPackage.ddf /D CabinetNameTemplate=$(MSBuildProjectName).wsp /D DiskDirectory1=WSPs\$(Configuration) "/>
<Exec Condition=" '$(Configuration)$(Platform)' == 'DebugAnyCPU'"
Command="$(MakeCabPath) /F DeploymentFiles\BuildSharePointPackage.ddf /D CabinetNameTemplate=$(MSBuildProjectName).cab /D DiskDirectory1=WSPs\$(Configuration)"/>
Please not that the <MakeCabPath> should point to wherever you have MakeCab.exe installed.
- manifest.xml – this files tells the WSP what to do with the files when the solution is deployed
<?xml version="1.0" encoding="utf-8" ?>
<Assembly DeploymentTarget="GlobalAssemblyCache" Location="CustomWebService.dll">
Lets break this part down, as it is important.
- SolutionId – get a new GUID to identify your solution
- <RootFiles> – this tag tells the solution to put files at the root lever, the 12 Hive
- <RootFile> – one tag for each file,the location property tells where in the WSP we are getting the file to place at the Root. B/c we put the the file into the WSP at the ISAPI path, the manifest will also deploy the file to the ISAPI path from the root location.
- <Assembly> – this tag will place the dlls in the appropriate location. The DeploymentTarget set to “GlobalAssemblyCache” will put the dll in the GAC.
So, now your solution should look like this:
4. Update the .csproj file to user MakeCab to generate the WSP
- Unload the project and edit the .csproj file
Find the line:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Below there, enter the following lines:
<Import Project="DeploymentFiles\BuildSharePointPackage.targets" />
<CallTarget Targets="BuildSharepointPackage" Condition=" '$(Configuration)$(Platform)' == 'DebugAnyCPU' " />
Save and Reload the project.
Build the project and you should have WSP files in a WSPs folder in the project. Make sure you include this folder in your project.
Add the WSP to SharePoint & Deploy!
use stsadm to add the solution to the central admin solution store
from the 12 hive/bin, open a command window and run the following command:
stsadm –o addsolution –filename [Location of your .wsp file]
Then go to Central Administration > Operations > Solution Management
You should see your wsp in there. Click the name and deploy it immediately.
Verify that :
- your dll is in the GAC
- your .asmx, xxxwsdl.aspx, and xxxdisco.aspx are in the 12 hive/ISAPI folder
- and that you can navigate now to http://yourserver/_vti_bin/yourservice.aspx
And you are finished! congrats!