Forum Discussion
farhan faiz
Jan 30, 2017Copper Contributor
SharePoint Online - Upload document using PowerShell / CSOM
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.Web...
- Jan 31, 2017
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()
farhan faiz
Jan 30, 2017Copper Contributor
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
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
Jan 31, 2017
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()
- farhan faizFeb 01, 2017Copper ContributorThanks. Casting worked.