使用客户端对象模型在线访问SharePoint-禁止的错误 [英] Access SharePoint online using client object model- Forbidden error
问题描述
我尝试使用客户端对象模型创建新的列表项.我创建了一个asp.net应用程序来执行此任务.如果我传递了安装在计算机中的SharePoint服务器的URL,则它可以工作. 但是,如果我提供我的SharePoint在线URL,它将无法正常工作,如下面的代码所示.我收到远程服务器返回错误:(403)禁止."错误. 有什么主意吗?
I tried to Create a new list item using client object model. I have created an asp.net application to do the task. It works if I pass the URL of SharePoint server which is installed in my machine. But if I give my SharePoint online URL it is not working as below code shows. I get "The remote server returned an error: (403) Forbidden. " error. Any idea?
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();
推荐答案
如果尝试从SharePoint Online获取上下文对象,则必须放入正确的Credentials
,对于SharePoint Online,则应使用 SharePointOnlineCredentials类
if you are trying to get a Context object from SharePoint Online you have to put in the right Credentials
, as for SharePoint Online you should use the SharePointOnlineCredentials Class
可能的身份验证方法如下所示:
A possible Authentication Method can be look like this:
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();
}
这篇关于使用客户端对象模型在线访问SharePoint-禁止的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!