Term update failed because of save conflict SharePoint Online

In this SharePoint PnP tutorial, We will discuss how to solve “Term update failed because of save conflict” error which comes while creating terms in SharePoint Online using SharePoint PnP core library.

Recently, I was working with PnP SharePoint in an ASP.NET Web Application in Visual Studio 2017. There, our requirement was to create a term set, terms, etc. for SharePoint global navigation.

Learn SharePoint term store.

If you are new to PnP SharePoint, have a look at this article: SharePoint Online Development using Patterns and Practices (PnP)

To add this term navigation, I have written CSOM code as [childTerm.GetTaxonomyTerm().TermStore.CommitAll();] within the foreach() loop.
While I was running this CSOM code, an error occurred as “Term update failed because of save conflict“. You can see the error message in below screenshot.

term update failed because of save conflict
Term update failed because of save conflict SharePoint online

This below CSOM code is what I have written within the foreach() loop.

try
                {
                    foreach (ImportTerms level1TermItem in level1TermItems)
                    {
                        childTerm = termCategory.CreateTerm(level1TermItem.Level1Term, 

NavigationLinkType.SimpleLink, Guid.NewGuid());
                        childTerm.SimpleLinkUrl = "/sites/Bhawana/Pages/Product.aspx?c=" + 

level1TermItem.Level1Term;
                       childTerm.GetTaxonomyTerm().TermStore.CommitAll();
                    ctx.ExecuteQuery();
                    }
                    
                }
                catch (Exception ex)
                {
                } 

Term update failed because of save conflict

The Solution of this above error is: You should write the CSOM code [childTerm.GetTaxonomyTerm().TermStore.CommitAll();] outside of the foreach() loop.

Once you will write the above code outside of the loop and again run the code, then it will execute and give the appropriate result.

Follow the below-tested CSOM code:

try
                {
                    foreach (ImportTerms level1TermItem in level1TermItems)
                    {
                        childTerm = termCategory.CreateTerm(level1TermItem.Level1Term, 

NavigationLinkType.SimpleLink, Guid.NewGuid());
                        childTerm.SimpleLinkUrl = "/sites/Bhawana/Pages/Product.aspx?c=" + 

level1TermItem.Level1Term;
                    }
                    childTerm.GetTaxonomyTerm().TermStore.CommitAll();
                    ctx.ExecuteQuery();
                }
                catch (Exception ex)
                {
                } 

Term update failed because of save conflict

Another very useful solution I got and it’s a brilliant solution from sadomovalex where I have added the code in a do while loop and it worked perfectly as per the requirement.

So you can put the term store csom code inside a do while loop.

NavigationTerm childTerm = null;
                foreach (ImportTerms level1TermItem in level1TermItems)
                {
                    do
                    {
                        try
                        {
                            childTerm = termBrand.CreateTerm(level1TermItem.Level1Term, NavigationLinkType.SimpleLink, Guid.NewGuid());
                            childTerm.SimpleLinkUrl = "/sites/Bhawana/Pages/Product.aspx?c=" + level1TermItem.Level1Term;
                            childTerm.GetTaxonomyTerm().TermStore.CommitAll();
                            ctx.ExecuteQueryRetry();
                            break;
                        }
                        catch (Exception)
                        {
                           
                        }
                    } while (true);
                }

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we discussed the solution of an error as “Term update failed because of save conflict” using CSOM. By using this above solution, You can overcome this type of issue.

>