Pages

Tuesday, December 14, 2010

SharePoint 2010: Create Taxonomy Error “Term set update failed because of save conflict.”

I was trying to answer a user’s problem in MSDN forum. He was trying to add a term in term store. Using the term adding code (that I have taken from the post) I was getting the error “Term set update failed because of save conflict.” while I was calling the CommitAll method.

 

Problematic Code

My code is as shown below:

public static void AddTerminTermStoreManagement(string siteUrl, string termSetName, string Term)
{
    try
    {
        using (var siteTerm = new SPSite(siteUrl))
        {
            var sessionTerm = new TaxonomySession(siteTerm);
            var termStoreTerm = sessionTerm.DefaultSiteCollectionTermStore;
                    
            var collection = termStoreTerm.GetTermSets(termSetName, 1033);
            var termSet = collection.FirstOrDefault();

            if (!termSet.IsOpenForTermCreation)
            {
                termSet.IsOpenForTermCreation = true;
            }

            termSet.CreateTerm(Term, sessionTerm.TermStores[0].DefaultLanguage);
            termStoreTerm.CommitAll();
        }

    }
    catch
    {

    }

}

After investigating the code I had found the problem was in the line as shown in the above code snippet with yellow marker (termSet.IsOpen..).

 

Solution

I have fixed the error by modifying the code as shown below. I had just called the CommitAll just after changing the value of IsOpenForTermCreation as shown below:

public static void AddTerminTermStoreManagement(string siteUrl, string termSetName, string Term)
{
    try
    {
        using (var siteTerm = new SPSite(siteUrl))
        {
            var sessionTerm = new TaxonomySession(siteTerm);
            var termStoreTerm = sessionTerm.DefaultSiteCollectionTermStore;
                    
            var collection = termStoreTerm.GetTermSets(termSetName, 1033);
            var termSet = collection.FirstOrDefault();

            if (!termSet.IsOpenForTermCreation)
            {
                termSet.IsOpenForTermCreation = true;
                termStoreTerm.CommitAll();
            }

            termSet.CreateTerm(Term, sessionTerm.TermStores[0].DefaultLanguage);
            termStoreTerm.CommitAll();
        }

    }
    catch
    {

    }
}

 

Conclusion

So the problem was that when I changed the value of IsOpenForTermCreation, there was a commit pending. So without committing, I created a new term and tried to committed. So the save conflict error was thrown. If you get the same error in different scenario then u can check if u have any pending commit that u have not committed.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.