Forum Discussion

amir asyraf's avatar
amir asyraf
Copper Contributor
Aug 18, 2017
Solved

CSOM : Server Unauthorized Access Exception: Access Denied.

i created window agent for the end user. where the data will send to the Data list Sharepoint. The problem is, when i try to use to the window agent to the local company user account, it shown the problem that i state on the title. But when i try to use my local company user account, it succcessfully sent.

My theory: you need to have permission at sharepoint site using your local account and also in window current user, you need to use the same account at the sharepoint. Means, your local user account in windows and sharepoint is needed to send to datalist, and you cannot use other window account to send to datalist.

 

any solution of this ?

 

  string username = "USERNAME";
            string password = "PASSWORD";
            string domain = "DOMAIN;

            System.Net.NetworkCredential _myCredentials = new System.Net.NetworkCredential(username, password, domain);

            string siteUrl = "MYSITE";

            ClientContext clientContext = new ClientContext(siteUrl);
            SP.List oList = clientContext.Web.Lists.GetByTitle("Test_List");

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);

            //CUSTODIAN DETAIL
            oListItem["Title"] = "Test";


            oListItem.Update();

            clientContext.ExecuteQuery();

            Console.WriteLine("Send Sucessfully!");
            Console.ReadLine();
  • My suggestion, you're using SharePoint On-Premises.

    For On-Premises credentials should be defined in the following way:

     

    using System.Net;
    using Microsoft.SharePoint.Client;
    
    using (ClientContext context = new ClientContext("http://sp-onprem/")) {
    
        context.Credentials = new NetworkCredential("user", "password", "domain");
    	
        // CSOM code
    
    }

     

    SharePoint Online requires the secure string as a password:

    using (ClientContext context = new ClientContext("https://contoso.sharepoint.com/")) {
    
        string pwd = "password";
        SecureString password = new SecureString();
        foreach (char c in pwd.ToCharArray()) 
        {
            password.AppendChar(c);
        }
    	
        context.Credentials = new SharePointOnlineCredentials("user", password);
    	
        // CSOM code
    
    }

    Also, I would recommend to figure out what auth scenario your instance is configured to support.

     

     

11 Replies

  • My suggestion, you're using SharePoint On-Premises.

    For On-Premises credentials should be defined in the following way:

     

    using System.Net;
    using Microsoft.SharePoint.Client;
    
    using (ClientContext context = new ClientContext("http://sp-onprem/")) {
    
        context.Credentials = new NetworkCredential("user", "password", "domain");
    	
        // CSOM code
    
    }

     

    SharePoint Online requires the secure string as a password:

    using (ClientContext context = new ClientContext("https://contoso.sharepoint.com/")) {
    
        string pwd = "password";
        SecureString password = new SecureString();
        foreach (char c in pwd.ToCharArray()) 
        {
            password.AppendChar(c);
        }
    	
        context.Credentials = new SharePointOnlineCredentials("user", password);
    	
        // CSOM code
    
    }

    Also, I would recommend to figure out what auth scenario your instance is configured to support.

     

     

    • amir asyraf's avatar
      amir asyraf
      Copper Contributor

      This is what i do before, but the error show (access denied). Actually this error is shown for the local user that don't have the authorization to access the sharepoint subsite.

      • Oh, so your user account has no permissions?

        You should use an account with permissions corresponding to operations which should be done with in CSOM for sure.

  • Hi it is because you do not use the credentials in your clientcontext...

    please take a look here http://www.c-sharpcorner.com/blogs/office365-how-to-connect-sharepoint-online-site-using-csom

     

      • Deleted's avatar
        Deleted

        First check if you are connected. then if connected try this : http://www.c-sharpcorner.com/code/2635/create-a-list-item-in-sharepoint-2013-using-csom-in-visual-studio.aspx

Resources