-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleup2sharepoint.ps1
52 lines (45 loc) · 2.59 KB
/
singleup2sharepoint.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Publishing\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Search\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Search.dll"
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.DocumentManagement\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.DocumentManagement.dll"
#Set parameter values
$SiteURL="https://clarios.sharepoint.com/sites/Partners/"
$SourceFilePath="D:\Files\upload\filesname1.xlsx"
$TargetFolderRelativeURL ="/sites/Partners/Shared Documents/subfolder"
$User = "[email protected]"
$Password = "Password" | ConvertTo-SecureString -AsPlainText -Force
#Bind to site collection
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Ctx.Credentials = $Credentials
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Target Folder to upload
$Web = $Ctx.Web
$Ctx.Load($Web)
$TargetFolder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
#Get the source file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $SourceFilePath -leaf
$TargetFileURL = $TargetFolderRelativeURL+"/"+$SourceFileName
#Upload the File to SharePoint Library Folder
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $TargetFileURL
$FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)
$Ctx.ExecuteQuery()
#Close file stream
$FileStream.Close()
Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
}
catch {
write-host "Error Uploading File to Folder: $($_.Exception.Message)" -foregroundcolor Red
}