SOLVED

SharePoint Online - Upload document using PowerShell / CSOM

%3CLINGO-SUB%20id%3D%22lingo-sub-42302%22%20slang%3D%22en-US%22%3ESharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42302%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20upload%20document%20to%20SharePoint%20Online%20using%20PowerShell%20%2F%20CSOM.%3CBR%20%2F%3EIssue%20is%20that%20I%20don't%20have%20file%20on%20local%20drive.%20I%20have%20URL%20of%20a%20file%20which%20I%20download%20as%3CBR%20%2F%3E%24wc%20%3D%20New-Object%20System.Net.WebClient%3CBR%20%2F%3E%24ByteArray%20%3D%20%24wc.downloaddata(Document%20URL)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24ByteArray%20is%20a%20Byte%20array%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20try%20to%20user%20following%20code%2C%20it%20generates%20error%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Specify%20tenant%20admin%20and%20site%20URL%20and%20including%20DLL%3C%2FP%3E%3CP%3E%24User%20%3D%20%22XXXXX%40XXXXXXX%22%3CBR%20%2F%3E%24SiteURL%20%3D%20%22XXXXXXXXXXX%22%3CBR%20%2F%3E%24DocLibName%20%3D%20%22Documents%22%3CBR%20%2F%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%3CBR%20%2F%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%3CBR%20%2F%3E%24Password%20%3D%20'XXXXXX'%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Bind%20to%20site%20collection%3C%2FP%3E%3CP%3E%24Context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteURL)%3CBR%20%2F%3E%24Creds%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24User%2C%24Password)%3CBR%20%2F%3E%24Context.Credentials%20%3D%20%24Creds%3CBR%20%2F%3ERetrieve%20list%3C%2FP%3E%3CP%3E%24List%20%3D%20%24Context.Web.Lists.GetByTitle(%24DocLibName)%3CBR%20%2F%3E%24Context.Load(%24List)%3CBR%20%2F%3E%24Context.ExecuteQuery()%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Upload%20File%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24Upload%20%3D%20%24List.RootFolder.Files.Add('abc.pdf'%2C%24ByteArray%2C%24true)%3C%2FSTRONG%3E%3CBR%20%2F%3E%24Context.Load(%24Upload)%3CBR%20%2F%3E%24Context.ExecuteQuery()%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAdd%20doesn't%20work%20and%20not%20sure%20how%20to%20upload%20a%20byte%20array.%20I%20have%20tried%20with%20different%20arguments.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EError%20is%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ECannot%20find%20an%20overload%20for%20%22Add%22%20and%20the%20argument%20count%3A%20%223%22.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-134907%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-134907%22%20slang%3D%22en-US%22%3EFor%20those%20looking%20help%20on%20uploading%20files%20via%20PowerShell.%20I%20was%20able%20to%20do%20that%20using%20the%20information%20from%20the%20link%20below%3A%20The%20approach%20reads%20files%20from%20a%20drive%20and%20uploads%20them%20to%20the%20site%20using%20Invoke-WebRequest.%20This%20is%20a%20different%20from%20the%20original%20question%2C%20but%20I%20hope%20it%20helps.%3CBR%20%2F%3E%3CBR%20%2F%3E%23upload%20the%20file%20to%20the%20server%20using%20default%20credentials%20from%20the%3CBR%20%2F%3E%23current%20session%3CBR%20%2F%3E%20%24result%20%3D%20Invoke-WebRequest%20-Uri%20%24url%20-InFile%20%24filePath%20-Method%20PUT%3CBR%20%2F%3E%20-UseDefaultCredentials%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22http%3A%2F%2Fwww.ozkary.com%2F2017%2F09%2Fpowershell-upload-multiple-files-sharepoint.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.ozkary.com%2F2017%2F09%2Fpowershell-upload-multiple-files-sharepoint.html%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42831%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42831%22%20slang%3D%22en-US%22%3EThanks.%20Casting%20worked.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42620%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42620%22%20slang%3D%22en-US%22%3EHave%20you%20tried%20to%20use%20PnP%20PowerShell%20commands%3F%20there's%20an%20AddFile%20command%20that%20can%20take%20a%20Stream%20as%20a%20parameter%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FAddPnPFile.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FAddPnPFile.md%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42606%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42606%22%20slang%3D%22en-US%22%3E%3CP%3EPlease%20check%20the%20below%20modified%20script%2C%20which%20generates%20the%20byte%20array%20from%20the%20given%20URL%20and%20upload%20it%20to%20the%20SPO.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24wc%20%3D%20New-Object%20System.Net.WebClient%0A%23%20update%20with%20correct%20path%20%0A%24ByteArray%20%3D%20%24wc.downloaddata(%22http%3A%2F%2Fdownloads.microsoft.com%2Fdownload%2Ffile.exe%22)%0A%0A%23Specify%20tenant%20admin%20and%20site%20URL%20and%20including%20DLL%0A%24User%20%3D%20%22XXXXX%40XXXXXXX%22%0A%24Password%20%3D%20'XXXXXX'%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%0A%24SiteURL%20%3D%20%22XXXXXXXXXXX%22%0A%24DocLibName%20%3D%20%22Documents%22%0AAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%0AAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C15%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%0A%0A%0A%23Bind%20to%20site%20collection%0A%24Context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteURL)%0A%24Creds%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24User%2C%24Password)%0A%24Context.Credentials%20%3D%20%24Creds%0A%0A%24List%20%3D%20%24Context.Web.Lists.GetByTitle(%24DocLibName)%0A%24Context.Load(%24List)%0A%24Context.ExecuteQuery()%0A%0A%23Upload%20File%0A%24s%20%3D%5BSystem.IO.MemoryStream%5D(%24ByteArray)%0A%24FileCreationInfo%20%3D%20New-Object%20Microsoft.SharePoint.Client.FileCreationInformation%0A%24FileCreationInfo.Overwrite%20%3D%20%24true%0A%24FileCreationInfo.ContentStream%20%3D%20%24s%0A%24FileCreationInfo.URL%20%3D%20%24UniqueFileName%0A%24Upload%20%3D%20%24List.RootFolder.Files.Add(%24FileCreationInfo)%0A%24Context.Load(%24Upload)%0A%24Context.ExecuteQuery()%20%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42395%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42395%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20FileCreationInformation%20object%20that%20you%20need%20to%20pass%20contains%20a%20property%20%22Content%22%20to%20which%20you%20can%20assign%20this%20Byte%5B%5D%20(which%20you%20already%20have%20right%3F)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42340%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42340%22%20slang%3D%22en-US%22%3EThis%20is%20the%20issue.%3CBR%20%2F%3EI%20don't%20have%20file%20on%20local%20computer%20or%20on%20shared%20drive.%3CBR%20%2F%3EI%20have%20a%20URL%20from%20where%20I%20download%20file%20in%20the%20form%20of%20Byte%20Array.%3CBR%20%2F%3E%3CBR%20%2F%3EOne%20possible%20way%20is%20to%20save%20byte%20array%20on%20local%20drive%20and%20then%20upload%20but%20this%20is%20last%20resort%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42339%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42339%22%20slang%3D%22en-US%22%3E%3CP%3EInstead%20of%20the%203%20parameters%20you%20are%20currently%20passing%20you%20have%20to%20provide%20a%20single%20parameter%20of%20type%20FileCreationInformation%20to%20List.RootFolder.Files.Add%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMore%20Info%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fmicrosoft.sharepoint.client.filecreationinformation.aspx%26nbsp%3B%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fmicrosoft.sharepoint.client.filecreationinformation.aspx%26nbsp%3B%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42325%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42325%22%20slang%3D%22en-US%22%3EYes.%3CBR%20%2F%3EFile%20from%20computer%20drive%20to%20SharePoint%20Online%20is%20working%20fine.%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42318%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42318%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20mean%20a%20local%20file%20to%20SharePoint%20or%20just%20a%20basic%20file%20to%20another%20folder%20local%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42309%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42309%22%20slang%3D%22en-US%22%3EI%20have%20tried%20with%20file%20on%20local%20drive%20and%20it%20seems%20to%20be%20working.%3CBR%20%2F%3E%3CBR%20%2F%3EIssue%20seems%20to%20be%20for%20Add%20method%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42307%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42307%22%20slang%3D%22en-US%22%3E%3CP%3ECheck%20the%20below%20sample.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgist.github.com%2Fasadrefai%2Fecfb32db81acaa80282d%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgist.github.com%2Fasadrefai%2Fecfb32db81acaa80282d%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20number%20of%20arguments%20for%20the%20Add%20is%201%2C%20also%20I%20think%20can't%20add%20the%20byte%20stream%20in%20one%20go.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-42305%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-42305%22%20slang%3D%22en-US%22%3E%3CP%3Ei%20think%20it%20can%20not%20find%20your%20library%20but%20not%20sure%20are%20you%20using%20the%20default%20documents%20library%3F(aka%20Shared%20Documents)%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I am trying to upload document to SharePoint Online using PowerShell / CSOM.
Issue is that I don't have file on local drive. I have URL of a file which I download as
$wc = New-Object System.Net.WebClient
$ByteArray = $wc.downloaddata(Document URL)

 

