Tuesday, 16 June 2015

PowerShell script to upload documents to SharePoint online/Office 365

 

 

 

 

Below script is an ideal PowerShell script to deploy MasterPages or PageLayouts to SharePoint Online/ Office 365

This is been tested on Windows 10, Windows 8 platform and works without issues

Prerequisites:

1)      Copy the script below in a .ps1 file and keep it in a folder

2)      Keep all the documents that needs to be uploaded in the same folder with a new folder inside it named “Docs” (Important to keep the name as is)

3)      Be ready with the URL of the SharePoint site where the document needs to be uploaded

 

 

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

 

 

Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)

{

    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force

    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)

    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)

    return $context

}

 

 

Function Deploy-PageLayout([Microsoft.SharePoint.Client.Web]$Web,[string]$FolderPath,[string]$AssociatedContentTypeId)

{

    $filesfolde = Get-Location #Split-Path $script:MyInvocation.MyCommand.Path

    $masterPageLocalDir = Join-Path  $filesfolde "Docs"

 

    #For upload all files in document library from file system

    foreach ($FilePath in Get-ChildItem $masterPageLocalDir)

       {

         $pageLayoutContentTypeId = "0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE8110003D357F861E29844953D5CAA1D4D8A3B001EC1BD45392B7A458874C52A24C9F70B"

         $fileName = [System.IO.Path]::GetFileName($FilePath)

 

         $associatedContentType = $Web.AvailableContentTypes.GetById($AssociatedContentTypeId)

         $catalogList = $Web.GetCatalog([Microsoft.SharePoint.Client.ListTemplateType]::MasterPageCatalog)   

         $Web.Context.Load($catalogList.RootFolder)

         $Web.Context.Load($associatedContentType)

         $Web.Context.ExecuteQuery()

 

         $FilePath = Join-Path  $masterPageLocalDir $fileName

 

         $fileContent = [System.IO.File]::ReadAllBytes($FilePath)

         $fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation

         $fileInfo.Content = $fileContent

         $fileInfo.Url = $catalogList.RootFolder.ServerRelativeUrl + "/" + $fileName

         $fileInfo.Overwrite = $true

 

         $file = $catalogList.RootFolder.Files.Add($fileInfo)

         $Web.Context.Load($file)

         $Web.Context.ExecuteQuery()

 

         $listItem = $file.ListItemAllFields

         #listItem["Title"] = title;

         #listItem["MasterPageDescription"] = description;

         $listItem["ContentTypeId"] = $pageLayoutContentTypeId

         #$listItem["PublishingAssociatedContentType"] = [string]::Format(";#{0};#{1};#", $associatedContentType.Name, $associatedContentType.Id.StringValue)

         $listItem["UIVersion"] = [Convert]::ToString(15)

         $listItem.Update()

 

         $Web.Context.ExecuteQuery()

     }

}

 

$Url = Read-Host -Prompt "Enter the site url" #"https://microsoft.sharepoint.com/teams/GMOBISPOPOC/"

$UserName = Read-Host -Prompt "Enter the username"

$Password = Read-Host -Prompt "Enter the password"   

 

$WelcomePageContentTypeId = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4"

 

 

 

$context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password

Deploy-PageLayout -Web $context.Web -FilePath $masterPageLocalDir -AssociatedContentTypeId $WelcomePageContentTypeId

$context.Dispose()  

 

1 comment:

  1. microsoft visio professional 2016 activated , free win 7 keygen , how to genuine windows 7 ultimate free , windows 10 activation key 10240 , windows 7 product , windows 10 activation script , product key for windows 7 ultimate upgrade , vmware workstation 11 key generator , tlQ14z

    office 2016 product serial free

    windows 10 enterprise key

    office 2016 product key

    Windows 10 product key code sale

    office 2016 product key sale

    ReplyDelete