代码下载: ODC_WritingCustomWebServicesSampleSPPT.EXE.
我们可以基于WSS构建强大的项目。做为开发人员,我们有时需要创建基于WSS的自定义 Web服务来满足特定的开发要求。
注意:由于WSS增强的安全性,我们需要一些特殊的步骤来创建自定义Web服务。当我们使用VS.NET提供的ASP.NET Web service模板来创建我们的SharePoint Web service时,我们必须将Web service创建到单独的IIS网站中,而不能和WSS处于同一个IIS网站(不能使用相同的端口)。同时,我们还必须是运行WSS的机器上本地管理员组的成员。
下面列出了开发自定义SharePoint Web 服务的简要步骤:
1、如果我们的WSS位于默认网站(端口80),那么我们需要创建一个新的网站并配置不同的端口。新的网站作为开发用网站,而WSS所在的默认网站作为生产环境的部署网站。
2、在开发网站上创建一个Web Service项目
3、生成并修改静态发现(.disco)文件和 .wsdl文件,并保存成 .aspx页面。在页面内容顶部注册Microsoft.SharePoint命名空间。
4、修改.disco和.wsdl文件,使其支持服务虚拟化机制。
5、当我们开发完成Web service后,将相关的文件部署到WSS网站的_vti_bin和_vti_bin/bin虚拟目录下。
实例ODC_WritingCustomWebServicesSampleSPPT.EXE 提供了一个远程访问文档的Web服务示例。项目中实现了一些Microsoft.SharePoint命名空间中暴露出来的文档访问方法,如SPFile.CheckIn, SPFile.CheckOut, SPFile.UndoCheckOut, 和 SPFileCollection.Add。在没有这些自定义的Web service时,我们必须使用WebDAV或FrontPage RPC来实现类似的文档访问。
在WSS的服务器上安装该示例
下载ODC_WritingCustomWebServicesSampleSPPT.EXE并解压到WSS前端服务器的本地路径。Local_drive:/CreatingaCustomWebServiceSample 双击build.bat文件来执行编译和安装。如下文件被拷贝到Local_drive:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/60/isapi/ 目录:
SPFiles.asmx spfilesdisco.aspx spfileswsdl.aspx如下文件被拷贝到Local_drive:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/60/isapi/BIN/目录:
WSCheckOut.dll WSCheckOut.pdb编写一个自定义Web服务
1、 在开发用网站上使用VS.NET利用其提供的模板创建一个空的 ASP.Net Web Service项目,其中将包括一个Service1.asmx 文件。
2、 进入代码编辑,移除下面的行的注释后编译该项目。现在,我们可以将我们的服务部署到WSS网站中。
// [WebMethod] // public string HelloWorld() // { // return "Hello World"; // }3、
要想将自定义的Web服务绑到WSS上,VS .NET需要一些特定的基于.disco和.wsdl文件的.aspx文件。首先使用VS .NET命令行工具生成我们需要的.disco和.wsdl文件。
创建并编辑.disco和.wsdl文件
使用VS .NET命令行工具在当前目录中生成Service1.disco 和 Service1.wsdl,命令如下:
Disco http://server_name:New_Port/Project_Name/Service_1.asmx
打开Service1.disco文件并定位到下面的行:
<? xml version="1.0" encoding="utf-8" ?>将其替换成:
< %@ Page Language ="C#" Inherits ="System.Web.UI.Page" % > < %@ Assembly Name ="Microsoft.SharePoint, Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" % > < %@ Import Namespace ="Microsoft.SharePoint.Utilities" % > < %@ Import Namespace ="Microsoft.SharePoint" % > < % Response .ContentType = "text/xml" ; % >以 Service1disco.aspx保存该文件。
对Service1.wsdl也进行同样的修改并保存为Service1wsdl.aspx。
修改 Service1disco.aspx和Service1wsdl.aspx使其支持服务虚拟化机制
打开Service1disco.aspx文件并定位到下面的行:
< contractRef ref ="http://server_name:New_Port/Project_Name/Service1.asmx?wsdl" docRef ="http://server_name:New_Port/Project_Name/Service1.asmx" xmlns ="http://schemas.xmlsoap.org/disco/scl/" />对标签<contractRef>进行如下修改:
< 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 ="http://server_name:New_Port/Project_Name/Service1.asmx" xmlns:q1 ="http://tempuri.org/" binding ="q1:Service1Soap" xmlns ="http://schemas.xmlsoap.org/disco/soap/" />修改<soap address> 标签为:
< soap address =<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); % > xmlns:q1="http://tempuri.org/" binding="q1:Service1Soap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
保存所有更改。
打开Service1wsdl.aspx文件并定位到下面的行:
< soap:address location ="http://server_name:New_Port/Project_Name/Service1.asmx" />修改soap:address行为:
< soap:address location =<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); % > />保存所有更改。
将Web service相关文件拷贝到_vti_bin虚拟目录
拷贝Service1wsdl.aspx,Service1disco.aspx和Service1.asmx到_vti_bin虚拟目录。该目录是默认的Web服务存放目录。
拷贝对应的.dll文件到_vti_bin/bin虚拟目录。
注意:_vti_bin/bin虚拟目录映射到物理路径//Server_Name/Program Files/Common Files/Microsoft Shared/Web Server Extensions/ISAPI/bin
下面,我们要将我们的Web服务添加到WSS的Web服务列表中,这样就可以在VS.NET中添加该Web服务的引用了。
1、打开spdisco.aspx文件,该文件位于Local_Drive:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/60/ISAPI目录。
2、在文件末尾的discovery元素中添加下面的内容,保存。注意:soap元素的binding属性中在 "Soap"之前的文字(该例中的binding="q1:Service1Soap")指定了定义Web service时使用的类名。
< contractRef ref =<% SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/Service1.asmx?wsdl", '"'); % > docRef= < % SPEncode .WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/Service1.asmx", '"'); % > xmlns="http://schemas.xmlsoap.org/disco/scl/" /> < soap address =<% SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/Service1.asmx", '"'); % > xmlns:q1="http://schemas.microsoft.com/sharepoint/soap/directory/" binding="q1:Service1Soap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
至此,我们的自定义Web service就部署完成了。我们可以像使用默认的Web service一样来调用我们的自定义Web service了。
创建文档上载Web服务
我们可以使用上面的方法来建立文档上载Web服务来上载文档到一个WSS的共享文档文档库。该服务利用服务虚拟化来获取站点上下文,然后上载文档到指定的文档库。
创建一个Web service项目,名为UploadSvc。添加一个新的Web service类命名为UploadFile。
添加对Windows SharePoint Services (Microsoft.Sharepoint.dll)的引用。
在UploadFile类中添加下面的Web方法:
编译该Web service项目。
[WebMethod] public string UploadDocument( string fileName, byte [] fileContents, string pathFolder){ if ( fileContents == null ) { return " Null Attachment " ; } try { SPWeb site = SPControl.GetContextWeb(Context); SPFolder folder = site.GetFolder(pathFolder); string fileUrl = fileName; SPFile file = folder.Files.Add(fileUrl, fileContents); return file.TimeCreated.ToLongDateString() + " :: " + file.Title; } catch (System.Exception ee) { return ee.Message + " :: " + ee.Source; }}添加下面的命名空间引用:
using System.IO; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls;
创建并修改.disco和.wsdl并修改spdisco.aspx, 需要将上面的Service1 替换成UploadFile。分别保存为UploadFiledisco.aspx和UploadFilewsdl.aspx。
拷贝这些文件到_vti_bin, 拷贝对应得.dll到 _vti_bin/bin。
调用上载文件服务的例子
新建一个WinForm应用程序,添加Web引用,并将该引用命名为WSSServer。
添加一个button和两个textbox到默认的窗体,一个textbox用来输入上载文件的路径,另一个用来指定要上载到哪个文档库。如http://Server_Name/sites/Target_Site/Document_Library。
添加下面的代码到button的Click事件中。
WSSServer.UploadFile svcDocLib = new WSSServer.UploadFile();svcDocLib.Credentials = CredentialCache.DefaultCredentials; string strPath = textBox1.Text; string strFile = strPath.Substring(strPath.LastIndexOf( " // " ) + 1 ); string strDestination = textBox2.Text; FileStream fStream = new FileStream(strPath, System.IO.FileMode.Open); byte [] binFile = new byte [( int )fStream.Length];fStream.Read(binFile, 0 , ( int )fStream.Length);fStream.Close(); string str = svcDocLib.UploadDocument(strFile, binFile, strDestination);MessageBox.Show(str);添加命名空间引用:
using System.Net; using System.IO;编译运行。