0

I'm using PowerShell v 5.1 How can I upload simple text file from my local machine to the corporate SharePoint? SharePoint URL when I'm on the needed web page looks like below:

https://mycompany.sharepoint.com/sites/BlaBlaBla/AnotherBlaBlaBla/How%20To/Upload/FileToThisUrl.aspx?newTargetListUrl=%2blabla%2MyCompany%2Want%2FMe%20ToDoviewpath=%2This%2Impossible%2Task%2Uploading%20This%2FDocuments%2FTo%2SharePoint&id=%2FAnd%2FIHaveNoIdea%2How%2FTo%20Do%2FThis%20Job%20Reports

I've tried this script (suggested somewhere else don't remember where) but it throws an 403 error, and my credentials are correct I know it:

$credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

Function UploadToSharePoint{

$destination = "https://mycompany.sharepoint.com/sites/BlaBlaBla/AnotherBlaBlaBla/How%20To/Upload/FileToThisUrl.aspx?newTargetListUrl=%2blabla%2MyCompany%2Want%2FMe%20ToDoviewpath=%2This%2Impossible%2Task%2Uploading%20This%2FDocuments%2FTo%2SharePoint&id=%2FAnd%2FIHaveNoIdea%2How%2FTo%20Do%2FThis%20Job%20Reports"
$File =Get-ChildItem  "C:\MyNameShouldBeHere\Work\PowerShell\UploadFileTest.txt"

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination + "/" + $File.Name, "PUT", $File.FullName)

According to this microsoft documentation page there is no indication on HOW TO SIMPLY UPLOAD A FILE TO THE SHAREPOINT. They got bunch of other methods to do there, but not how to upload a file. Hence there is this information listed:

Working with large files by using REST When you need to upload a binary file that is larger than 1.5 megabytes (MB), the REST interface is your only option. For a code example that shows you how to upload a binary file that is smaller than 1.5 MB by using the SharePoint JavaScript object model, see Complete basic operations using JavaScript library code in SharePoint. The maximum size of a binary file that you can create with REST is 2 gigabytes (GB).

The following example shows how to create a large binary file.

Warning

This approach works only with Internet Explorer 10 and the latest versions of other browsers.

Copy url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/Add(url='file name', overwrite=true) method: POST body: contents of binary file headers: Authorization: "Bearer " + accessToken X-RequestDigest: form digest value content-type: "application/json;odata=verbose" content-length:length of post body

My file is not that big, so not sure if I need to use this method? And what the h.. in the world Internet Explorer means in this context? Can you only upload the file using IE? What?

2 questions that arise in my head:

  1. Do I need to get authorization token in order to use REST API to upload the file
  2. Where in Sharepoint UI can I find this feature? Under My account? I've searched and didn't find anything that reminds this feature.
  3. Was someone able to upload a file using PowerShell into the Sharepoint web site?
1

I personally use the PnP module for working with SharePoint via PowerShell.

https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

More specifically, the Add-PnPFile cmdlet.

https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnpfile?view=sharepoint-ps

Connect-PnPOnline -url $siteURL -UseWebLogin
Add-PnPFile -Path $localPathToFile -Folder $docLibName -Values @{Modified="1/1/2016", "Custom_x0020_Column"="Custom Value"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.