SharePoint CSOM Upload File Access Denied Error

Using a .Net Web App, a simple process is to create a local file and upload it to SharePoint. I thought the service account I was using had ample permissions to the Site, but it didn’t… For testing, I granted the service account Full Control at the Web App level (user policy), site collection admin, and more. Nothing worked.

Sample of the code I was using:

ClientContext ctxH = new ClientContext(hURL); 
Web siteH = ctxH.Web; 
ctxH.Load(siteH); 
ctxH.ExecuteQuery(); 

List _library = siteH.Lists.GetByTitle("Drop Off Library"); 
Folder _oFolder = siteH.GetFolderByServerRelativeUrl(siteH.ServerRelativeUrl.TrimEnd('/') + "/" + "DropOffLibrary"); 
ctxH.Load(_oFolder); 
ctxH.ExecuteQuery(); 

FileStream fileStream = System.IO.File.OpenRead(fileName); FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.ContentStream = fileStream; fileInfo.Overwrite = true; fileInfo.Url = destFileName; Microsoft.SharePoint.Client.File _oFile = _oFolder.Files.Add(fileInfo); 

ctxHub.Load(_oFile); 
ctxHub.ExecuteQuery();

Leave a Reply

Your email address will not be published. Required fields are marked *