自定制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即可调用。我遇到的问题是有时候修改类库中的方法再重新部署时不更新……还在研究中
源码
先用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即可调用。我遇到的问题是有时候修改类库中的方法再重新部署时不更新……还在研究中
源码
相关文章推荐
- SharePoint Open Web Services
- jQuery Call web services(SOAP) in SharePoint 2013
- Using SharePoint 2003 Web Services to Retrieve Data From A List
- SharePoint Web Service系列:编写自定义SharePoint Web Services之二
- WSSv3 Technical Articles_使用ASP.NET 2.0 Web Parts和Windows SharePoint Services 3.0
- 浏览 Windows SharePoint Services Web 站点时收到“Service Unavailable”(
- 如何在Windows SharePoint Services中创建Web Parts(二)
- 浏览 Windows SharePoint Services Web 站点时收到“Service Unavailable”(服务不可用)错误信息的解决办法
- MOSS2007基础:Windows SharePoint Services 3.0中的Web Part
- SharePoint Web Service系列:编写自定义SharePoint Web Services之一
- How to use sharepoint webservices in web application
- FW: Deploy reporting services web parts (RSWebParts) to SharePoint 2010
- javascript + sharepoint webservices保存含有HTML字符的内容(富文本编辑框)
- SharePoint Web Services入门
- SharePoint Web Service系列:编写自定义SharePoint Web Services之二
- 浏览 Windows SharePoint Services Web 站点时收到“Service Unavailable”(服务不可用)错误信息
- SharePoint Web Services入门(转)_
- SharePoint Web Services入门
- 使用Visual Studio 2005扩展创建Windows SharePoint Services 3.0 Web Part
- WSSDAL (WSS Data Access Layer) – a SharePoint Web Services alternative