SharePoint Web Service系列:编写自定义SharePoint Web Services之二

下面,我们要将我们的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 " NullAttachment " ;
}
try
{
SPWebsite
= SPControl.GetContextWeb(Context);
SPFolderfolder
= site.GetFolder(pathFolder);
string fileUrl = fileName;
SPFilefile
= folder.Files.Add(fileUrl,fileContents);
return file.TimeCreated.ToLongDateString() + " :: " + file.Title;
}
catch (System.Exceptionee)
{
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.UploadFilesvcDocLib = new WSSServer.UploadFile();
svcDocLib.Credentials
= CredentialCache.DefaultCredentials;

string strPath = textBox1.Text;
string strFile = strPath.Substring(strPath.LastIndexOf( " \\ " ) + 1 );
string strDestination = textBox2.Text;

FileStreamfStream
= 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;

编译运行。
(结束)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值