当我使用OpenBinaryDirect方法从SharePoint 2013文档库检索文件时,我得到了远程服务器401未经授权的错误?你能帮我解决一下如何纠正问题
答案 0 :(得分:0)
使用提供在线凭证 代码中的SharePointOnlineCredentials(用户名,密码)类
代码:
string username =“xxx”;
string password =“xx”;
SecureString ss = new SecureString();
foreach(密码中的字符) ss.AppendChar(c)中
clientcontext.credentials = SharePointOnlineCredentials(username,ss);
Web web = clientContext.Web;
列表list = web.Lists.GetById(new Guid(“xxxxxxxxxx”));
var data = new CamlQuery(){ViewXml =“query”};
Microsoft.SharePoint.Client.ListItemCollection items_attachments = list.GetItems(data);
clientContext.Load(items_attachments);
clientContext.ExecuteQuery();
foreach(items_attachments中的Microsoft.SharePoint.Client.ListItem listitem) {
clientContext.Load(listitem,i => i.File);
clientContext.ExecuteQuery();
var fileRef = listitem.File.ServerRelativeUrl;
FileInformation fileinfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext,fileRef);
Stream fl = fileinfo.Stream;
byte [] s = ReadFully(fl);
}
public static byte [] ReadFully(Stream input) {
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}