-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateTaxonomyTermSets.ps1
216 lines (197 loc) · 15 KB
/
CreateTaxonomyTermSets.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
# Script: CreateTaxonomyTermSets.ps1
# Description: Power Shell script to create term sets and terms in SharePoint Taxonomy Term Store using an XML file as input.
# Author: Venkatesh Subramanian
# Created Date: 26-March-2013
# =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
param (
[string]$centralAdminUrl = "Enter the Central Admin site URL",
[string]$termstoreName = "Enter the name of the Managed Metadata Service Instance",
[string]$filename = "Path to the XML File containing the TermSets and Terms and their GUIDs",
[string]$logfile = "Path to the Log File Name (.txt)",
[string]$groupName = "Enter the Term Group Name",
[string]$owner = "Enter the owner's user ID (for Term Group, Term Sets and Terms) "
)
$ErrorActionPreference = 'Stop'
function LogMessage ([string]$message)
{
Try
{
Write-Host $message
Out-File -filepath $logfile -InputObject $message -Append
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -foregroundcolor Red
break
}
}
function GroupExists([Microsoft.SharePoint.Taxonomy.GroupCollection]$grps,[string]$grpName)
{
Try
{
$grpExists = $false
foreach($tg in $grps){
if($tg.Name -eq $grpName){
LogMessage "Group ""$grpName"" exists!"
$grpExists = $true
break
}
}
Return $grpExists
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -foregroundcolor Red
break
}
}
function TermSetExists([Microsoft.SharePoint.Taxonomy.TermSetCollection]$tSets,[string]$tsName)
{
Try
{
$tsExists = $false
foreach($ts in $tSets){
if($ts.Name -eq $tsName){
LogMessage "Term Set ""$tsName"" exists!"
$tsExists = $true
break
}
}
Return $tsExists
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -foregroundcolor Red
break
}
}
function TermExists([Microsoft.SharePoint.Taxonomy.TermCollection]$termColln,[string]$tName)
{
Try
{
$tExists = $false
foreach($t in $termColln){
if($t.Name -eq $tName){
LogMessage "Term ""$tName"" exists!"
$tExists = $true
break
}
}
Return $tExists
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -foregroundcolor Red
break
}
}
function CreateChildTerms([System.Object]$cEles,[Microsoft.SharePoint.Taxonomy.Term]$t,[string]$tNm)
{
Try
{
foreach($cEle in $cEles)
{
$cEleName = $cEle.Attribute("Name").Value
$cEleID = $cEle.Attribute("ID").Value
$cEleExists = $false
$cTerms = $t.Terms
$cEleExists = TermExists $cTerms $tNm
if($cEleExists -eq $false)
{
$cTerm = $t.CreateTerm($cEleName, 1033, [System.Guid]($cEleID))
$cTerm.Owner = $owner
$termStore.CommitAll()
LogMessage "Created Term ""$cEleName"" in Term ""$tNm"""
}
$cTermEles = $cEle.Elements() | Where-Object {$_.NodeType -eq [System.Xml.XmlNodeType]::Element -and $_.Name -eq "Term"}
if($cTermEles.Count -gt 0)
{
CreateChildTerms $cTermEles $cTerm $cEleName
}
}
}
catch [System.Exception]
{
write-host $_.Exception.ToString() -foregroundcolor Red
break
}
}
# Load SharePoint PowerShell Snapin
Write-Host " Verifying if SharePoint PowerShell Snapin Loaded.." -ForegroundColor White
$snapin = Get-PSSnapin | Where-Object { $_.Name -eq 'Microsoft.SharePoint.Powershell'}
if($snapin -eq $null){
Write-Host " Loading SharePoint PowerShell Snapin..." -ForegroundColor Gray
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
Write-Host " Loaded SharePoint PowerShell Snapin..." -ForegroundColor Gray
# Validating Arguments
if (($siteurl -eq "") -or ($filename -eq "")) {
Write-Host "Site URL and Filename are required" -foregroundcolor Red
break }
if ($logfile -eq ""){$logfile="c:\temp\TermSetLog.txt"}
# Read TermSets XML file using XDocument
[Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null
$xDoc = [System.Xml.Linq.XDocument]::Load($filename)
Try {
#establish session and identify Term Store (same for all groups)
$site = Get-SPSite $centralAdminUrl
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termstore = $session.TermStores[$termstoreName]
get-date | Out-File -filepath $logfile -append
LogMessage "Importing Data from File: ""$filename"""
$termGroups = $termStore.Groups
$groupExists = $false
# Check if Term Group Exists or Not
$groupExists = GroupExists $termGroups $groupName
# Create Group and Add Contributor
if($groupExists -eq $false){
$tsGroup = $termStore.CreateGroup($groupName)
$tsGroup.AddContributor($owner)
$termStore.CommitAll()
LogMessage "Created Group ""$groupName"""
}
# Create Term Sets
$tsElements = $xDoc.Root.Descendants() | Where-Object { $_.NodeType -eq [System.Xml.XmlNodeType]::Element -and $_.Name -eq "TermSet"}
$termSets = $tsGroup.TermSets
foreach($tsEle in $tsElements){
$tsEleName = $tsEle.Attribute("Name").Value
$tsEleID = $tsEle.Attribute("ID").Value
$tsExists = $false
$tsExists = TermSetExists $termSets $tsEleName
if($tsExists -eq $false)
{
$termSet = $tsGroup.CreateTermSet($tsEleName, [System.Guid]($tsEleID), 1033)
$termSet.Owner = $owner
$termStore.CommitAll()
LogMessage "Created TermSet ""$tsEleName"" in Group ""$groupName"""
}
$termSet = $tsGroup.TermSets[$tsEleName]
$terms = $termSet.Terms
$tsElement = $xDoc.Root.Descendants() | Where-Object {$_.NodeType -eq [System.Xml.XmlNodeType]::Element -and $_.Name -eq "TermSet" -and $_.Attribute("Name").Value -eq $tsEleName}
$termElements = $tsElement.Elements() | Where-Object {$_.NodeType -eq [System.Xml.XmlNodeType]::Element -and $_.Name -eq "Term"}
foreach($tEle in $termElements){
$tEleName = $tEle.Attribute("Name").Value
$tEleID = $tEle.Attribute("ID").Value
$tExists = $false
$tExists = TermExists $terms $tEleName
if($tExists -eq $false)
{
$term = $termSet.CreateTerm($tEleName, 1033, [System.Guid]($tEleID))
$term.Owner = $owner
$termStore.CommitAll()
LogMessage "Created Term ""$tEleName"" in Term Set ""$tsEleName"""
}
$childTermEles = $tEle.Elements() | Where-Object {$_.NodeType -eq [System.Xml.XmlNodeType]::Element -and $_.Name -eq "Term"}
if($childTermEles.Count -gt 0)
{
CreateChildTerms $childTermEles $term $tEleName
}
}
}
Write-Host "Completed Successfully.."
} catch [System.Exception]{
write-host $_.Exception.ToString() -foregroundcolor Red
break
}
$site.dispose()