创建Sharepoint 2010 custom WebService

一。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 static void 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 private static 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 return new NetworkCredential(account, password, domain);
26 }
27 return (NetworkCredential)System.Net.CredentialCache.DefaultCredentials;
28 }
29 }

这样就完成了!!!!

转载于:https://www.cnblogs.com/Forest-zhu/archive/2011/07/07/2100057.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值