1
votes

I'm working on an SSIS script task that uploads a file to a SharePoint site and applies item level permission to the file. I use a windows account to establish a connection with SharePoint library like below.

using (ClientContext ctx = new ClientContext("http://server"))
{
    ctx.Credentials = new NetworkCredential("username", "password", "domain");
    //ctx.Credentials = CredentialCache.DefaultCredentials;

    Web currentWeb = ctx.Web;

    ctx.Load(currentWeb);

    ctx.ExecuteQuery();

    using (FileStream fs = new FileStream(@"filepath", FileMode.Open))
    {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "site path to save", fs, true);
    }
}

The account has administrator permission to the SharePoint site, so it makes a connection to it perfectly and the file gets uploaded without any issues. Now there is another account, let's call it Account B, which has full administrator access at both site and site collection levels. But still I get an error 'The remote server returned an error : 401 Unauthorized' when I try executing the above code with this account B. Is there any other permission that is required to establish a connection to the SharePoint library/site using client object model?

1

1 Answers

1
votes

I think you need to uncomment the following commented line.

//ctx.Credentials = CredentialCache.DefaultCredentials;

This statement

ctx.Credentials = CredentialCache.DefaultCredentials;

passes on the security context of the credentials (that you are setting in the previous line) to the web-site that you are trying to interact with.