$ByteArray is a Byte array

 

 

When I try to user following code, it generates error

 

#Specify tenant admin and site URL and including DLL

$User = "XXXXX@XXXXXXX"
$SiteURL = "XXXXXXXXXXX"
$DocLibName = "Documents"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = 'XXXXXX' | ConvertTo-SecureString -AsPlainText -Force

 

#Bind to site collection

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
Retrieve list

$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

 

#Upload File

$Upload = $List.RootFolder.Files.Add('abc.pdf',$ByteArray,$true)
$Context.Load($Upload)
$Context.ExecuteQuery()

 

Add doesn't work and not sure how to upload a byte array. I have tried with different arguments.

 

Error is

 

Cannot find an overload for "Add" and the argument count: "3".

 

Any help?

12 Replies

i think it can not find your library but not sure are you using the default documents library?(aka Shared Documents)?

Check the below sample.

https://gist.github.com/asadrefai/ecfb32db81acaa80282d

 

The number of arguments for the Add is 1, also I think can't add the byte stream in one go.

I have tried with file on local drive and it seems to be working.

Issue seems to be for Add method

You mean a local file to SharePoint or just a basic file to another folder local?

Yes.
File from computer drive to SharePoint Online is working fine.

Instead of the 3 parameters you are currently passing you have to provide a single parameter of type FileCreationInformation to List.RootFolder.Files.Add

 

