Dec 12 2017
05:16 AM
- last edited on
Feb 01 2023
01:45 PM
by
TechCommunityAP
Dec 12 2017
05:16 AM
- last edited on
Feb 01 2023
01:45 PM
by
TechCommunityAP
I tried the sample powershell code in below link, getting error as path not found.
https://community.spiceworks.com/topic/2001221-powershell-script-to-upload-file-to-onedrive-business
Same error with any onedrive folder.
Can anyone share any sample and the settings I need to do in my OneDrive account for scripting.
Dec 12 2017 10:06 AM
Dec 12 2017 10:53 AM - edited Dec 12 2017 10:54 AM
Dec 12 2017 10:53 AM - edited Dec 12 2017 10:54 AM
It is not working for sub folders.
$DocLibName = "Documents" it is working(only for files, not folders)
$DocLibName = "Attachments" or $DocLibName = "Documents/Attachments" it is not working.
Attachments is the name of subfolder we created.
Dec 15 2017 06:23 AM
you can try this modified script and this script support sub folder in document library
$User = "testuser@domainname.com" $SiteURL = "https://test-my.sharepoint.com/personal/testuser_domainame_com"; $Folder = "C:\Users\Desktop\New folder" #DocDocLibName is document libary name $DocLibName = "Documents" $foldername = "Attachments" #Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM 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 = ConvertTo-SecureString ‘test1234’ -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.Load($List.RootFolder) $Context.ExecuteQuery() $ServerRelativeUrlOfRootFolder = $List.RootFolder.ServerRelativeUrl $uploadFolderUrl= $ServerRelativeUrlOfRootFolder+"/"+$foldername #Upload file Foreach ($File in (dir $Folder -File)) { $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open) $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.ContentStream = $FileStream $FileCreationInfo.URL = $File if($foldername -eq $null) { $Upload = $List.RootFolder.Files.Add($FileCreationInfo) } Else { $targetFolder = $Context.Web.GetFolderByServerRelativeUrl($uploadFolderUrl) $Upload = $targetFolder.Files.Add($FileCreationInfo); } #$Upload = $List.RootFolder.Files.Add($FileCreationInfo) $Context.Load($Upload) $Context.ExecuteQuery()
Dec 15 2017 06:33 AM
Thank you,
I will try this script.
And also if we share a folder internally with other team members, or if I open a shared folder by someone else, How to upload to that folder?
Dec 15 2017 06:40 AM
And also I am getting below error if I am uploading files larger than 200 MB.
Exception calling "ExecuteQuery" with "0" argument(s): "The operation has timed out"
At C:\Users\uday-\Desktop\New Text Document (2).ps1:55 char:1
+ $Context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Dec 15 2017 09:35 PM
Yes, this script only support upto 250 mb size for single file so if you want to upload more than 250mb file size of single file, you can refer the below link.
https://gist.github.com/asadrefai/ecfb32db81acaa80282d
Dec 16 2017 03:18 AM
UploadFileInSlice ($ctx, $libraryName, $fileName, $fileChunkSizeInMB)
What is the $ctx we need to pass?
Dec 16 2017 04:08 AM
Yes you need to pass the $ctx, $ctx is Client context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$ctx.Credentials = $Creds
May 16 2019 12:15 AM
Hi, I tried this script but getting below error:
Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the Microsoft account system.”
But the account do exists.
The account is working if I login through webportal.
Jun 03 2019 11:40 PM
Hello, I get the same error.
This worked 'previously' ( until about February this year ).
I am having the same issue with uploading to sharepoint teams sites.
I have tried every piece of code and uploader I could find in the last 2 weeks and nothing is working.
Sep 20 2019 04:01 AM
thanks for your code...
but, how can i do the same uploading to folders instead of lists?
regards
Dec 18 2019 02:19 PM
I had to modify this line to make it work
$FileCreationInfo.URL = $File
to this
$FileCreationInfo.URL = $ServerRelativeUrlOfRootFolder + "/" + $File.Name
This needs to be a web relative url and not the file path.
Its should equate to something like:
/personal/your_name_domain_com/Documents/Filename.txt
Jan 19 2024 05:05 AM
How do I use your great code and make it so it that allow me to download users' onedrive for business data to my local drive?
Thanks