-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportContentTypesCSOM_PnP.ps1
56 lines (48 loc) · 1.85 KB
/
ImportContentTypesCSOM_PnP.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Param(
[Parameter(Mandatory=$true)]
[string]$SiteUrl,
[Parameter(Mandatory=$false)]
[string]$XMLCTFileName = "SiteContentTypes.xml"
)
Set-Location $PSScriptRoot
function LoadAndConnectToSharePoint($url)
{
##Using PnP library
Connect-PnPOnline -Url $SiteUrl #-CurrentCredentials
$spContext = Get-PnPContext
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-PnPContentType -Identity $_.Name
if( $spContentType -eq $null)
{
#Create Content Type object inheriting from parent
$spContentType = Add-PnPContentType -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-PnPFieldToContentType -Field $_.Name -ContentType $spContentType.Name -Required -Hidden
}
elseif($_.Hidden -eq "TRUE" -and $_.Required -eq "FALSE")
{
$spFieldLink = Add-PnPFieldToContentType -Field $_.Name -ContentType $spContentType.Name -Hidden
}
elseif($_.Hidden -eq "FALSE" -and $_.Required -eq "TRUE")
{
$spFieldLink = Add-PnPFieldToContentType -Field $_.Name -ContentType $spContentType.Name -Required
}
else
{
$spFieldLink = Add-PnPFieldToContentType -Field $_.Name -ContentType $spContentType.Name
}
}
write-host "Content type" $spContentType.Name "has been created"
}
}