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?
$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.