Updating a managed metadata column from choice column value

I am trying loop through all lists in a web, and for each list, 

1. create a managed metadata column->Working

2. Pull value from choice column->Working

3. Move choice column value to managed metadata column->Not Working

Point number 3 is not working. Please advise

Here is the code:

        static void Main(string[] args)
        {

            const string SPLocationListColumn = "CHECK6";

            SPSite site = new SPSite("http://sp2010:8080");

            SPWeb rootweb = site.RootWeb;

            Microsoft.SharePoint.Taxonomy.TaxonomySession taxonomySession = new Microsoft.SharePoint.Taxonomy.TaxonomySession(site);

     
            TermStore termStore = taxonomySession.TermStores["Managed Metadata Service"];
     
            Console.WriteLine(termStore.Name);

            Group group = termStore.Groups["KM Metatags"];
            Console.WriteLine(group.Name);

            TermSet termSet = group.TermSets["Document Classification"];
           
            Guid termsetid = termSet.Id;
            Console.WriteLine(termSet.Name);

            Term term = termSet.Terms["Document subclassification"];
            TermCollection terms = termSet.Terms;
            Console.WriteLine(term.Name);
            SPWebCollection collWebsite = site.AllWebs;


            for (int i = 0; i < collWebsite.Count; i++)
            {

                using (SPWeb oWebsite = collWebsite[i])
                {

                    SPListCollection collList = oWebsite.GetListsOfType(SPBaseType.DocumentLibrary); ;

                    for (int j = 0; j < collList.Count; j++)
                    {


                        SPList list = collList[j];
                        if (list.Title == "Documents")
                        {
                            Console.WriteLine(list.Title);
                            TaxonomyField field = list.Fields.CreateNewField("TaxonomyFieldType", SPLocationListColumn) as TaxonomyField;

                            field.SspId = termSet.TermStore.Id;
                            Console.WriteLine(termSet.TermStore.Id);
                            field.TermSetId = termSet.Id;
                            Console.WriteLine(termSet.Id);
     
                            field.AnchorId = Guid.Empty;



                            try
                            {
                                Console.WriteLine("Entering");

     
                                Console.WriteLine("Entering");
                                list.Fields.Add(field);
                                Console.WriteLine("Entering");


                                Console.WriteLine("Entering1");
                                SPView view = list.DefaultView;
                                Console.WriteLine("Entering2");
                                list.Update();
                                SPViewFieldCollection collViewFields = view.ViewFields;
                                collViewFields.Add("CHECK6");
                                Console.WriteLine("Entering3");
                                view.Update();
                                Console.WriteLine("Entering4");

                            }
                            catch (Exception e1)
                            {

                                Console.WriteLine(e1.Message);
                            }

                        }
                        for (int f = 1; f < list.ItemCount; f++)
                        {
                            if (list.Title == "Documents")
                            {
                                Console.WriteLine(list.Title);
                                SPListItem item = list.Items[f];

                                if (item.Fields.ContainsField("subclass"))
                                {

                                    SPField field9 = item.Fields["subclass"];
                                    String subclassvalue = field9.GetFieldValueAsText(item["subclass"]);
                                    Console.WriteLine(subclassvalue);
                                    TaxonomyField taxonomyField = item.Fields["CHECK6"] as TaxonomyField;
                                    TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
     
                                    taxonomyFieldValue.TermGuid = term.Id.ToString();
                                    taxonomyFieldValue.Label = subclassvalue;
                                    
                                    taxonomyField.Update(); 
                                    item.Update();
                                    list.Update();


                                }
                            }

                        }
                    }
                }
            }
        }

January 17th, 2014 5:44am

Well it's a pain to read your code as it doesn't have comments.

On the other hand I suspect i know where you're going wrong. The taxonomy terms are cached in a hidden list when they are used. When setting taxonomy terms programatically SharePoint will assume that the terms have already been cached and will fail if they have not. This post by Nick Hobbs is very informative. http://nickhobbs.wordpress.com/2012/02/21/sharepoint-2010-how-to-set-taxonomy-field-values-programmatically/

Free Windows Admin Tool Kit Click here and download it now
January 17th, 2014 6:58am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics