使用客户端对象模型在线访问SharePoint - 禁止错误

时间:2014-08-22 11:03:10

标签: c# sharepoint-2010 sharepoint-online sharepoint-clientobject

我尝试使用客户端对象模型创建新的列表项。我已经创建了一个asp.net应用程序来完成任务。如果我传递安装在我的计算机中的SharePoint服务器的URL,它就可以工作。 但是,如果我提供我的SharePoint在线URL,它将无法正常工作,如下面的代码所示。我得到“远程服务器返回错误:(403)禁止。”错误。 有什么想法吗?

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();

2 个答案:

答案 0 :(得分:6)

如果您尝试从SharePoint Online获取Context对象,则必须放在右侧Credentials,对于SharePoint Online,您应该使用SharePointOnlineCredentials Class

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

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();
}

答案 1 :(得分:0)

我想你只需提供你的登录凭据就可以了:

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

您需要包含System.Net:

using System.Net;
相关问题