Search, SharePoint, Technical

Duplicate Key Error when Editing a Content Search Web Part

When developing custom display templates for your CSWPs, be careful when setting the property value for ManagedPropertyMapping. This property is used to tell the template how to map managed properties to properties within the template, so you can pull them in and display the property. If you add the same property more than once, you won’t be able to edit the web part properties of CSWPs in the site collection where the display template is located. You will get this error in ULS:

System.ArgumentException: An item with the same key has already been added.    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)     at Microsoft.Office.Server.Search.WebControls.ResultScriptWebPart.ParsePropertyMappingsString(String mappings)     at Microsoft.Office.Server.Search.WebControls.ContentBySearchToolPart.get_MaxSlots()     at Microsoft.Office.Server.Search.WebControls.ContentBySearchToolPart.CreatePropertySlotControls()     at Microsoft.Office.Server.Search.WebControls.ContentBySearchToolPart.CreatePropertyMappingsSectionControls()     at Microsoft.Office.Server.Search.WebControls.ContentBySearchToolPart.CreateSections()     at Microsoft.Office.Server.Search.WebControls.ContentBySearchToolPart.CreateChildControls()     at System.Web.UI.Control.EnsureChildControls()     at Microsoft.Office.Server.Search.WebControls.ContentBySearchToolPart.OnLoad(EventArgs e)     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

When I say to not add the same property more than once, I mean with the same property name as specified within the template. Not the same managed property. In other words, it’s ok to map the same managed property to more than one template property:

<mso:ManagedPropertyMapping msdt:dt=”string”>’Line 1′{Line 1}:’Title’, ‘Line 2′{Line 2}:’Title'</mso:ManagedPropertyMapping>

But you can’t create two template properties with the same name, like this:

<mso:ManagedPropertyMapping msdt:dt=”string”>’Title’:’Title’, ‘Title’:’Path'</mso:ManagedPropertyMapping>

Those were simple examples, but as you add more and more properties, you could lose track. Don’t.

5 thoughts on “Duplicate Key Error when Editing a Content Search Web Part

Leave a Reply

Your email address will not be published. Required fields are marked *