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?

3
  • And what is your problem? Just don't code dump on us. Commented May 3, 2016 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. Commented May 3, 2016 at 12:22
  • Your $taxonomyField object is not properly cast, so even if your code makes it this far you'll have more problems. Commented May 3, 2016 at 21:22

1 Answer 1

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

1
  • This is what i did at last .. thanks for the reply!! Commented May 12, 2016 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.