Thursday, May 17, 2012

Fix “Property does not exist” error when add search scope rule in Sharepoint

If you configure search scope based on some managed property (e.f. ContentType) via PowerShell using New-SPEnterpriseSearchQueryScopeRule cmdlet, e.g. using the following code:

   1: New-SPEnterpriseSearchQueryScopeRule -Scope $scope -RuleType $ruleType -FilterBehavior
   2: $filterBehavior -ManagedProperty $parameter -PropertyValue $value -Url
   3: $url -SearchApplication $searchApplication -Confirm:$false

You may get the following error:

Property "ContentType" does not exist

In order to fix it go to Central Administration > Service applications > Search service application > Metadata properties > Edit "Content type" property (or property which you use) > Check checkbox "Allow this property to be used in scopes":

image

After this New-SPEnterpriseSearchQueryScopeRule cmdlet should work successfully.

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.