SHOW:
|
|
- or go back to the newest paste.
| 1 | # Most of the code came from this article: | |
| 2 | # http://blogs.technet.com/b/fromthefield/archive/2014/02/19/office365-script-to-upload-files-to-a-document-library-using-csom.aspx | |
| 3 | ||
| 4 | $File = $args[0] | |
| 5 | ||
| 6 | #Specify tenant admin and site URL | |
| 7 | $User = "[email protected]" | |
| 8 | $SiteURL = "https://valleymed.sharepoint.com/sites/PNPTest" | |
| 9 | $DocLibName = "hcitest" | |
| 10 | ||
| 11 | #Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM | |
| 12 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
| |
| 13 | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
| |
| 14 | ||
| 15 | $Password = "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000ef46c4675031d4478b6c5df04ddee3610000000002000000000003660000c000000010000000f6dd700cb3ffad231094c38af79b94a70000000004800000a0000000100000004f7f0539f41c05e01c66788a693b742510000000b098e29ff72a47d68c7c133974b58d5b14000000fc16cbac8fa8b3ef9edb1781e00efdc51d01b81c" | ConvertTo-SecureString | |
| 16 | ||
| 17 | #Bind to site collection | |
| 18 | $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) | |
| 19 | $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) | |
| 20 | $Context.Credentials = $Creds | |
| 21 | ||
| 22 | #Retrieve list | |
| 23 | $List = $Context.Web.Lists.GetByTitle($DocLibName) | |
| 24 | $Context.Load($List) | |
| 25 | $Context.ExecuteQuery() | |
| 26 | ||
| 27 | $FileStream = New-Object IO.FileStream($File,[System.IO.FileMode]::Open, [System.IO.FileAccess]::Read) | |
| 28 | $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation | |
| 29 | $FileCreationInfo.Overwrite = $true | |
| 30 | $FileCreationInfo.ContentStream = $FileStream | |
| 31 | $FileCreationInfo.URL = $File | |
| 32 | $Upload = $List.RootFolder.Files.Add($FileCreationInfo) | |
| 33 | $Context.Load($Upload) | |
| 34 | Write-host "Uploading" $File | |
| 35 | $Context.ExecuteQuery() |