Last active
June 23, 2016 14:08
-
-
Save reshmee011/cb83653a8926dfd84495b78fbb43c1f2 to your computer and use it in GitHub Desktop.
Export/Import of Site Columns and Site Content Types using SharePoint 2016 PnP PowerShell commands
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
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$SiteUrl, | |
[Parameter(Mandatory=$false)] | |
[string]$XMLCTFileName = "SiteContentTypes.xml", | |
[string] $GroupToExport = "Portfolio DB" | |
) | |
Set-Location $PSScriptRoot | |
function LoadAndConnectToSharePoint($url) | |
{ | |
##Using PnP library | |
Connect-SPOnline -Url $SiteUrl -CurrentCredentials | |
$spContext = Get-SPOContext | |
return $spContext | |
} | |
$Context = LoadAndConnectToSharePoint $SiteUrl | |
$SPOContentTypes = Get-SPOContentType | |
$PathToExportXMLSiteContentTypes = $PSScriptRoot | |
$xmlFilePath = "$PathToExportXMLSiteContentTypes\$XMLCTFileName" | |
#Create Export Files | |
New-Item $xmlFilePath -type file -force | |
#Export Site Columns to XML file | |
Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>" | |
Add-Content $xmlFilePath "`n<ContentTypes>" | |
$SPOContentTypes | ForEach-Object { | |
if ($_.Group -eq $GroupToExport) { | |
Add-Content $xmlFilePath $_.SchemaXml | |
} | |
} | |
Add-Content $xmlFilePath "</ContentTypes>" |
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
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$SiteUrl, | |
[Parameter(Mandatory=$false)] | |
[string]$XMLTermsFileName = "SiteColumns.xml", | |
[string] $GroupToExport = "Custom Group" | |
) | |
Set-Location $PSScriptRoot | |
function LoadAndConnectToSharePoint($url) | |
{ | |
##Using PnP library | |
Connect-SPOnline -Url $SiteUrl -CurrentCredentials | |
$spContext = Get-SPOContext | |
return $spContext | |
} | |
$Context = LoadAndConnectToSharePoint $SiteUrl | |
#http://dev-sp-001a:1214/ContentTypeHub | |
$SPOfields = Get-SPOField | |
$PathToExportXMLSiteColumns = $PSScriptRoot | |
$xmlFilePath = "$PathToExportXMLSiteColumns\$XMLTermsFileName" | |
#Create Export Files | |
New-Item $xmlFilePath -type file -force | |
#Export Site Columns to XML file | |
Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>" | |
Add-Content $xmlFilePath "`n<Fields>" | |
$SPOfields | ForEach-Object { | |
if ($_.Group -eq $GroupToExport) { | |
Add-Content $xmlFilePath $_.SchemaXml | |
} | |
} | |
Add-Content $xmlFilePath "</Fields>" |
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
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$SiteUrl, | |
[Parameter(Mandatory=$false)] | |
[string]$XMLCTFileName = "SiteContentTypes.xml" | |
) | |
Set-Location $PSScriptRoot | |
function LoadAndConnectToSharePoint($url) | |
{ | |
##Using PnP library | |
Connect-SPOnline -Url $SiteUrl -CurrentCredentials | |
$spContext = Get-SPOContext | |
return $spContext | |
} | |
LoadAndConnectToSharePoint $SiteUrl | |
#Get XML file exported | |
$xmlFilePath = (get-location).ToString() + "\$XMLCTFileName" | |
[xml]$CTXML = Get-Content($xmlFilePath) | |
$CTXML.ContentTypes.ContentType | ForEach-Object { | |
#check if content type exists | |
$spContentType = Get-SPOContentType -Identity $_.Name | |
if( $spContentType -eq $null) | |
{ | |
#Create Content Type object inheriting from parent | |
$spContentType = Add-SPOContentType -ContentTypeId $_.ID -Group $_.Group -Name $_.Name -Description $_.Description | |
$_.Fields.Field | ForEach-Object { | |
#Create a field link for the Content Type by getting an existing column | |
#-Required $Required -Hidden $Hidden | |
if($_.Required -eq "TRUE" -and $_.Hidden -eq "TRUE") | |
{ | |
$spFieldLink = Add-SPOFieldToContentType -Field $_.Name -ContentType $spContentType.Name -Required -Hidden | |
} | |
elseif($_.Hidden -eq "TRUE" -and $_.Required -eq "FALSE") | |
{ | |
$spFieldLink = Add-SPOFieldToContentType -Field $_.Name -ContentType $spContentType.Name -Hidden | |
} | |
elseif($_.Hidden -eq "FALSE" -and $_.Required -eq "TRUE") | |
{ | |
$spFieldLink = Add-SPOFieldToContentType -Field $_.Name -ContentType $spContentType.Name -Required | |
} | |
else | |
{ | |
$spFieldLink = Add-SPOFieldToContentType -Field $_.Name -ContentType $spContentType.Name | |
} | |
} | |
write-host "Content type" $spContentType.Name "has been created" | |
} | |
} |
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
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$SiteUrl, | |
[Parameter(Mandatory=$false)] | |
[string]$XMLTermsFileName = "SiteColumns.xml" | |
) | |
Set-Location $PSScriptRoot | |
function LoadAndConnectToSharePoint($url) | |
{ | |
Add-Type -Path "..\SharePoint Assemblies\Microsoft.SharePoint.Client.dll" | |
Add-Type -Path "..\SharePoint Assemblies\Microsoft.SharePoint.Client.Runtime.dll" | |
Add-Type -Path "..\SharePoint Assemblies\Microsoft.SharePoint.Client.Taxonomy.dll" | |
##Using PnP library | |
Connect-SPOnline -Url $SiteUrl #-CurrentCredentials | |
$spContext = Get-SPOContext | |
return $spContext | |
} | |
$Context = LoadAndConnectToSharePoint $SiteUrl | |
#http://dev-sp-001a:1214/ContentTypeHub | |
$xmlFilePath = (get-location).ToString() + "\$XMLTermsFileName" | |
#Get XML file exported | |
[xml]$fieldsXML = Get-Content($xmlFilePath) | |
$fieldsXML.Fields.Field | ForEach-Object { | |
$field = Get-SPOField -Identity $_.ID -ErrorAction SilentlyContinue | |
if($field -eq $null) | |
{ | |
$fieldName = $_.Name | |
##remove Version attribute from XML as throwing message "The object has been updated by another user since it was last fetched" | |
$_.RemoveAttribute("Version") | |
if($_.Type -eq 'TaxonomyFieldTypeMulti' -or $_.Type -eq 'TaxonomyFieldType') | |
{ | |
$termSetIdEle= $_.Customization.ArrayOfProperty.Property|?{$_.Name -eq "TermSetId"} | |
$termId= [Guid]$termSetIdEle.Value.InnerText; | |
$termStoreIdEle= $_.Customization.ArrayOfProperty.Property|?{$_.Name -eq "SspId"} | |
$ChildNodes = $_.ChildNodes; | |
foreach($childNode in $ChildNodes) {$childNode.ParentNode.RemoveChild($childNode)} | |
} | |
Add-SPOFieldFromXml -FieldXml $_.OuterXml | |
$field = Get-SPOField -Identity $_.ID | |
if($_.Type -eq 'TaxonomyFieldTypeMulti' -or $_.Type -eq 'TaxonomyFieldType') | |
{ | |
try | |
{ | |
##Retrieve as Taxonomy Field | |
$taxonomyField= [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($Context, $field) | |
$taxonomyField.SspId = [Guid]$termStoreIdEle.Value.InnerText; | |
$taxonomyField.TermSetId = $termId; | |
$taxonomyField.Update(); | |
$Context.ExecuteQuery(); | |
} | |
catch | |
{ | |
Write-Host "Error associating field $fieldName to term $termId Exception is $_.Exception.Message" -foregroundColor "Red" | |
return | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment