This programming task provides an overview of how to create a custom Web service that operates within the context of Microsoft Windows SharePoint Services 3.0. The task steps through the process of creating a simple "Hello World" Web service, and then shows how to modify the service so that it implements the Windows SharePoint Services 3.0 server-side object model to return site and list data.
To provide discovery and description for your custom Web service, you must create a .disco and a .wsdl file. Because Windows SharePoint Services virtualizes its URLs (for example, http://MyServer/MySite/MySubsite becomes http://MyServer), you cannot use the autogenerated .disco and .wsdl files generated by ASP.NET. Instead, you must create a .disco page and a .wsdl aspx page that provide the necessary redirection and maintain virtualization.
You can use ASP.NET to generate the .disco and .wsdl files by temporarily hosting your Web service in a virtual directory, such as /_layouts, and then using the .NET Framework Web Service Discovery tool (Disco.exe) to obtain the generated files.
To generate the static discovery and WSDL files
-
In Windows Explorer, copy the .asmx file of your Web service to \\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS.
-
Run Disco.exe at the command prompt from the LAYOUTS directory to generate .disco and .wsdl files. Run a command in the following format to generate the files in \LAYOUTS:
disco http://MyServer/_layouts/MyCustomWebService.asmx
-
To register namespaces of the Windows SharePoint Services object model, open both the .disco and .wsdl files and replace the opening XML processing instruction -- <?xml version="1.0" encoding="utf-8"?> -- with instructions such as the following:
<%@ Page Language="C#" Inherits="System.Web.UI.Page"%> <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint.Utilities" %> <%@ Import Namespace="Microsoft.SharePoint" %> <% Response.ContentType = "text/xml"; %>
-
In the .disco file, modify the contract reference and SOAP address tags to be like the following example, which replaces literal paths with code generated paths through use of the Microsoft.SharePoint.Utilities.SPEncode class, and which replaces the method name that is specified in the binding attribute:
<contractRef ref=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request) + "?wsdl", '"'); %> docRef=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> xmlns="http://schemas.xmlsoap.org/disco/scl/" /> <soap address=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> xmlns:q1="http://tempuri.org/" binding="q1:HelloWorld" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
-
In the .wsdl file, make the following, similar substitution for the SOAP address that is specified:
<soap:address location=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> />
-
Rename both files in the respective formats MyCustomWebServicedisco.aspx and MyCustomWebServicewsdl.aspx so that your service is discoverable through Windows SharePoint Services.
After copying the Web services files to the _vti_bin directory, the next step is to create a Windows Application to consume the Web service.
To create a Windows Application that consumes the Web service
-
Open Visual Studio 2005, and on the File menu, point to New, and then click Project.
-
In the New Project dialog box, select Visual C# or Visual Basic, and then select the Windows Application template.
-
Type a name for the application in the Name box, specify a location for the project files in the Location box, and then click OK.
-
In Solution Explorer, right-click References, and then click Add Web Reference.
-
In the address bar of the Add Web Reference browser, type the URL for the site to which to apply the service, as follows, and then press ENTER:
http:// Server_Name /[sites/][ Site_Name /]_vti_bin/MyCustomWebService.asmx
-
Click Add Reference to download the service contract for the Web service.
-
Open Form1 in Design view, display the Toolbox, and then drag a button onto the form.
-
Double-click the Button1 control on Form1 to display the code-behind file in the code editor, and add the following code that calls your custom method.
Visual BasicDim MyCustomService As New Web_Reference_Folder.MyServiceClass() MyCustomService.UseDefaultCredentials = True
-
MessageBox.Show(MyCustomService.HelloWorld())
C#Web_Reference_Folder.MyServiceClass MyCustomService = new Web_Reference_Folder.MyServiceClass(); MyCustomService.UseDefaultCredentials = true; MessageBox.Show(MyCustomService.HelloWorld());
-
Press F5 to compile and run the project and see a message box that displays "Hello World".
Now you are ready to try out types and members of the wssnoversshort object model in your Web service.
To implement the Windows SharePoint Services object model
-
In your project Service.cs file, you must import the appropriate namespaces in the object model. For example, to use types and members of the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces, add the following directives:
Visual BasicImports Microsoft.SharePoint Imports Microsoft.SharePoint.Utilities
C#using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities;
-
Rename the method in Service.cs. For example, change the name from HelloWorld to GetSiteListCount.
-
Add the following code to display the name of the Web site and the number of lists that it contains:
Visual BasiDim myWebSite As SPWeb = SPContext.Current.Web Dim lists As SPListCollection = myWebSite.Lists Return myWebSite.Title + " contains " + lists.Count.ToString() + " Web sites." SPWeb myWebSite = SPContext.Current.Web; SPListCollection lists = myWebSite.Lists; return (myWebSite.Title + " contains " + lists.Count.ToString() + " Web sites.");
Note: |
---|
If your code modifies Windows SharePoint Services data in some way, you may need to allow unsafe updates on the Web site, which you can do by setting the AllowUnsafeUpdates property. |
-
To rebuild the class library DLL and copy it to the GAC, repeat steps 9 and 10 from the previous section, "To create an ASP.NET Web service".
-
Reset Internet Information Services (IIS) for changes in the DLL to take effect.
-
To generate new versions of the .disco and .wsdl files, repeat the previous procedure, "To generate the static discovery and WSDL files," but change the method binding name as appropriate, for example, to GetSiteListCount.
-
Copy the new versions of the MyCustomWebServicedisco.aspx and MyCustomWebServicewsdl.aspx files to the ISAPI folder.
-
Open the Windows application your created previously, delete the previous Web service reference for the Hello World example, and add a new Web reference to the revised Web service.
-
Press F5 to compile and run the project and see a message box that displays the Web site name and the number of lists that it contains.
?
1、什么时候需要重新生成 wsdl文件?
2、自定义页面或自定义用户控件时是否可以借用这种方式把后台代码放到 GAC ,然后在页面上修改引用的路径等信息?