我尝试使用客户端对象模型创建一个新的列表项。我已经创建了一个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();
如果你想从SharePointOnline获得一个上下文对象,你必须放入正确的Credentials
,对于SharePointOnline,你应该使用sharepointonlinecredals类
一种可能的身份验证方法如下所示:
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();
}
我想您只需要提供您的登录凭据,它应该工作:
clientContext.Credentials = new NetworkCredential("Username", "Password", "Domain");
你需要包括System。净:
using System.Net;