arame3333
arame3333

Reputation: 10193

Access denied. You do not have permission to perform this action or access this resource

I have a DEV environment where I develop my code and I use a local Sharepoint. I have a TEST environment where I deploy my application and use a Sharepoint on the TEST server. I am using Sharepoint 2010. I have a client object model that I am using to save a document in a sharepoint server. I am using the Microsoft.Sharepoint.Client library for the following code;

    public void Save(byte[] file, string url)
    {
        using (var context = new ClientContext(this.SharepointServer))
        {
            var list = context.Web.Lists.GetByTitle(this.DocumentLibrary);
            var fileCreationInformation = new FileCreationInformation
                                              {
                                                  Content = file,
                                                  Overwrite = true,
                                                  Url = url
                                              };
            var uploadFile = list.RootFolder.Files.Add(fileCreationInformation);
            uploadFile.ListItemAllFields.Update();
            context.ExecuteQuery();
        }
    }
}

When I run this code in my development environment, the document is saved on the Sharepoint as intended. When I deploy to the test environment when the document is saved to a different Sharepoint server, I get the following exception:

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.

So there is a permissions issue here.

However when I change my code on the development server to save the document to the Sharepoint on the Test server, it works. The same Sharepoint that would not save the document from my deployed application on Test. I am also logging in with forms authentication with the same user name and password.

So why might this happen and where should I look to fix it?

Upvotes: 2

Views: 18973

Answers (1)

arame3333
arame3333

Reputation: 10193

I found what I was missing

I needed to set the credentials for the context;

context.Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain);

Upvotes: 3

Related Questions