Office 365 and PnP-PowerShell 2013 : ContentType
In this
content type section is most important section. You can use these methods to
create, delete and update the existing content type. So the cmdlets are piety
straight. The following are the command lets that we are going to use in this
section. You can check the github documentation and example as well.
With the help of these cmdlets I will create the Utility same as we did for in before blog post.
- Add‑PnPContentType
- Get‑PnPContentType
- Remove‑PnPContentType
- Remove‑PnPContentTypeFromList
- Get‑PnPContentTypePublishingHubUrl
- Add‑PnPContentTypeToList
- Set‑PnPDefaultContentTypeToList
- Remove‑PnPFieldFromContentType
- Add‑PnPFieldToContentType
Lets start the end of my goal is the build some kind of meaning utility that you can quickly use in your project and get some benefit.
Same create the multiple content type and attach columns to content type. For configure the content type, I am using the same CSV approach to read every properties from the file and built the content type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ContentTypeID | ContentTypeName | ContentTypeDescription | ContentTypeGroup | ParentContentType | SiteColumns | HideColumns | |
---|---|---|---|---|---|---|---|
E4AA64A88E735043B17388E3FD765050 | MyContentType | This is my content type | MyGroup | Item | SiteColumn1^TRUE|SiteColumn2^FALSE|SiteColumn3^FALSE||SiteColumn4^FALSE | SiteColumn3|SiteColumn4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Dev Import | |
Import-Module 'C:\Users\basantp\Documents\PNP Blog Section\Code\SharePointPnPPowerShell2013\SharePointPnP.PowerShell.2013.Commands.dll'; | |
function Create-ContentType ($csvPath , $siteUrl) { | |
#get and save your O365 credentials | |
#connect to the root site collection first | |
Connect-PnPOnline –Url $siteUrl -CurrentCredentials | |
$message = "Connected to site : $siteUrl" | |
Write-Host $message | |
Write-Host $message -ForegroundColor Green | |
$MyFile = Import-Csv $csvPath | |
# Looping the csv file collection | |
foreach ($myRow in $MyFile) { | |
$ContentTypeName = $myRow.ContentTypeName | |
$ParentContentType = $myRow.ParentContentType | |
$Description = $myRow.ContentTypeDescription | |
$Group = $myRow.ContentTypeGroup | |
$ContentTypeID = $myRow.ContentTypeID | |
$Required = $false | |
$pageCT = Get-PnPContentType -Identity $ParentContentType | |
$ctypeParentID = $pageCT.Id.StringValue.ToString() | |
$sep = "00" | |
$ct = [System.Convert]::ToString("$ctypeParentID$sep$ContentTypeID"); | |
$ctype = Get-PnPContentType -Identity $ContentTypeName | |
if (!$ctype) { | |
try { | |
$ctype = Add-PnPContentType -Name $ContentTypeName -Description $Description -Group $Group -ContentTypeId $ct | |
$message = "Content type : $ContentTypeName created !!" | |
Write-Host $message | |
Write-Host $message -ForegroundColor green | |
} | |
catch { | |
$message = "Unable to Create Content type : $ContentTypeName !!" | |
Write-Host $message | |
Write-Host $message -ForegroundColor Red | |
Write-Host "$($_.Exception.Message)" | |
} | |
#Read the columns form the CSV with "|" separated, example: "SiteColumn1^TRUE|SiteColumn2^FALSE" | |
if ($myRow.SiteColumns -ne "") { | |
foreach ($colName in $myRow.SiteColumns.split("|")) { | |
try { | |
$fieldInternalName = "" | |
if ($colName.indexOf("^") -ge 0) { | |
$fieldInternalName = $colName.split("^")[0] | |
} | |
else { | |
$fieldInternalName = $colName | |
} | |
if ($colName.indexOf("^") -ge 0) { | |
$Required = [System.Convert]::ToBoolean($colName.split("^")[1]) | |
} | |
try { | |
$lnkCol = Add-PnPFieldToContentType -Field $fieldInternalName -ContentType $ContentTypeName -Required:$Required -ErrorAction Stop | |
$message = "Add field $fieldInternalName to content type : $ContentTypeName " | |
Write-Host $message | |
Write-Host $message -ForegroundColor green | |
} | |
catch { | |
$message = "Unable add the field $fieldInternalName to content type : $ContentTypeName " | |
Write-Host $message | |
Write-Host $message -ForegroundColor Red | |
Write-Host "$($_.Exception.Message)" | |
} | |
} | |
catch { | |
write-host "Column $colName not found." | |
} | |
} | |
} | |
#Read the hidden columns form the CSV with "|" separated, example: "SiteColumn1|SiteColumn2" | |
if ($myRow.HiddenColumns -ne $null -and $myRow.HiddenColumns -ne "") { | |
$ctype = Get-PnPContentType -Identity $ContentTypeName | |
Get-PnPProperty -ClientObject $ctype -Property FieldLinks | |
foreach ($hiddenCol in $myRow.HiddenColumns.split("|")) { | |
$FieldCT = $null; | |
foreach ($Field in $ctype.FieldLinks) { | |
"Field Name: $($Field.Name) and Hidden Column name: $hiddenCol" | |
if ($Field.Name -eq $hiddenCol) { | |
$FieldCT = $Field; | |
break; | |
} | |
} | |
if ($FieldCT -ne $null) { | |
$FieldCT.set_Hidden($true); | |
$FieldCT.set_Required($false) | |
} | |
} | |
try { | |
$ctype.Update($true); | |
Execute-PnPQuery | |
$message = "Make Hidden $($myRow.HiddenColumns) , from content type $ContentTypeName" | |
Write-Host $message | |
Write-Host $message -ForegroundColor green | |
} | |
Catch { | |
$message = "Unable Make Hidden $($myRow.HiddenColumns) , from content type $ContentTypeName" | |
Write-Host $message | |
Write-Host $message -ForegroundColor red | |
Write-Host "$($_.Exception.Message)" | |
} | |
} | |
} | |
} | |
} | |
$siteUrl = "http://myintranet.com/sites" | |
$path = 'C:\Users\basantp\Documents\PNP Blog Section\Code\ContentType' | |
$inputFile = $path + "\CreateContentType.csv" | |
Create-ContentType $inputFile $siteUrl |
Remove Content Type or Site Columns from ContentType
Remove content type and remove particular columns in the content type. It is very easy, If you required to delete multiple of columns you can create one utility to delete the columns form the csv as well.
Remove content type and remove particular columns in the content type. It is very easy, If you required to delete multiple of columns you can create one utility to delete the columns form the csv as well.
_______
_______
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ContentTypeName | ||
---|---|---|
MyContentType | ||
MyContentType2 | ||
MyContentType3 | ||
MyContentType4 | ||
MyContentType5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Dev Import | |
Import-Module 'C:\Users\basantp\Documents\PNP Blog Section\Code\SharePointPnPPowerShell2013\SharePointPnP.PowerShell.2013.Commands.dll'; | |
function Create-ContentType ($csvPath , $siteUrl) { | |
#connect to the root site collection first | |
Connect-PnPOnline –Url $siteUrl -CurrentCredentials | |
$message = "Connected to site : $siteUrl" | |
Write-Host $message | |
Write-Host $message -ForegroundColor Green | |
$MyFile = Import-Csv $csvPath | |
# Looping the csv file collection | |
foreach ($myRow in $MyFile) { | |
$ContentTypeName = $myRow.ContentTypeName | |
$ctype = Get-PnPContentType -Identity $ContentTypeName | |
if (!$ctype) { | |
try { | |
Remove-PnPContentType -Identity $ContentTypeName -Force | |
$message = "Content type : $ContentTypeName Deleted !!" | |
Write-Host $message | |
Write-Host $message -ForegroundColor green | |
} | |
catch { | |
$message = "Unable to delete Content type : $ContentTypeName !!" | |
Write-Host $message | |
Write-Host $message -ForegroundColor Red | |
Write-Host "$($_.Exception.Message)" | |
} | |
} | |
else { | |
$message = "Content type : $ContentTypeName !!, not found" | |
Write-Host $message | |
Write-Host $message -ForegroundColor Red | |
} | |
} | |
} | |
$siteUrl = "http://myintranet.com/sites" | |
$path = 'C:\Users\basantp\Documents\PNP Blog Section\Code\ContentType' | |
$inputFile = $path + "\RemoveContentType.csv" | |
Remove-ContentType $inputFile $siteUrl |
Comments
Post a Comment