curl upload to sharepoint

Broeckling,Corey 1 Reputation point
2022-12-09T15:42:41.733+00:00

i am at a university which has an office 365 license. i am using sharepoint to store project data, and would like to use curl from the command line to post files to the sharepoint site programmatically. i have seen some general guidance on this from various online sources, but seem am receiving a 403 error. Presumably my sharepoint site is declining access. Is there a tip for getting curl to work in saving files to sharepoint?

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-12-12T02:05:44.09+00:00

    Hi @Broeckling,Corey
    403 error usually caused by your permissions to the folder haven’t replicated correctly on the server. Please check if you have the correct permission to the folder you want to upload files by following steps.
    https://sharepointmaven.com/2-ways-see-users-access-sharepoint/

    After check the permission you can upload a file by following code

     string userName = "******@xxxx.onmicrosoft.com";  
     string password = "xxx";  
     var securePassword = new SecureString();  
     foreach (char c in password)  
     {  
         securePassword.AppendChar(c);  
     }  
     using (var clientContext = new ClientContext("https://xxx.sharepoint.com/sites/test"))  
     {  
         clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);  
         Web web = clientContext.Web;  
         clientContext.Load(web, a => a.ServerRelativeUrl);  
         clientContext.ExecuteQuery();  
         List documentsList = clientContext.Web.Lists.GetByTitle("Contact");  
          
         var fileCreationInformation = new FileCreationInformation();  
         //Assign to content byte[] i.e. documentStream  
          
         fileCreationInformation.Content = System.IO.File.ReadAllBytes(@"D:\document.pdf");  
         //Allow owerwrite of document  
          
         fileCreationInformation.Overwrite = true;  
         //Upload URL  
          
         fileCreationInformation.Url = "https://testlz.sharepoint.com/sites/jerrydev/" + "Contact/demo" + "/document.pdf";  
          
         Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);  
          
         //Update the metadata for a field having name "DocType"  
         uploadFile.ListItemAllFields["Title"] = "UploadedviaCSOM";  
          
         uploadFile.ListItemAllFields.Update();  
         clientContext.ExecuteQuery();  
          
     }   
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.