Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to migrate a set of list columns to managed metadata columns. As part of this process, I have created a set of metadata columns parallel to the existing list columns, suffixed with the letter 'M'. I get references to the term store and term I wish to set, but when calling TaxonomyField.setFieldValue(), SharePoint hangs. The relevant parts of the code are given below. If this is relevant, this specific code is run through the debugger in the Page_Load event handler of an applciation page.

            SPList l = web.Lists["Testlib"];

            TaxonomySession oSession = new TaxonomySession(SPContext.Current.Site);
            TermStore oTermStore = oSession.TermStores["Metadata Service Application Proxy"];
            Group oGroup = oTermStore.Groups["Reports"];

            TermSet years = oGroup.TermSets["Year"];

            foreach (SPListItem i in l.Items)
            {

                // If year non-null, move value from Year to YearM
                if (i["Year"] != null) {
                    string index = i["Year"].ToString();
                    Term thisYear= years.Terms[index];                        

                    TaxonomyField oField = (TaxonomyField)i.Fields["YearM"];

                    web.AllowUnsafeUpdates = true;
                    oField.SetFieldValue(i, thisYear); // Sharepoint hangs here
                    web.AllowUnsafeUpdates = false; 
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

You have to add a TaxonomyFieldValue in stead of a Term, something like this:

string termString = String.Concat(thisYear.GetDefaultLabel(1033), TaxonomyField.TaxonomyGuidLabelDelimiter, thisYear.Id);

TaxonomyFieldValue tagValue = new TaxonomyFieldValue(string.Empty);
tagValue.PopulateFromLabelGuidPair(termString);
oField.SetFieldValue(i, tagValue);
share|improve this answer
    
Thank you very much, this works. If this is relevant, I think I stumbled onto a weird bug in the process..when running my code, for some reason an "Updates not allowed over GET requests" exception pops up, regardless of whether I set the SPWeb to allow unsafe updates or not. This error does not happen when running your code. –  Geir Smestad Sep 13 '11 at 12:22
    
This is not a good solution. It will break the Search Crawl!!! –  Hristo Yankov Jan 15 at 13:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.