1

The following is just a test script borrowed in part from http://goo.gl/Cw1D8h "PowerShell to create and reuse Managed Navigation for multiple sites in SharePoint 2013"

The following script works fine initially when it doesn't have to delete a Termset. But if the termset already exists. I get the following error at the $termStore.CommitAll()

]: PS C:>> Exception calling "CommitAll" with "0" argument(s): "Term set deletion failed because of save conflict." At C:_ps1\MetaDataNav\POSHCreateMetaData.ps1:57 char:1 + $termStore.CommitAll() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : TermStoreOperationException

I'm trying to find a reusable way to enter Link Terms for a SharePoint 2013 Metadata Navigational Menu.

The script will work again if I go into Site Settings => Manage Metadata and delete the termset. I want to automate the deletion.

Script below.

change these variables based on environment

$siteName = "http://intranet.contoso.com/sites/ep" 
$termStoreName = “Managed Metadata Service”
$termGroupName = “Navigation”
$termsetName = "Custom Navigation Termset"
$appendUrl = "http://intranet.contoso.com/sites/ep"
$defaultMetadataServiceProxy = "Managed Metadata Service"


function CreateTerm( $parent, $name, $url )  
{  
  Write-Host "Adding term $name to parent $parent.Name"  
  $term = $parent.CreateTerm("$name", 1033)  
  $term.IsAvailableForTagging = $true    
  $newUrl = $url
  if(!$url.ToLower().Contains("http"))
  {  
    # Logic to append site url to relative path to create complete url which is needed for Simple Link Navigation
    $newUrl = $appendUrl + $url
  }
  $term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $newUrl)   
  return $term  
} 

Create termgroup and termset for My Site

get central admin

$adminwebapp = Get-SPWebapplication -includecentraladministration | where   {$_.IsAdministrationWebApplication}

$caSite=Get-SPSite $adminwebapp.Url

get the Taxonomy session

$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($caSite)

get the Term Store

$termstore = $session.TermStores | where {$_.Name -eq     $defaultMetadataServiceProxy}

create the group (if required)

$group = $termstore.Groups["$termGroupName"]
if ($group -eq $null)
{
 $group = = $termstore.CreateGroup("$termGroupName");
 $group.Description = "Term Group description"
}

$termStore.CommitAll()

Write-Host "TermGroup - $termGroupName created successfully"  

delete the term set (if it exists)

if ($group.TermSets["$termSetName"] -ne $null)
{
 $group.TermSets["$termSetName"].Delete();
 #$group.TermSets | % { $_.Delete() } #Should delete all Termsets
}


$termStore.CommitAll()

Write-Host "TermGroup - $termSetName Deleted or Absent"  


$termSet = $group.CreateTermSet($termsetName,1033)
$termSet.Description = “Navigation TermSet description”
$termSet.IsAvailableForTagging = $false
$termSet.IsOpenForTermCreation = $false
$navigationSet = $group.TermSets[$termsetName]
$navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
$termStore.CommitAll()

Write-Host "TermSet - $termsetName created successfully"  

create terms for My site

$wwrTerm = CreateTerm $termSet “WHO WE ARE” "/who"
$wavTerm = CreateTerm $termSet “WORKING AT OUR COMPANY” "/working"
$wynTerm = CreateTerm $termSet “ABOUT US” "/what"

$termStore.CommitAll()

$termSet.CustomSortOrder =     $wwrTerm.id.ToString()+":"+$wavTerm.id.ToString()+":"+$wynTerm.id.ToString()

create sub terms for My site

$term1 = CreateTerm $wwrTerm "Company" "/who/company"
$term2 = CreateTerm $wwrTerm "Organization" "/who/organization"
$term3 = CreateTerm $wwrTerm "Products" "/who/products"

$term4 = CreateTerm $wavTerm "Perks" "/working/perks"
$term5 = CreateTerm $wavTerm "Recognition" "/working/recognition"
$term6 = CreateTerm $wavTerm "HR Info" "/who/organization/hr"

$term7 = CreateTerm $wynTerm "Resources and Services" "/what/resources/"
$term8 = CreateTerm $wynTerm "Policies" "/what/policies"
$term9 = CreateTerm $wynTerm "Contact Us" "/what/contact"

$wwrTerm.CustomSortOrder = $term1.id.ToString() +":"+ $term2.id.ToString() +":"+ $term3.id.ToString()
$wavTerm.CustomSortOrder = $term4.id.ToString() +":"+ $term5.id.ToString() +":"+ $term6.id.ToString()
$wynTerm.CustomSortOrder = $term7.id.ToString() +":"+ $term8.id.ToString() +":"+ $term9.id.ToString()
$termStore.CommitAll()

Write-Host "Terms for TermSet - $termsetName created successfully"  
  • If I set the Navigation to Structural and delete the last TermSet I created for Navigation--this works fine. – user10711 Jan 12 '14 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.