Sunday, September 18, 2016

SharePoint PowerShell to Manage Term Store / Add Terms using PowerShell

SharePoint PowerShell to Manage Term Store / Add Terms

//get the term store
$mysite = get-spsite “http://yoursite:portnumber
$taxonomySession = get-taxonomySession -site $mysite
$termStore = $taxonomySession.TermStores[“Managed Metadata Service”]

//now we will create the Term Store groups. Before we create any group we will check if the group already exists
Example 1:

if($termstore.groups["News Keys"] -eq $null) { $termstoregroup = $termstore.creategroup("News Keys");  $termstore.description = "News Group";  $termstore.addgroupmanager ("KS\spadmin"); $termstore.addcontributor ("ks\k.singh");$termStore.CommitAll();}

This is not working as “description” is not an identified property.

Example 2:

if($termstore.groups["Main News Keys"] -eq $null) { echo "group is not there";  echo "group is there"; $termstoregroup = $termstore.creategroup("Main News Keys"); $termstore.commitall();   }

Example 3:
$session = Get-SPTaxonomySession -Site "http://ksshrpt01/";
$termStore = $session.TermStores[“Managed Metadata Service”];
$group = $termstore.CreateGroup(“News Group”);
$group.Description = "My News Group";
$termStore.addgroupmanager ("KS\spadmin");
$termSet = $group.CreateTermSet(“Sports”,1033);
$termSet.Description = “Sports”;
$termSet.IsAvailableForTagging = $true;
$termSet.IsOpenForTermCreation = $true;

I encountered many issues/errors and finally went through. I will advise the following troubleshooting tips.

  1. termstore.description("") - Invalid command. This never worked for me. I shall update this once I come across any solution to this. Remove this cmdlet if you too face the same issue.
  2. I faced issue related to Permission (UnAuthorized User). I started the PowerShell console as Administrator and still I had the same issue.
  3. Central Admin -> Manage Service applications -> Managed Metadata Service -> Select and go to permission. Assign permission to the user running PowerShell.
  4. Central Admin -> Service Applications->Managed Metadata Service -> Properties -> Set term store administrator as the user you want. Save it and reset IIS as a precautionary measure.

The Create TermStore Group command shall now work for you.

Both of the following commands worked well.

  1. PS> if($termstore.groups["Main News Keys"] -eq $null) { echo "group is not there";  $termstoregroup = $termstore.creategroup("Main News Keys");  $termstore.commitall();   }
  2. PS>$session = Get-SPTaxonomySession -Site "http://ksshrpt01/";
PS>$termStore = $session.TermStores[“Managed Metadata Service”];
PS>$group = $termstore.CreateGroup(“News Group”);
PS>$group.Description = "My News Group";

*We will use the second command for all further examples

Please check the following screen.

Use following the command to assign permission to the group.

PS> $termstoregroup.addGroupManager("ks\spadmin")
PS> $termstoregroup.AddContributor("ks\k.singh")

You need to commit the changes to the Term Store.


Let’s create Term Sets in the Term Store

PS> $newTermSet = $termstoregroup.CreateTermSet("Sports",1033)
PS> $newTermSet = $termstoregroup.CreateTermSet("Politics",1033)
PS C:\Users\spadmin> $newTermSet = $termstoregroup.CreateTermSet("Nation",1033)
PS C:\Users\spadmin> $newTermSet = $termstoregroup.CreateTermSet("Business",1033)
PS C:\Users\spadmin> $termstore.commitall()

Hope this helps…..

No comments:

Visual Studio Build Error - The "GenerateResource" task failed unexpectedly

Error: "The GenerateResource task failed unexpectedly" This error is quite common while building the project in Visual Studio....