SharePoint2010 创建自定义webservice1

一。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:

<%@ WebService Language="C#" class="MyServiceLibrary.Service1, MyServiceLibrary, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=272075ee6da5d920" %>

  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,

复制代码
1 class Program
2 {
3 staticvoid Main(string[] args)
4 {
5 gssd3.Service1 s =new ConsoleApplication1.gssd3.Service1();
6 s.Credentials = GetNewWorkCredential();
7 gssd3.User[] users = s.GetLists();
8 Console.WriteLine(s.HelloWorld());
9 foreach(gssd3.User user in users)
10 {
11 Console.WriteLine("{0} {1}", user.Author, user.Name);
12 }
13
14 Console.ReadLine();
15 }
16
17 privatestatic NetworkCredential GetNewWorkCredential()
18 {
19 string account ="user1";
20 string password ="123";
21 string domain ="abcOffice";
22
23 if (account !=null&& password !=null&& domain !=null)
24 {
25 returnnew NetworkCredential(account, password, domain);
26 }
27 return (NetworkCredential)System.Net.CredentialCache.DefaultCredentials;
28 }
29 }
复制代码

这样就完成了!!!!

posted @ 2013-02-25 00:13  Star★  阅读(180)  评论(0编辑  收藏  举报