none
Add multiple Taxonomy terms in a taxonomy field using powershell

    Question

  • Hi,

    I want to add multiple taxonomy terms in a taxonomy field. Is there a way to do this in powershell? I want to add the terms based on some conditions I dont want to add all the terms in a termstore to the field.

    Thanks,

    Bokes

    Friday, July 29, 2011 12:38 AM

Answers

  • Hi Bokes,
     
    In order to use Taxonomy terms to a taxonomy field using PowerShell, we need to involve SharePoint Object Model from PowerShell, and then use the Object Model to add terms in a termstore to the field.
     
    Below is the same code for your reference. Please add the logic in the For-Each method:
     
    #Site Collection URL - Give your site collection url in quotation marks
     $TaxonomySiteUrl = "http://server"

    #Access the termstore
    $site = Get-SPSite $TaxonomySiteUrl
    $web = $site.OpenWeb()

    $list = $web.Lists["listName"]

    $newItem = $list.Items.Add()
     
    $targetField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["Name of Taxonomy Field"]
     
    $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
     $termstore = $session.TermStores[$targetField.SspId]
     $termSet = $termstore.GetTermSet($targetField.TermSetId)
     
    $taxCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($targetField)
     
    $termSet.GetAllTerms() | ForEach-Object {
     $taxonomyFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($targetField)
     $taxonomyFieldValue.TermGuid = $_.Id
     $taxonomyFieldValue.Label = $_.Name
     $taxCollection.Add($taxonomyFieldValue)
     }
     
    $targetField.SetFieldValue($newItem, $taxCollection)
     
    $newItem["Title"] = "Item1"
    $newItem.Update()
     
     
     
    For more ifnormation, please see:
    Creating Managed Metadata Termsets using PowerShell: http://www.sharepointfix.com/2011/01/creating-managed-metadata-termsets.html
    TaxonomyField Class: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx
     
    If you have any more questions, please feel free to ask.

    Thanks,
    Jinchun Chen


    • Marked as answer by Wayne Fan Saturday, August 06, 2011 4:38 AM
    Monday, August 01, 2011 8:29 AM

All replies

  • Hi Bokes,
     
    In order to use Taxonomy terms to a taxonomy field using PowerShell, we need to involve SharePoint Object Model from PowerShell, and then use the Object Model to add terms in a termstore to the field.
     
    Below is the same code for your reference. Please add the logic in the For-Each method:
     
    #Site Collection URL - Give your site collection url in quotation marks
     $TaxonomySiteUrl = "http://server"

    #Access the termstore
    $site = Get-SPSite $TaxonomySiteUrl
    $web = $site.OpenWeb()

    $list = $web.Lists["listName"]

    $newItem = $list.Items.Add()
     
    $targetField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["Name of Taxonomy Field"]
     
    $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
     $termstore = $session.TermStores[$targetField.SspId]
     $termSet = $termstore.GetTermSet($targetField.TermSetId)
     
    $taxCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($targetField)
     
    $termSet.GetAllTerms() | ForEach-Object {
     $taxonomyFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($targetField)
     $taxonomyFieldValue.TermGuid = $_.Id
     $taxonomyFieldValue.Label = $_.Name
     $taxCollection.Add($taxonomyFieldValue)
     }
     
    $targetField.SetFieldValue($newItem, $taxCollection)
     
    $newItem["Title"] = "Item1"
    $newItem.Update()
     
     
     
    For more ifnormation, please see:
    Creating Managed Metadata Termsets using PowerShell: http://www.sharepointfix.com/2011/01/creating-managed-metadata-termsets.html
    TaxonomyField Class: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx
     
    If you have any more questions, please feel free to ask.

    Thanks,
    Jinchun Chen


    • Marked as answer by Wayne Fan Saturday, August 06, 2011 4:38 AM
    Monday, August 01, 2011 8:29 AM
  • Hi do you know how you would do this in c#?
    Thursday, January 19, 2012 4:46 PM
  • Jinchun,

    My requirement is add Managed Metadata Columns to a sharepoint document library using powershell.And i am very new to powershell and unable to convert the c sharp code into powershell.Cna you please help me on this.I am in very need of it.

    Please respond to this

    Thanks in advance

    Kashyap

    Monday, March 26, 2012 2:45 PM
  • Hi Kashyap,

    The code I posted in my previous reply is PowerShell. You can use it directly.

    Anyway, as this case is a little old. I would suggest you posting a new question in the forum to get better support.

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)

    Wednesday, March 28, 2012 9:45 AM
  • Hello Manonamission

    Here is c# interpretation :

           private void SetTaxonomyFieldMultiValue(SPListItem listItem, SPField field, string fieldValue)
            {
                TaxonomyField taxonomyField = listItem.Fields.GetFieldByInternalName(field.InternalName) as TaxonomyField;

                if (String.IsNullOrEmpty(fieldValue) || String.IsNullOrWhiteSpace(fieldValue)) return;

                fieldValue = fieldValue.Trim();

                string[] labelGuidPairFieldValues = fieldValue.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                TaxonomyFieldValueCollection taxonomyFieldValues = new TaxonomyFieldValueCollection(taxonomyField);

                foreach (string labelGuidPairFieldValue in labelGuidPairFieldValues)
                {
                    TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
                    taxonomyFieldValue.PopulateFromLabelGuidPair(labelGuidPairFieldValue);
                    taxonomyFieldValues.Add(taxonomyFieldValue);
                }

                listItem[taxonomyField.Id] = taxonomyFieldValues;
                listItem[taxonomyField.TextField] = labelGuidPairFieldValues;
            }


    Momo

    Wednesday, May 23, 2012 7:35 PM