一。Create a webservice on Sharepoint 2010 server
1. Create a ClassLibrary project calls 'MyServiceLibrary'.
2. Add two references: Microsoft.Sharepoint and System.Web.Services
3. Create a Class calls Service1
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World11111";
}
[WebMethod]
public List<User> GetLists()
{
List<User> toReturn = new List<User>();
foreach (SPList list in SPContext.Current.Web.Lists)
{
toReturn.Add(
new User() { Name = list.Title, Author = list.Author.Name }
);
}
return toReturn;
}
}
4. Go to Properties of 'MyServiceLibrary'-> Signing, create a strong name key calls MyService.snk.
5. Create a Asp.net Web Service Application project calls 'MyWebService'.
6. Add a new Web Service Item calls 'DemoWebService.asmx', and delete its .cs file.
7. Open DemoWebService.asmx, add a line as below:
You can drawing the MyServiceLibrary.dll to GAC and get the PublicKeyToken from the MyServiceLibrary.dll properties.
8. Copy DemoWebService.asmx into C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS
9. As the MyServiceLibrary.dll is existing in GAC, you can access the url as 'http://yourhost/_layouts/DemoWebService.asmx', you can see two functions as well.
二。Create a Console Application to access Web Service
1. Create a Console Application calls 'ConsoleApplication1'
2. Add a new Web Reference calls 'abc', as the url is 'http://yourhost/_layouts/DemoWebService.asmx'.
3. Modify the Program.cs file as below,