SharePoint 2013 Document Library by using OpenBinaryDirect method I got remote server 401 unauthorized error?

依然范特西╮ 提交于 2019-12-13 08:38:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!