Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Term store import with Office Dev PnP Full
Function writeTermLine($csvlines, $csvfile)
{
foreach($csvline in $csvlines)
{
$xmlWriter.WriteStartElement("pnp:Term")
$xmlWriter.WriteAttributeString("Name",$csvline.Name)
$xmlWriter.WriteAttributeString("Language","1033")
$xmlWriter.WriteStartElement("pnp:CustomProperties")
#properties
$xmlWriter.WriteStartElement("pnp:Property")
$xmlWriter.WriteAttributeString("Key","CompanyID")
$xmlWriter.WriteAttributeString("Value",$csvline.ID)
#end pnp:CustomProperties
$xmlWriter.WriteEndElement()
#properties
$xmlWriter.WriteStartElement("pnp:Property")
$xmlWriter.WriteAttributeString("Key","ParentCompanyID")
$xmlWriter.WriteAttributeString("Value",$csvline.parentID)
#end pnp:CustomProperties
$xmlWriter.WriteEndElement()
#properties
$xmlWriter.WriteStartElement("pnp:Property")
$xmlWriter.WriteAttributeString("Key","Sector")
$xmlWriter.WriteAttributeString("Value",$csvline.Sector)
#end pnp:CustomProperties
$xmlWriter.WriteEndElement()
#end pnp:CustomProperties
$xmlWriter.WriteEndElement()
$Companyid = $csvline.ID
#check if item has childrem
$csvitems = $csvfile | where {$_.parentID -eq $Companyid}
if($csvitems.Count -gt 0)
{
writeTermLine $csvitems $csvfile
}
#end pnp:Term
$xmlWriter.WriteEndElement()
}
}
$csv = Import-Csv -Delimiter `t -Path "CompanyList.txt"
$filePath = "CompanyList.xml"
$xmlencoding = New-Object System.Text.UTF8Encoding($false);
$xmlWriter = New-Object System.Xml.XmlTextWriter($filePath,$xmlencoding)
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = 4
$xmlWriter.WriteStartDocument();
$xmlWriter.WriteStartElement("pnp:TermGroups")
$xmlWriter.WriteStartElement("pnp:TermGroup")
$xmlWriter.WriteAttributeString("Name","Corporate")
$xmlWriter.WriteAttributeString("ID","c20c3140-29a4-4ca0-92a8-e353cca620bd")
$xmlWriter.WriteAttributeString("Description"," ")
#start termset
$xmlWriter.WriteStartElement("pnp:TermSets")
$xmlWriter.WriteStartElement("pnp:TermSet")
$xmlWriter.WriteAttributeString("Name","Company")
$xmlWriter.WriteAttributeString("Description","Company structure")
$xmlWriter.WriteAttributeString("Language","1033")
$xmlWriter.WriteStartElement("pnp:Terms")
#foreach row pnp:Term
$rooterm = $csv | Where { $_.ID -eq "1"}
writeTermLine $rooterm $csv
#end pnp:Terms
$xmlWriter.WriteEndElement()
#end pnp:TermSet
$xmlWriter.WriteEndElement()
#end pnp:TermSets
$xmlWriter.WriteEndElement()
#end pnp:TermGroup
$xmlWriter.WriteEndElement()
#end pnp:TermGroup
$xmlWriter.WriteEndElement()
#end document
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.