[英]Add item to SharePoint Online list c#
我有一个 web api,它的主体中有一些数据,我想使用 c# 添加到 SharePoint 在线列表中的数据。 但是下面的代码给了我未认证的错误。
using (var context = new ClientContext(siteUrl))
{
context.ExecutingWebRequest += Context_ExecutingWebRequest; // for oAuth it working in get list dat
Web web = context.Web;
List topicsList = context.Web.Lists.GetByTitle("ListName");
ListItemCreationInformation newTopicInfo = new ListItemCreationInformation();
ListItem oListItem = topicsList.AddItem(newTopicInfo);
oListItem["Title"] = "Test";
oListItem["Column1"] = "Test1";
oListItem.Update();
context.ExecuteQuery();
}
对于 SharePoint Online,使用 SharePointOnlinCredentials 类将凭据传递给身份验证:
string password = "*******";
string account = "username@tenant.onmicrosoft.com";
var secret = new SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
using (ClientContext ctx = new ClientContext("https://tenant.sharepoint.com/sites/dev/"))
{
ctx.Credentials = new SharePointOnlineCredentials(account, secret);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
List topicsList = ctx.Web.Lists.GetByTitle("ListName");
ListItemCreationInformation oListItemCreationInformation = new ListItemCreationInformation();
ListItem oListItem = topicsList.AddItem(oListItemCreationInformation);
oListItem ["Title"] = "New List Item";
oListItem["Column1"] = "Test1";
oListItem .Update();
ctx.ExecuteQuery();
};
上面的代码现在工作得很好,我缺少对我的应用程序的权限。 为了使用 oAuth,我们必须在 SharePoint 中注册一个加载项,并且要在 sharepoint 中发布数据,我必须将 FullControl 赋予我的加载项,但在创建时我已将其设为只读。 下面是参考。 参考资料
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.