More Info:

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.filecreationinformation.aspx 

This is the issue.
I don't have file on local computer or on shared drive.
I have a URL from where I download file in the form of Byte Array.

One possible way is to save byte array on local drive and then upload but this is last resort

The FileCreationInformation object that you need to pass contains a property "Content" to which you can assign this Byte[] (which you already have right?)

best response confirmed by farhan faiz (Occasional Contributor)
Solution

Please check the below modified script, which generates the byte array from the given URL and upload it to the SPO.

 

 

$wc = New-Object System.Net.WebClient
# update with correct path 
$ByteArray = $wc.downloaddata("http://downloads.microsoft.com/download/file.exe")

#Specify tenant admin and site URL and including DLL
$User = "XXXXX@XXXXXXX"
$Password = 'XXXXXX' | ConvertTo-SecureString -AsPlainText -Force
$SiteURL = "XXXXXXXXXXX"
$DocLibName = "Documents"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Upload File
$s =[System.IO.MemoryStream]($ByteArray)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $s
$FileCreationInfo.URL = $UniqueFileName
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery() 

 

Have you tried to use PnP PowerShell commands? there's an AddFile command that can take a Stream as a parameter:
https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPFile.md
Thanks. Casting worked.
For those looking help on uploading files via PowerShell. I was able to do that using the information from the link below: The approach reads files from a drive and uploads them to the site using Invoke-WebRequest. This is a different from the original question, but I hope it helps.

#upload the file to the server using default credentials from the
#current session
$result = Invoke-WebRequest -Uri $url -InFile $filePath -Method PUT
-UseDefaultCredentials

http://www.ozkary.com/2017/09/powershell-upload-multiple-files-sharepoint.html