Uploading files to SharePoint without use of API

90 Views Asked by At

I'm trying to build a script for uploading a single file to my SharePoint storage. I'm also limited of use of official API provided by Microsoft as on my work account I'm unable to register an app.

My initial idea was to use a shareplum python module which supposedly would accept my user credentials to access the SharePoint server. This however didn't work as the script failed to reach login page. (Not entirely sure why. Supposedly due to company firewall. Will try to investigate yet.)

This led to other idea. Maybe it is possible to record particular HTTP communication to avoid using API directly and make a script that could be reused with uploaded file path as the only inputted variable?

If anyone considers this manageable, shall I use Wireshark or Postman for this? Also would highly appreciate any detailed step up guide, as I'm poorly experienced in this area.

Thanks

P.S.
Uploaded files are small HTML files for test resports.

1

There are 1 best solutions below

0
RaytheonXie-MSFT On

You can upload files to sharepoint by csom. Please refer to following code

public static void UploadFile(ClientContext context,string uploadFolderUrl, string uploadFilePath)  
{  
    var fileCreationInfo = new FileCreationInformation  
    {  
            Content = System.IO.File.ReadAllBytes(uploadFilePath),  
            Overwrite = true,  
            Url = Path.GetFileName(uploadFilePath)  
    };  
    var targetFolder = context.Web.GetFolderByServerRelativeUrl(uploadFolderUrl);  
    var uploadFile = targetFolder.Files.Add(fileCreationInfo);  
    context.Load(uploadFile);  
    context.ExecuteQuery();  
}  
  
using (var ctx = new ClientContext(webUri))  
{  
     ctx.Credentials = credentials;  
  
     UploadFile(ctx,"LibName/FolderName/Sub Folder Name/Sub Sub Folder Name/Sub Sub Sub Folder Name",filePath);     
}