1

Somehow I am not able to add a managed metadata column using client side PowerShell script. Here is the code I am using:

#Specify tenant admin and site URL
$User = "-" 
$Password = "-"
$SiteURL = "-"
$GroupName = "People"
$termSetName = "Job Title"
#The name of the MMS Service Application where the Term Set lives
$mmsServiceName = "Managed Metadata Service"
$displayName = "Business Operations_Test"
$fieldGroup = "Columns"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"

$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

Write-Host "connected to online"

# Create Taxonomy Session
$taxonomy = GetTaxonomySession($Context)
$Context.Load($taxonomy)
$Context.ExecuteQuery()

#Retrieve Term Stores
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()

#Bind to Term Store
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()

#Bind to Group
$Group = $TermStore.Groups.GetByName($GroupName)
$Context.Load($Group)
$Context.ExecuteQuery()

#Bind to Term Set
$TermSet = $Group.TermSets.GetByName($TermSetName)
$Context.Load($TermSet)
$Context.ExecuteQuery()

$web = $Context.Web
$Context.Load($web)
$Context.ExecuteQuery()
$mytitle=$web.Title
 write-Host "Connected to SharePoint Online site: '$mytitle'" -ForegroundColor Green 


$taxonomyField = $web.Fields.CreateNewField("TaxonomyFieldType", $displayName)

  $taxonomyField.SspId = $termSet.TermStore.Id
  $taxonomyField.TermSetId = $termSet.Id
  $taxonomyField.AllowMultipleValues = $true
  $taxonomyField.Group = $fieldGroup
  $taxonomyField.StaticName = "Demo"
  $taxonomyField.ShowInEditForm = $true
  $taxonomyField.ShowInNewForm = $true
  $taxonomyField.Hidden = $false
  $taxonomyField.Required = $false

  $web.Fields.Add($taxonomyField);
  $web.Update();

  $Context.ExecuteQuery()

The problem is:

$taxonomy = GetTaxonomySession($Context) 
$Context.Load($taxonomy) 
$Context.ExecuteQuery() 
#Retrieve Term Stores 
$TermStores = $MMS.TermStores 
$Context.Load($TermStores) 
$Context.ExecuteQuery() 

When this line is getting executed $MMS.TermStores does not return any values

Can anyone help?

  • And what is your problem? Just don't code dump on us. – Eric Alexander May 3 '16 at 12:19
  • $taxonomy = GetTaxonomySession($Context) $Context.Load($taxonomy) $Context.ExecuteQuery() #Retrieve Term Stores $TermStores = $MMS.TermStores $Context.Load($TermStores) $Context.ExecuteQuery() now when this line are getting executed $MMS.TermStores does not return any values. – Rahul Barua May 3 '16 at 12:22
  • Your $taxonomyField object is not properly cast, so even if your code makes it this far you'll have more problems. – Derek Gusoff May 3 '16 at 21:22
3

Use the PnP PowerShell CmdLets, and you can do this with one line of code:

Add-SPOTaxonomyField -DisplayName "Region" -InternalName "Region" -TermSetPath "Contoso|Region"

Documentation

Implementation

  • This is what i did at last .. thanks for the reply!! – Rahul Barua May 12 '16 at 18:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.