From be621d1547bb1fc78159ca547af1b31adbe1ef98 Mon Sep 17 00:00:00 2001 From: JakubPiegza <45433779+JakubPiegza@users.noreply.github.com> Date: Thu, 30 Jan 2020 17:25:22 +0100 Subject: [PATCH] Update AddExistingContentTypetoOneList.ps1 --- .../AddExistingContentTypetoOneList.ps1 | 71 +++++++++---------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/Content Types/Create/Add existing content type directly to SPO list/AddExistingContentTypetoOneList.ps1 b/Content Types/Create/Add existing content type directly to SPO list/AddExistingContentTypetoOneList.ps1 index 43825fba..b03c128d 100644 --- a/Content Types/Create/Add existing content type directly to SPO list/AddExistingContentTypetoOneList.ps1 +++ b/Content Types/Create/Add existing content type directly to SPO list/AddExistingContentTypetoOneList.ps1 @@ -1,47 +1,40 @@ -function Add-SPOContentType -{ -param( -[Parameter(Mandatory=$true,Position=1)] +function Add-SPOContentType{ + param( + [Parameter(Mandatory=$true,Position=1)] [string]$Username, - [Parameter(Mandatory=$true,Position=2)] + [Parameter(Mandatory=$true,Position=2)] $AdminPassword, - [Parameter(Mandatory=$true,Position=3)] + [Parameter(Mandatory=$true,Position=3)] [string]$Url, - [Parameter(Mandatory=$true,Position=4)] + [Parameter(Mandatory=$true,Position=4)] [string]$ListTitle, - [Parameter(Mandatory=$true,Position=7)] + [Parameter(Mandatory=$true,Position=7)] [string]$ContentTypeID - ) - - $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url) - $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword) - $ctx.Load($ctx.Web.Lists) - $ctx.ExecuteQuery() - - $contentType=$ctx.Web.ContentTypes.GetById($ContentTypeID) - $ctx.Load($contentType) - - $ll=$ctx.Web.Lists.GetByTitle($ListTitle) - $ctx.load($ll) - $ctx.load($ll.ContentTypes) - $ctx.ExecuteQuery() - $ll.ContentTypesEnabled=$true - $AddedContentType=$ll.ContentTypes.AddExistingContentType($contentType) - $ll.Update() - - try - { - - $ctx.ExecuteQuery() - Write-Host "Adding content type " $AddedContentType.Name " to " $ll.Title - } - catch [Net.WebException] - { - Write-Host $_.Exception.ToString() - } - - - + ) + + $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url) + $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword) + $ctx.Load($ctx.Web.Lists) + $ctx.ExecuteQuery() + + $contentType=$ctx.Web.ContentTypes.GetById($ContentTypeID) + $ctx.Load($contentType) + + $ll=$ctx.Web.Lists.GetByTitle($ListTitle) + $ctx.load($ll) + $ctx.load($ll.ContentTypes) + $ctx.ExecuteQuery() + $ll.ContentTypesEnabled=$true + $AddedContentType=$ll.ContentTypes.AddExistingContentType($contentType) + $ll.Update() + + try{ + $ctx.ExecuteQuery() + Write-Host "Adding content type " $AddedContentType.Name " to " $ll.Title + } + catch [Net.WebException]{ + Write-Host $_.Exception.ToString() + } }