Saturday, May 12, 2012

Problem with creation of managed metadata term with predefined term id in Sharepoint

Using Sharepoint object model you can create managed metadata term in your term set with predefined name and term guid (overridden CreateTerm method). E.g. using PowerShell it can be done like this:

   1: $site = Get-SPSite "..."
   2:  
   3: $taxonomySession = Get-SPTaxonomySession -site $site
   4: $store = $taxonomySession.TermStores["Managed Metadata Service"] 
   5:  
   6: $group = $store.Groups["MyGroup"]
   7: $termset = $group.Termsets["MyTermset"] 
   8: $termset.CreateTerm("Foo", 1033, [Guid]"{8272295a-288e-4aac-9da6-ad733c6ecc66}")
   9:  
  10: $store.CommitAll()
  11: $site.Dispose()

In one of the projects when I tried to execute similiar script I encountered with the following error when call CommitAll() method:

Failed to read from or write to database. Refresh and try again. If the problem persists, please contact the administrator.

Sharepoint log had more details including stack trace and internal exception:

Exception returned from back end service. System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: TermStoreEx:Failed to read from or write to database. Refresh and try again. If the problem persists, please contact the administrator. (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: Microsoft.SharePoint.Taxonomy.TermStoreOperationException: TermStoreEx:Failed to read from or write to database. Refresh and try again. If the problem persists, please contact the administrator. ----> System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.ECMTerm' with unique index 'IX_ECMTerm_TermGUID'. The duplicate key value is (7acd1222-a19a-4c46-866e-f840c3f0a48b, 8272295a-288e-4aac-9da6-ad733c6ecc66).  The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReade...).

As it shows the exact error is that there already was term with the same id in the managed metadata table (Cannot insert duplicate key row in object 'dbo.ECMTerm' with unique index 'IX_ECMTerm_TermGUID'). I checked whole solution for occurrence of specified id and found that it was used already in another feature which provisions term sets. So I changed id in the PowerShell and after that it became working. Hope it will help you if you will encounter with the same issue.

4 comments:

  1. Yeah you are right, that works all well. There is a specific scenario where if I tag some list items with the new term created and that item already has more terms tagged (using a multi-select metadata type column), then on trying to migrate from stage to production, the old terms already associated with the list item get pruned off and only the new term is left.


    have you come across something like that? Any ideas?

    P.S. the new term has already been created and migrated to the taxonomy hidden list on live system all well.

    ReplyDelete
  2. Rajni,
    I didn't faced with your problem before, but there was a problem when we tried to use OTB SPExport/Import for copying content across the sites: http://sadomovalex.blogspot.fi/2011/09/using-spexportspimport-for-copying.html. How you copy content from staging to production?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  3. Thanks Alex for writing back. I looked at the link, but have not faced such an issue so far.

    Owing to some special needs, we use the codeplex API which is a wrapper over the SP's Export API.

    The verbose logs in migration show the item migrated all well, but the existing terms do not migrate whenever the new term is used in conjunction with the already existing terms.

    We have been able to replicate it on a test environment as well. But looks like something to do with some content / permission issues.
    Will let you know if we do further progress on it!!

    ReplyDelete