您的位置:首页 > 其它

自定制SharePoint WebServices取附件

2009-03-30 15:03 387 查看
SharePoint提供的WebServices中没有直接取到附件内容的,都是提供了附件的地址,还需要用WebClient带上身份取,那样可能会因为iis的一些配置被屏蔽掉。

先用ASP.NET Web 服务模板新建一个网站,将App_Code下的文件删除,然后再添加一个类库项目,cs文件代码如下:

using System;
2using System.Web;
3using System.Web.Services;
4using System.Web.Services.Protocols;
5using Microsoft.SharePoint;
6using Microsoft.SharePoint.Utilities;
7using System.IO;
8[WebService(Namespace = "http://tempuri.org/")]
9[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service()
{ }

[WebMethod]
public object[] GetAttachments(string listName, int itemID)
{
SPWeb myWeb = SPContext.Current.Web;
SPList myList = myWeb.Lists[listName];
SPListItem myItem = myList.GetItemById(itemID);
int attCount = myItem.Attachments.Count;
string[] myAttUrl = myItem.Attachments.UrlPrefix.Split('/');
string attachUrl = string.Empty;
for (int i = 3; i < myAttUrl.Length; i++)
{
attachUrl += "/" + myAttUrl[i];
}

object[] attachmentsObject = new object[attCount * 2];
for (int i = 0; i < attCount; i++)
{
attachmentsObject[i] = myItem.Attachments[i];
SPFile myFile = myWeb.GetFile(attachUrl + myItem.Attachments[i]);
attachmentsObject[attCount + i] = myFile.OpenBinary();
}
return attachmentsObject;
}
}
编译类库以后将dll文件拷贝到GAC中,将asmx文件内容改为<%@ WebService Language="C#" Class="Service, SPAttachments, Version=1.0.0.0, Culture=neutral, PublicKeyToken=890647b58f8f1295" %>

到这一步,WebServices应该可以在解决方案中调试运行了 。

使用VS .NET命令行工具生成disco 和 wsdl文件,命令如下:

Disco http://localhost:Port/Project_Name/Service1.asmx



端口 port是.NET调试自动分配的,然后到命令行工具目录下找到disco和wsdl文件

两个文件改成Attachmentsdisco.aspx和Attachmentswsdl.aspx复制到\12\ISAPI目录下,同时将Attachments.asmx页也复制到该目录,disco中代码可以直接复制该目录里其他类似的网页,aspx将头和 <wsdl:service name="Service">代码段改成类似的网页格式即可。然后在该目录下找到spdisco.aspx文件,在</discovery>前面添加如下两行:

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/Attachments.asmx?wsdl"),Response.Output); %> docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/Attachments.asmx"),Response.Output); %> xmlns="http://schemas.xmlsoap.org/disco/scl/" />

<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/Attachments.asmx?disco"),Response.Output); %> xmlns="http://schemas.xmlsoap.org/disco/" />

再重启IIS即可调用。我遇到的问题是有时候修改类库中的方法再重新部署时不更新……还在研究中

源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: