none
sharepoint CSOM 403 forbidden error in C# RRS feed

  • Question

  • Hello everyone,

    I am trying to add a list item into a SharePoint 2013 using CSOM in C#. I have also added "OfficeDevPnP.Core" & "SharePoint PnP Core library for SharePoint Online" references and to make a connection, I have used service reference of url. 

    here is the code 

    string sharePointSiteUrl = "site_url/";

    ClientContext clc = new ClientContext(sharePointSiteUrl);

     List oList = clc.Web.Lists.GetByTitle("List");
                        ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
                        ListItem oListItem = oList.AddItem(listCreationInformation);
                        oListItem["Title"] = "Add item in SharePoint List using CSOM";
                        oListItem.Update();
                        clc.ExecuteQuery();  //403 forbidden error

    Now I am getting the above exception. Can any one please help resolve this issue. 

    Friday, April 28, 2017 6:46 AM

All replies

  • Hi Aziz,

    Since 403 Forbidden is mostly encountered when the user context that you are using does not has permissions on SharePoint site.

    Please check the following:

    1. Are you using any proxy to connect to the SharePoint site?
    2. I don't see a authentication in the client context? Which authentication mode are you using?
    3. The user under which the current code is executing, does he has permission to add items in list in question?

    Try :

    clientcontext.credentials = credentialcache.defaultcredentials, if the user has permissions on SharePoint site

    --Rati


    Rati Ranjan

    Friday, April 28, 2017 7:43 AM
  • Hi,

    If you’re accessing SharePoint on-premise, you could try with below code:

    using (var clientContext = new ClientContext("http://sp/"))
                {
                    clientContext.Credentials = new NetworkCredential("contoso\\lee", "PW");
                    var peopleManager = new PeopleManager(clientContext);
                    var properties = peopleManager.GetMyProperties();
                    clientContext.Load(properties);
                    clientContext.ExecuteQuery();
    
                    if (properties.UserProfileProperties.ContainsKey("SPS-Locale"))
                    {
                        Console.WriteLine(properties.UserProfileProperties["SPS-Locale"]);
                    }
    
                    Console.ReadKey();
    
                }

    If you’re accessing SharePoint online, try with below code:

    using (ClientContext clientContext = new ClientContext("https://yoursite.sharepoint.com/"))
    {
        SecureString passWord = new SecureString();
    
        foreach (char c in "yourpassword".ToCharArray()) passWord.AppendChar(c);
    
        clientContext.Credentials = new SharePointOnlineCredentials("loginname@yoursite.onmicrosoft.com", passWord);
    
        Web web = clientContext.Web;
    
        clientContext.Load(web);
    
        clientContext.ExecuteQuery();
    
        Console.WriteLine(web.Title);
    
        Console.ReadLine();
    } 

    Best Regards,

    Lee


    Monday, May 1, 2017 3:55 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    You can also share your own solution here and mark it as answer, and we can learn from each other.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, May 15, 2017 12:02 PM