Tuesday, February 2, 2016

PowerShell to upload a file into multiple sites under Project Web App (Office 365)

This is a powershell script custom written to upload an Excel report to multiple Project Online sites, but you could easily customize it to suit your own purposes. The same method works on Office 365 and SharePoint Online sites.

Prerequisites:
You will need to install the SharePoint 2013 client components on your client machine. I am running it on Windows 7 so I am actually using the PowerShell 64 bit version and 64bit client components.

 # Load SharePoint Client  
 Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"   
 Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"   

I am using the newest version on Project online, hence the 16 hive. 15 hive works as well!

This script will accept a site collection URL and loops through all subsites.

If a document library called "My Reports" is found, it will upload an excel file from the local drive to the library.

You will need the O365 account that has access to all sites.

 $StartAFile = get-date -uformat "%Y-%m-%d %H:%M:%S"  
 Write-Host $StartAFile   
 # Get the file on Disk that we want to upload  
 $filePath = "D:\Report.xlsx"  
 $file = Get-Item $filePath  
 $fileInfo = New-Object System.IO.FileInfo($filePath)  
 $strUrl = $fileInfo.BaseName + $fileInfo.Extension  
 $strUrl  
 #$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 = $strUrl  
 function UploadToSite(){  
 param(  
   $Url = $(throw "Please provide a Site Collection Url"),  
   $Credential = $(throw "Please provide a Credentials")  
 )  
       $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)   
       $context.Credentials = $Credential   
       $web = $context.Web  
       $context.Load($web)  
       $context.Load($web.Webs)  
       $context.ExecuteQuery()  
       foreach($web in $web.Webs)  
       {  
           if ($web.Title -match 'Demo' -or $web.Title -match 'Test')   
           {  
                # Test libraries, can skip
           }  
           else   
           {  
                #Retrieve list  
                $List = $web.Lists.GetByTitle("My Reports")  
                $context.Load($List)  
                if ($List)  
                {  
                     $context.ExecuteQuery()  
                     $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 = $strUrl  
                     Write-Host "Uploading to "$web.Title   
                     $Upload = $List.RootFolder.Files.Add($FileCreationInfo)  
                     $context.Load($Upload)  
                     $context.ExecuteQuery()  
                     $FileStream.Close()  
                     if ($FileStream) {$FileStream.Dispose()}  
                }  
                UploadToSite -Url $web.Url -Credential $Credential  
           }  
       }  
 }  
 # Connect to PWA, retrieve list of project sites  
 $UserName = "admin@yourcompany.onmicrosoft.com"  
 $Password = "yourpassword"  
 $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force  
 $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)  
 UploadToSite -Url 'https://yourcompany.sharepoint.com/sites/pwa' -Credential $SPOCredentials  
 $EndAFile = get-date -uformat "%Y-%m-%d %H:%M:%S"  
 Write-Host $EndAFile   

No comments:

Related Posts Plugin for WordPress, Blogger...