创建sharepoint 2010 web service

原文:http://msmvps.com/blogs/windsor/archive/2011/11/04/walkthrough-creating-a-custom-asp-net-asmx-web-service-in-sharepoint-2010.aspx

 

先把原文贴过来,有时间再翻译下

Creating the ASP.NET Web Service
  • In Visual Studio, click File | New | Project…
  • In the New Project dialog, select the Visual C# > SharePoint > 2010 node under Installed Templates.
  • Select the Empty SharePoint Project template, set the Name to WebServiceDemo and click OK.

    image

    • In the SharePoint Customization Wizard, enter the site you want to use for debugging, select Deploy as a farm solution, and click Finish. We are creating a farm solution because we need to deploy files to the SharePoint system folders (14 hive).

      image

      • In the Solution Explorer, right-click on the project and select Add | SharePoint “Layouts” Mapped Folder. You should see two new folders added to your project.

        image

        Note: Normally web services go into the ISAPI folder. However, if we do that we will need go through all of the pain steps in the Generating and Modifying Static Discovery and WSDL Files section of the original walkthrough. Instead we are going to put the web service in a folder under the Layouts folder where these steps are not required. I have used this technique in the past have have not found any reason why custom ASP.NET web services should not be deployed under the layouts folder.

        • Right-click on the newly created WebServiceDemo folder, and select Add | New Item…
        • In the Add New Item dialog select Visual C# > General from Installed Templates, set Name to MyCustomWebService.asmx and click Add.

          image

            <%@ WebService Class="WebServiceDemo.MyCustomWebService, #assembly strong name#" %>
            • In the Solution Explorer, right-click on the project and select Add | Class…
            • In the Add New Item dialog, set Name to MyCustomWebService.cs and click Add.

              image

              • Before we add the code to the class, we need to add a reference. In the Solution Explorer, right-click on the project and select Add Reference…
              • In the Add Reference dialog, select the .NET tab, select System.Web.Services, and click OK.

                image

                • Inside the MyCustomWebService.cs file, replace the code with the following.
                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Web.Services;
                  using Microsoft.SharePoint;
                  
                  namespace WebServiceDemo
                  {
                      public class MyCustomWebService : WebService
                      {
                          [WebMethod]
                          public string GetSiteListCount()
                          {
                              var web = SPContext.Current.Web;
                  
                              return (web.Title + " contains " +
                                  web.Lists.Count.ToString() + " lists.");
                          }
                      }
                  }
                  • Deploy the service by right-clicking on the project in the Solution Explorer and selecting Deploy
                  • Open Internet Explorer and navigate to your test SharePoint site. In the address bar add /_layouts/WebServiceDemo/MyCustomWebService.asmx and press Enter. You should see a service test page like the following.

                    image

                    • Click the GetSiteListCount link to navigate to a page that will allow you to test your service operation.

                      image

                      • Click invoke to test your service operation. A window should open that has XML contents that look something like this.

                        image

                        Creating a Sample Client Application
                        • In Visual Studio, click File | New | Project…
                        • In the New Project dialog, select the Visual C# node under Installed Templates.
                        • Select the Console Application template, set the Name to WebServiceClient and click OK.

                        image

                        Note: The next thing we need to do is add a reference to the service to create the service proxy class. Since we created an ASP.NET (ASMX) Web Service instead of a WCF Web Service we need to use the Add Web Reference dialog. The steps to do this may not be obvious if you do not know what to look for.

                        • In the Solution Explorer, right-click on your project and select Add Service Reference…
                        • In the Add Service Reference dialog, click the Advanced button.

                        image

                        • In the Service Reference Settings dialog, click the Add Web Reference… button

                        image

                        • In the Add Web Reference dialog, put the URL of your service (that is, the URL for your SharePoint site plus /_layouts/WebServiceDemo/MyCustomWebService.asmx ) in the URL bar and press Enter. You should see a page similar to the one shown below in the browser area of the form.
                        • Set the Web reference name to DemoProxy and click the Add Reference button.

                        image

                        • You should see a few changes in the Solution Explorer. The important one is the addition of the Web References and DemoProxy nodes. This indicates your service proxy has been created.

                        image

                        • Inside Program.cs, replace the code with the following:
                        using System;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Text;
                        
                        namespace WebServiceClient
                        {
                            class Program
                            {
                                static void Main(string[] args)
                                {
                                    var proxy = new DemoProxy.MyCustomWebService();
                                    proxy.Url = "http://win7virtualbox/sites/demo/_layouts/WebServiceDemo/MyCustomWebService.asmx";
                                    proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        
                                    var response = proxy.GetSiteListCount();
                                    Console.WriteLine(response);
                                }
                            }
                        }

                        An explanation of the code is needed.

                        • The first line creates an instance of the service proxy class that was generated when we added the web reference.
                        • The second line sets the SharePoint context. I can call this service from the context of different SharePoint sites so it is this URL which determines the site name and list count that will be returned to me. As an example, the first of the following URLs calls the service from the Demo site while the latter calls it from the Sales site
                          • http://win7virtualbox/sites/demo/_layouts/WebServiceDemo/MyCustomWebService.asmx
                          • http://win7virtualbox/sites/sales/_layouts/WebServiceDemo/MyCustomWebService.asmx
                        • The third line sets the credentials of the caller. In this case it uses the credentials of the currently logged in Windows user.
                        • The fourth line calls the GetSiteListCount method and the last line writes out the response.

                        The final step is to test the client application.

                        • Press F5 to run the client application. You should see results similar to the following

                        image

                         

                         

                         

                        评论
                        添加红包

                        请填写红包祝福语或标题

                        红包个数最小为10个

                        红包金额最低5元

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

                        抵扣说明:

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

                        余额充值