Broken Content By Search Web Part

contentbysearchquery-witherror

I’ve been working with Managed Metadata Navigation and the Content Search Web Part a lot lately in SharePoint 2013. I just recently ran into a nasty little bug with a site that had been upgraded from SharePoint 2010.

Problem

The Content Search Web Part just wouldn’t work. Preview worked great but as soon as I published the page the web part was blank. So I opened the page up in Chrome from some better debugging tools and found the following:

A JavaScript error was preventing the results from showing. Specifically “Uncaught TypeError: undefined is not a function “. I really recommend reading through my troubleshooting steps but if you’re lazy then just skip to the resolution.

Troubleshooting

First thing I did was see if I could reproduce the issue in a completely fresh site collection. So I created a new publishing site collection and everything worked no problem. So something is wrong with my site collection, not with my farm.

Ok, now what? Now we start troubleshooting. Since SharePoint 2013 now uses so much JavaScript, Script On Demand and Client Side functionality I should probably blog my full troubleshooting steps since it will probably be very helpful if you’re normally a server side developer that is now living in a client side world.

Step 1 – Get a Stack Trace

Next, in the developer tools for Chrome I selected to break on all errors and to use “pretty print” for the JavaScript and refreshed the page. (Pretty print essentially de-minifies the JavaScript during debug time).

Perfect, now we have a call stack.

Step 2 – Find the Source of the Problem

Tracing the request backwards I as able to find that the error originated with the following function:

Sys.Application.add_init(function() {
    $create(Srch.ContentBySearch, {"delayLoadTemplateScripts": true,"itemTemplateId": "~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Item_Picture3Lines.js","messages": [{"serverTypeId": "{D3DFEF63-4D44-497D-B936-047135645AD7}","header": null,"level": 2,"code": -1,"type": null,"messageDetails": "This Web Part is not available for use. Search-Driven content (592CCB4A-9304-49AB-AAB1-66638198BB58) feature needs to be activated to use this Web Part.","messageDetailsForViewers": "","stackTrace": "","correlationID": "71bb119c-c5ae-30b7-f710-4ed25519004f","showInEditModeOnly": false,"showForViewerUsers": false,"encodeDetails": true}],"numberOfItems": 50,"queryGroupName": "Default","renderTemplateId": "~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Control_ListWithPaging.js","states": {}}, null, null, $get("ctl00_ctl39_g_4aefb98d_fd13_4866_82ec_228fc748aab5_csr"));
});

And a little work with the command line revealed that Srch.ContentBySearch was undefined.

Step 3 – Verify my Findings

I then verified that I was on the right path by confirming that Srch.ContentBySearch SHOULD and DOES have a value in my working site collection by using the same console command in my working site collection. Ok, how about why? Now we need to start searching through the requests to find which resource file declares this function.

By doing the above search on the Resource tab I was able to determine that this function is declared inside of “search.cbs.js” which should be getting referenced by “default.aspx” which is the page itself.

I then went back to my broken site and checked the resources list. Sure enough search.cbs.js is never loaded.

Step 4 – Fix with a Hack

So I crack open SharePoint Designer and I force that JavaScript file to load by modifying the master page and inserting the following script reference into the HEAD of the page.

<script type="text/javascript" src="/_layouts/15/search.cbs.js"></script>

Now we try again… and BOOM we finally get a useful error message with an actual correlation ID.

Now I’m thrilled because this actually makes sense. So let’s undo the hacked in changes to our master page and actually fix this thing.

The Real Resolution

The real solution is that the “Search-Driven content” feature was not activated for my Site Collection so even though the Content Search Web Part was available the necessary JavaScript was never loaded into the page. Beware of this issue in your upgrades from SharePoint 2010 as I’m pretty sure that is how this happened to me.

To fix this issue just run the following PowerShell:

enable-spfeature -Identity 592CCB4A-9304-49AB-AAB1-66638198BB58 -Url https://urltoyoursitecollection

Refresh the page and HURRAY!
 

I'm a public speaker and the Chief SharePoint Architect for Eastridge, a Microsoft Gold Partner specializing in SharePoint and custom application development in Winston-Salem, NC. I focus on the SharePoint platform with a specialty in Information Architecture, Publishing and Best Practices.

Got something to say? Go for it!