|
############################ |
|
# Configuration Parameters # |
|
############################ |
|
$tenantAdmin = "admin@josharepoint.onmicrosoft.com" |
|
$tenantAdminPassword = "pass" |
|
$secureAdminPassword = $(convertto-securestring $tenantAdminPassword -asplaintext -force) |
|
$siteURL = "https://blog.josequinto.com"; |
|
$lcidAllWebTemplates = 1033; |
|
$lcid = "all"; #1033 |
|
$templateName = "{20292346-62A0-401F-810A-56C44317B2AE}#WT_Custom" #"ENTERWIKI#0" |
|
|
|
|
|
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" |
|
#Get-Item "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" |
|
|
|
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL) |
|
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin, $secureAdminPassword) |
|
$ctx.Credentials = $credentials |
|
|
|
################################################################# |
|
# Get all Web Templates to verify our specified template exists # |
|
################################################################# |
|
#Site.GetWebTemplates method: https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.site.getwebtemplates.aspx |
|
$site = $ctx.Site; |
|
$ctx.Load($site); |
|
$ctx.ExecuteQuery(); |
|
$loc = [System.Int32]::Parse($lcidAllWebTemplates) |
|
$templates = $site.GetWebTemplates($loc, $false); |
|
$ctx.Load($templates); |
|
$ctx.ExecuteQuery(); |
|
# Convert Name property values into array |
|
$arrayWebTemplates = $templates.Name; |
|
|
|
if ($arrayWebTemplates -contains $templateName){ |
|
|
|
# Get __WebTemplates property bag |
|
$web = $ctx.Web; |
|
$ctx.Load($web); |
|
$ctx.ExecuteQuery(); |
|
$allProp = $web.AllProperties; |
|
$ctx.Load($allProp); |
|
$ctx.ExecuteQuery(); |
|
[xml]$xmlAvailableWebTemplates = $allProp["__WebTemplates"]; |
|
|
|
if ($xmlAvailableWebTemplates){ |
|
|
|
## Find if exists the given template name |
|
#<webtemplates> |
|
# <lcid id="all"> |
|
# <webtemplate name="CMSPUBLISHING#0" /> |
|
# <webtemplate name="{20292346-62A0-401F-810A-56C44317B2AE}#WT_Custom" /> |
|
# </lcid> |
|
#</webtemplates> |
|
$propertyXML = $xmlAvailableWebTemplates.SelectSingleNode("//webtemplates/lcid[@id='$lcid']/webtemplate[@name='$templateName']"); |
|
$propertyXML.OuterXml |
|
if (!$propertyXML.OuterXml){ |
|
|
|
$currentLCID = $xmlAvailableWebTemplates.SelectSingleNode("//webtemplates/lcid[@id='$lcid']"); |
|
if ($currentLCID){ |
|
$wt = $xmlAvailableWebTemplates.CreateElement('webtemplate'); |
|
$wt.SetAttribute('name',$templateName); |
|
$currentLCID.AppendChild($wt); |
|
$xmlAvailableWebTemplates.OuterXml |
|
|
|
#Save to Property Bag again |
|
try{ |
|
$web.AllProperties["__WebTemplates"] = $xmlAvailableWebTemplates.OuterXml; |
|
#$web.AllProperties["__InheritWebTemplates"] = "False"; |
|
$web.Update(); |
|
$ctx.ExecuteQuery(); |
|
|
|
Write-Host "Updated!" -ForegroundColor Green; |
|
} |
|
catch |
|
{ |
|
Write-Host "Errors found:`n$_" -ForegroundColor Red |
|
} |
|
} |
|
else{ |
|
Write-Host "LCID $lcid doesn't exists in the current Templates." -ForegroundColor Red |
|
} |
|
} |
|
else{ |
|
Write-Host "Template $templateName already exists in " $web.Title ": " $propertyXML.OuterXml -ForegroundColor Red |
|
} |
|
} |
|
} |
|
else{ |
|
Write-Host "$templateName doesn't exists in the current Site Collection." -ForegroundColor Red |
|
} |
|
|
|
$ctx.Dispose() |