问题
When I am retrieving the file from SharePoint 2013 Document Library by using OpenBinaryDirect method I got remote server 401 unauthorized error? Could you please help me how to rectify the problum
回答1:
Provide online-credentials by using SharePointOnlineCredentials(username,password) class in your code
Code:
string username="xxx";
string password="xx";
SecureString ss=new SecureString();
foreach(char c in password) ss.AppendChar(c)
clientcontext.credentials = SharePointOnlineCredentials(username,ss);
Web web = clientContext.Web;
List 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 (Microsoft.SharePoint.Client.ListItem listitem in items_attachments) {
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();
}
}
来源:https://stackoverflow.com/questions/32069686/sharepoint-2013-document-library-by-using-openbinarydirect-method-i-got-remote-s