标签:winform style blog http color io os ar for
Sharepoint2010中引入了客户端对象模型(COM) 来加强外部对sharepoint站点信息的访问(sharepoint2007只能通过web service)
SharePoint中有3种客户端对象模型:
3种客户端对象模型都通过Client.svc来实现与服务器的交互,对于COM在此不做详细的说明,本节的学习目标是:通过客户端对象模型上传附件
在sharepoint常用于存储附件的容器有:Library和List,在List中实现存储附件要先启动该功能。
using (var clientContext = new ClientContext(url)) { clientContext.Credentials = creds; using (var fs = new FileStream(fileName, FileMode.Open)) { var fi = new FileInfo(fileName); var list = clientContext.Web.Lists.GetByTitle(listTitle); clientContext.Load(list.RootFolder); clientContext.ExecuteQuery(); //上传附件到List string attachmentUrl = list.RootFolder.ServerRelativeUrl + "/Attachments/" + itemId.ToString() + "/" + fi.Name; //上传附件到Library string attachmentUrl = list.RootFolder.ServerRelativeUrl + "/" + fi.Name; Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, attachmentUrl, fs, true);
} }
备注:上面的代码上传附件到Library和List(已经存在/attachments/ItemId/ folder)没有任何问题,但是如果上传附件到一条新的List Item就会报409服务器拒绝的错误
原因是因为新的List Item并不存在/Attachments/ItemId 这样的folder,解决方法是通过web service 来实现附件的上传。
var soapClient = new ListsService.Lists(); soapClient.Credentials = creds; soapClient.Url = ctx.Web.Context.Url + "/_vti_bin/Lists.asmx"; return soapClient.AddAttachment(listName, itemId, Path.GetFileName(fileName),System.IO.File.ReadAllBytes(fileName));
总结:本节讲述了如何通过COM上传附件,同时也提供另外一种上传附件的方法(web service).
标签:winform style blog http color io os ar for
原文地址:http://www.cnblogs.com/splearning/p/4036637.html