Skip to main content

Programmatically Setting Taxonomy Field Values

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx



static void Main(string[] args)
{
    var url = ConfigurationManager.AppSettings["Url"].ToString();
    var library = ConfigurationManager.AppSettings["Library"].ToString();

    // Reads XML, considering
    // (key => lookup field,value => taxonomy field)
    Dictionary<string, string> mappingFields = XMLHelper.ReadDictionaryXML("MappingFields.xml");

    using (SPSite site = new SPSite(url))
    {
        using (SPWeb web = site.RootWeb)
        {
            // Gets the list/library
            SPList list = web.Lists[library];

            foreach (SPListItem item in list.Items)
            {
                // Iterates through all mapped fields
                foreach (var mappedField in mappingFields)
                {
                    if (item.Fields.ContainsField(mappedField.Key))
                    {
                        // Allows updates without a trace
                        web.Site.AllowUnsafeUpdates = true;

                        // Gets the lookup field instance
                        var lookupValueList = (item[mappedField.Key] as SPFieldLookupValueCollection).ToList();

                        // Gets the taxonomy field instance
                        TaxonomyField managedField = item[mappedField.Value] as TaxonomyField;

                        // Gets the current taxonomy session
                        TaxonomySession session = new TaxonomySession(web.Site, false);

                        // Gets the term store (by SspId)
                        var termStoreCol = session.TermStores[managedField.SspId];

                        // Gets the terms of a specific term set (by TermSetId)
                        var termCol = termStoreCol.GetTermSet(managedField.TermSetId).Terms;

                        var listTerms = new List<Term>();

                        // Iterates through the lookup values
                        foreach (var itemValue in lookupValueList)
                        {
                            string value = itemValue.LookupValue;

                            // Gets the correspondent term for the each value
                            // found in the lookup values list
                            var termToSet = termCol[value];

                            listTerms.Add(termToSet);
                        }

                        // Sets the field value using the list of terms
                        managedField.SetFieldValue(item, listTerms);

                        // Persists the item
                        item.SystemUpdate();

                        // Denies further unsafe updates
                        web.Site.AllowUnsafeUpdates = false;
                    }
                }
            }
        }
    }
}




Comments

  1. where are you getting your XMLHelper class from?

    ReplyDelete

Post a Comment

Popular posts from this blog

Site Logo Not Changing on Web Part Pages

I tested and reproduced your issue in my local machine. Since the Web Part Pages would override the content in PlaceHolderPageTitleInTitleArea place holder, the site logo would not change automatically. So would you please try remove or comment the following control TitleBarWebPart: See the similar scenario and solution: http://emanonsolutions.blogspot.com/2010/02/left-navigation-webpart-pages.html Hope this can help.

Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010

·          Web Templates ·          Site Definitions and Configurations ·          Deciding Between Custom Web Templates and Custom Site Definitions ·          Understanding Onet.xml Files ·          How to: Create a Custom Web Template ·          Overview of Creating Custom Site Definitions Site Template Configurator utility http://stefan-stanev-sharepoint-blog.blogspot.com/search/label/SharePoint%202010 Create a Custom Site Definition with Additional Content in SharePoint 2010 Using Visual Studio 2010 http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/11/11/sharepoint-2010-cookbook-how-to-create-a-custom-site-definition-with-additional-content-in-sharepoint-2010-using-visual-studio-2010.aspx http://blogs.msdn.com/b/allenwang/...

SPFx Fantastic 40 Web Parts

SPFx Fantastic 40 Web Parts Ref Link :  https://github.com/OlivierCC/spfx-40-fantastics Menu & Carousels & News Management Overview Web Part Description News Carousel Insert a classical, responsive, cool & touch ready News Carousel. With this web part, you can add easily news focus in your SharePoint site. The users can easily navigate in news items, with buttons or with touch. Tiles Menu This Web Part allows you to very easily create a menu in form of tiles that is responsive and adapted for mobile. You can directly manage the items on your menu, with a title, an image and manage Visual rendering options. 3D Carousel Insert a 3D Carousel in your SharePoint pages. With this Web Part, you can manage your menu items and create automatically a 3D carousel. Coverflow Generates a Coverflow Apple like menu in your pages. Manage your menu items with title and picture and create a cool coverflow menu. News Slider Insert a News Slider Tiles control to your pages....