[英]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.