0

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();
Sivakumar Piratheeban
  • 493
  • 4
  • 11
  • 39

2 Answers2

6

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();
}
Mark
  • 3,273
  • 2
  • 36
  • 54
  • The type or namespace name 'SharePointOnlineCredentials' could not be found (are you missing a using directive or an assembly reference?) – Sivakumar Piratheeban Aug 22 '14 at 11:54
  • 1
    using Microsoft.SharePoint.Client; which version of the SharePoint dll's are you using ? SharePoint Online is always Version 16 you can use for it Version 15 and 16. But SharePoint Online is SharePoint 2013 and not 2010 like you tagged this question – Mark Aug 22 '14 at 12:07
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;
ebooyens
  • 608
  • 3
  • 9
  • 21