Questo script PowerShell permette di leggere, da uno SharePoint Online, i content type a livello di site collection con le relative colonne, appartenenti ad uno specifico gruppo, e generare le istruzioni PnP per ricreare gli stessi su un altra site collection

PowerShell: sgart-generate-ct.ps1

# Genera istruzioni PnP per la creazione di content type di sito in una diversa site collection - Sgart.it 
# https://www.sgart.it/IT/informatica/generatore-di-istruzioni-add-pnpcontenttype-per-site-content-type-sharepoint/post"
# Moduli necessari: Install-Module -Name PnP.PowerShell
# es.: .\sgart-generate-ct.ps1 "https://tenantXX.sharepoint.com/siteCollection" "nome gruppo" > ".\sgart-generate-ct-dest_$(Get-Date -format "yyyy-MM-dd_HH-mm-ss").ps1"

param (
        [Parameter(Mandatory=$true)]
        [string]$siteCollectionUrlSource,
        
        [Parameter(Mandatory=$true)]
        [string]$ctGroupName
    )

Write-Host "siteCollectionUrlSource: $siteCollectionUrlSource"
Write-Host "contentTypeGroupName: $ctGroupName"

Connect-PnPOnline -Url $siteCollectionUrlSource -Interactive

function EscapeString {
    param (
        [string]$str
    )

    $str.Replace("`"", "```"").Replace("`r", "``r").Replace("`n", "``n").Replace("`t", "``t");
}

Write-Output "#==========================================================================================="
Write-Output "# Sgart.it - https://www.sgart.it/IT/informatica/generatore-di-istruzioni-add-pnpcontenttype-per-site-content-type-sharepoint/post"
Write-Output "# Generato il $(Get-Date -format "yyyy-MM-dd HH:mm:ss")"
Write-Output "# "
Write-Output "# ATTENZIONE: modifica le variabili per la connessione al nuovo tenant/site collection !!!!!"
Write-Output "#==========================================================================================="
Write-Output "`$siteCollectionUrlDest = `"https://<tenant-di-destinazione>.sharepoint.com/<eventuale-site-collection-url>`";"
Write-Output "`$ctGroupName = `"$($ctGroupName)`";"
Write-Output " "
Write-Output "Write-Output `"Url: `$siteCollectionUrlDest, Group: `$fieldsGroupName`";"
Write-Output "Connect-PnPOnline -Url `$siteCollectionUrlDest -Interactive;"
Write-Output " "

# esporta tutti i content type e genera le istruzioni di creazione
Write-Output "#==========================================================================================="
Write-Output "# genera le istruzioni di creazione dei CONTENT TYPE di site collection con relativi campi collegati"
Write-Output "#==========================================================================================="
Write-Output " "

$contentTypes = Get-PnPContentType | Where-Object {$_.Group -eq $ctGroupName}

$i = 1
$m = $contentTypes.Count

$contentTypes | ForEach-Object {
    $ct = $_

    $ctVarName = "`$ctAdd$($i)"
    $ctName = $ct.Name

    $msg = "$i / $m) $ctVarName - $ctName ..."
    Write-Host $msg
    Write-Output "Write-Output `"$msg`";"

    $ctCmd = ""
    # $ctCmd = "$ctParentVarName = Get-PnPContentType -Identity `"$($parent.Name)`";`n"
    $ctCmd += "$ctVarName = Add-PnPContentType -Name `"$ctName`" -Description `"$(EscapeString $ct.Description)`" -ContentTypeId `"$($ct.StringId)`" -Group `$ctGroupName"
    If($true -eq $ct.Required) {
        $ctCmd += " -Required"
    }
    Write-Output "$ctCmd;`n"
    
    # campi del CT
    $fieldLinks = get-pnpproperty -clientobject $ct -property "FieldLinks"

    $fieldLinks | Where-Object {$_.Name -ne "ContentType"} | ForEach-Object {
        $fld = $_
        
        $fldStr = "Add-PnPFieldToContentType -Field `"$($fld.Name)`" -ContentType $ctVarName"
        if($true -eq $fld.Required) {
            $fldStr += " -Required"
        }
        if($true -eq $fld.Hidden) {
            $fldStr += " -Hidden"
        }
        $fldStr += ";"

        Write-Output $fldStr
    }
    Write-Output ""
    $i++
}
genera un output simile a questo

PowerShell

#===========================================================================================
# Sgart.it - https://www.sgart.it/IT/informatica/generatore-di-istruzioni-add-pnpcontenttype-per-site-content-type-sharepoint/post
# Generato il 2021-11-04 23:13:26
# 
# ATTENZIONE: modifica le variabili per la connessione al nuovo tenant/site collection !!!!!
#===========================================================================================
$siteCollectionUrlDest = "https://<tenant-di-destinazione>.sharepoint.com/<eventuale-site-collection-url>";
$ctGroupName = "Sgart CT";
 
Write-Output "Url: $siteCollectionUrlDest, Group: $fieldsGroupName";
Connect-PnPOnline -Url $siteCollectionUrlDest -Interactive;
 
#===========================================================================================
# genera le istruzioni di creazione dei CONTENT TYPE di site collection con relativi campi collegati
#===========================================================================================
 
Write-Output "1 / 2) $ctAdd1 - Sgart CT Test ...";
$ctAdd1 = Add-PnPContentType -Name "Sgart CT Test" -Description "content type di test" -ContentTypeId "0x0100D45F4E45BF9F98409F67ECCE0680B893" -Group $ctGroupName;

Add-PnPFieldToContentType -Field "Title" -ContentType $ctAdd1 -Required;
Add-PnPFieldToContentType -Field "ColonnaNumerica" -ContentType $ctAdd1 -Required;
Add-PnPFieldToContentType -Field "TMAGCCActions5" -ContentType $ctAdd1;

Write-Output "2 / 2) $ctAdd2 - Sgart CT 1 ...";
$ctAdd2 = Add-PnPContentType -Name "Sgart CT 1" -Description "esempio content type per export" -ContentTypeId "0x0100E755584AC0D99F40A14EE129F2D201F7" -Group $ctGroupName;

Add-PnPFieldToContentType -Field "Title" -ContentType $ctAdd2 -Hidden;
Add-PnPFieldToContentType -Field "SgartFldNumber" -ContentType $ctAdd2 -Required;
Add-PnPFieldToContentType -Field "SgartFldText" -ContentType $ctAdd2 -Required;
Add-PnPFieldToContentType -Field "SgartFldTextMulti" -ContentType $ctAdd2;
Add-PnPFieldToContentType -Field "SgartFldChoice" -ContentType $ctAdd2;
Non è detto che siano gestite tutte le casistiche.
Testato solo con SharePoint Online.

I comandi generati possono essere salvati su un file .ps1 con questo comando:

PowerShell

.\sgart-generate-ct.ps1 "https://tenantXX.sharepoint.com/siteCollection" "nome gruppo" > ".\sgart-generate-ct-dest_$(Get-Date -format "yyyy-MM-dd_HH-mm-ss").ps1"

Vedi anche Generatore di istruzioni Add-PnPField per site column SharePoint
Tags:
PowerShell199 SharePoint497 SharePoint 201667 SharePoint 201917 SharePoint Online75
Potrebbe interessarti anche: