0

我尝试使用客户端对象模型创建一个新的列表项。我创建了一个 asp.net 应用程序来完成这项任务。如果我传递安装在我的机器上的 SharePoint 服务器的 URL,它就可以工作。但是,如果我提供我的 SharePoint 在线 URL,它就不会像下面的代码所示那样工作。我收到“远程服务器返回错误:(403) Forbidden。”错误。任何想法?

ClientContext context = new ClientContext("https://xxx.sharepoint.com/SitePages/");
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = result.City;
newItem["Body"] = result.State;
newItem.Update();
context.ExecuteQuery();
4

2 回答 2

6

如果您尝试从 SharePoint Online 获取 Context 对象,则必须输入正确的Credentials内容,对于 SharePoint Online,您应该使用SharePointOnlineCredentials 类

可能的身份验证方法如下所示:

private void AutheticateO365(string url, string password, string userName)
{
   Context = new ClientContext(url);
   var passWord = new SecureString();
   foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
   Context.Credentials = new SharePointOnlineCredentials(userName, passWord);
   var web = Context.Web;
   Context.Load(web);
   Context.ExecuteQuery();
}
于 2014-08-22T11:10:39.377 回答
0

I would imagine you just have to supply your login credentials and it should work:

clientContext.Credentials = new NetworkCredential("Username", "Password", "Domain");

You'll need to including System.Net:

using System.Net;
于 2014-08-22T11:13:08.443 回答