首页 > 解决方案 > 读取列表:远程服务器返回错误:(401)未授权

问题描述

我正在尝试使用 ac# app 在线阅读项目中的列表所以这里是我的代码:

ClientContext clientContext = new ClientContext(client.activeProfileUrl.ToString());
List announcementsList = clientContext.Web.Lists.GetByTitle("GetUrl");
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items = announcementsList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem listItem in items)
{
    logUtil.writeLine("Title: " + listItem["Title"]);
}

所以我的 client.activeprofile 返回我的用户连接的环境的 url(它运行良好)但是当我到达线路时:clientContext.ExecuteQuery();

我收到错误 401

知道我应该做什么吗?

谢谢

标签: c#listsharepoint

解决方案


I think you must set credentials for ClientContext. Please see this link: https://www.c-sharpcorner.com/article/connect-to-sharepoint-2013-online-using-csom-with-console-ap/
I think it will be what you want.

clientContext.Credentials = new SharePointOnlineCredentials(userName,password);

推荐阅读