Thursday, 14 March 2013

Displaying the right data in the Content Search web part in SharePoint 2013

Articles in this series:

So, let’s say you’re working with the Content Search web part in SP2013 and you’ve got it displaying the correct items. (If not, see my big post on Using the Content Search web part (and understanding search). In the case of this article, I’m searching for items I’ve created and tagged with “SharePoint”. Whatever the query, you’ll most likely be confronted by something like this:

Default Content Search web part presentation
Your client will be thrilled with something so aesthetically pleasing! By the way, the above search is for “documents in my SharePoint farm tagged with SharePoint” -  having a thumbnail image shown might not really be needed for documents, but just for fun we’ll talk about both options. In a later post, I’ll cover using the CSWP to roll up some news pages (publishing pages) – these do tend to benefit from thumbnails, but the good news is there’s less work to get them there.

Anyway, if we break down the issues we’d like to fix, we would say:

  • An image is rendered for each item, but one is not found (so a default image is used)
  • The fields may not be correct – certainly for the 2nd and 3rd items, a fairly useless value is being fetched from the Office document properties. This is being displayed in the absence of a ‘Description’ field on the list item (N.B. this is actually a great ‘fallback’ feature of SP2013 search – Office doc properties get pulled into the property bag of the list item, and indexed by search if no other values take precedence [via Managed Property mappings]), but in this case, it’s some irrelevant data about who created the original PowerPoint template for the conference I presented at:
    Office doc - comments field 
  • The overall formatting (HTML and CSS, especially colors) do not match our desired rendering

There are several settings in the CSWP web part properties at work here:

Thing Screenshot Notes
Control Display Template CSWP - selecting display template This determines the overall style of the presentation – it’s effectively the “outer HTML” that is output.

I’d say List will be the most commonly-used value, but it’s great to see List with Paging come for free. Slideshow is an image rotator.
Item Display Template CSWP - selecting display template item This dictates the style applied to each item displayed i.e. “the HTML for each item”. Later when we create a new Item Display Template, we’ll see this become available for selection in this dropdown.
Property Mappings CSWP - property mappings These are very much like ‘slots’ in the Content Query web part – they are a feature of the Item Display Template which is selected (specified in JavaScript). Item templates use generic  properties (e.g. ‘Picture URL’, ‘Line 1’ etc.), and you can ‘map’ some specific fields which exist on your list item. The image shows the default mappings. This means that if you only need to get a different field displayed in an existing slot, you can do this without modifying XSL (CQWP) or JavaScript (CSWP).

If you needed 10 slots rather than 5, editing the Item Display Template’s JavaScript controls this.

Note that can specify multiple values specified by a semi-colon (as shown).

 

Choosing the Item Display Template

As you change the selected Item Display Template in the dropdown, the Property Mappings section in the CSWP tool pane will change accordingly, allowing the mapping for each slot. Once configured appropriately (see next section), “Picture on left, 3 lines on right” can have 3 lines of information displayed:

Content search web part - picture 3 lines

Whereas “Two lines” of course, will give you no picture and just two lines:

Content search web part - 2 lines
We’d probably say that “Two lines” works just fine for documents – unless you do happen to want thumbnails (e.g. for some key policies/procedures/CAD files/PDFs/whatever). Whilst thumbnails kinda happen by default for publishing pages, the process for other items is useful for illustrating the ‘Property Mappings’ aspect – first let’s cover the possibilities for working with these mappings. 

Deep dive - getting the right data coming through

If you haven’t guessed already, those property mappings such as ‘PublishingImage’, ‘Path’, ‘Title’ and ‘Description’ above are Managed Properties in the search service app. If you are rolling up content from multiple locations (Content by *Search* remember!), consider that these mappings allow you to show results from across your farm/tenant  even if you don’t have standardized content types with the same fields everywhere. For example, perhaps ‘Description’ could really come from a custom field named ‘Project Description’ in one place but ‘Task Description’ for other items.

The use of Managed Properties means you might need to go down a different route to get your desired values coming through:

Option 1 – no work needed, the CSWP automatically displayed my data where I want it!

Awesome! You’ll see this for the ‘Title’ field and some others (e.g. roll-up images in publishing pages). This is happening because for this ‘slot’ in the selected item display template, the default Property Mapping contains a Managed Property which points to the field on the list item/content type which holds the data. Perfect. If this isn’t happening and you think it should be, check that you have populated some content and it has been successfully crawled – you won’t get anywhere until it has.

Option 2 – use an existing Managed Property and site column, but edit the CSWP properties for a particular slot (to point to this Managed Property). You would do this when the CSWP isn’t looking at this Managed Property by default  for this slot:

It’s a big old dropdown, but all you have to do is select the existing Managed Property you want:

Content search web part - selecting property mapping

Additionally you can add to the semi-colon separated list of Managed Properties, by typing into the box.

Option 3 - use a new site column but add it to an existing Managed Property:

Rare maybe, but you might want to do this if you want the same mapping to be use in different places e.g. multiple CSWP instances, or CSWP and the search results page etc. The process would be:

  1. Add a new column to your list/library/content type. Populate some content and perform a full/incremental crawl.
    1. Find an existing, somewhat generic, Managed Property which represents the thing you are working with – this could be something like ‘Title’ or ‘Description’. Edit the Managed Property so that the corresponding Crawled Property for your field (e.g. ‘ows_COBDescription’ for a field named ‘COBDescription’) is added to the list of Crawled Properties. 
  2. Ensure your Crawled Property is higher in the mappings list than others (if using the ‘Include content from  the first crawled property that is not empty..’ option. As an illustration:
           
    Configure Managed Property

Option 4 – use an entirely custom Managed Property and site column:

This might be the best approach if there are no appropriate fields in existence, or you really don’t want to pollute any other CSWP instances and/or the search results page with these settings. I used this approach to add thumbnails to my documents when using “Picture on left, 3 lines on right”. The process would be:

    1. Add a new column to your list/library/content type. Populate some content and perform a full/incremental crawl.
    2. Create a new Managed Property, and map it to the corresponding Crawled Property for your field (e.g. ‘ows_COBThumbnailUrl’ for a field named ‘COBThumbnailUrl’).
    3. Edit the Content Search web part properties, so that the Property Mappings for the desired slot contain the name of the Managed Property – here I’ve added my Managed Property (‘COBThumbnailUrlManaged’) to the ‘Picture URL’ slot:

      CSWP - property mappings updated 

In general, the deciding factors will be things like:

  • Do I want this change to take effect everywhere? (editing an existing Managed Property [i.e. adding a new Crawled Property to it] could mean that other Content Search web parts/search results show data from an instance of this column)
  • Is there an existing field and/or Managed Property which could represent this data?

“CAD files/documents with thumbnails (in team sites)” example

Recently I had to implement thumbnails for CAD files for a client demo – to appear in both CSWP results and the search hover panel (I’ll talk about customizing the search hover panel another day). The thumbnails aren’t automatically generated, but could be in the future. At it’s core, there’s an additional column which contains a hyperlink to the thumbnail image (e.g. in a Site Assets library).

I chose option 3 above in this case – since I don’t need to ensure I’m scoping only to one CSWP instance or anything.

Here are the specific changes I made to get my thumbnails and descriptions to come through to the CSWP:

  • Added two columns to my content type, to contain the thumbnail URL and description
  • Populated these fields for each item (for the thumbnails, as mentioned the images themselves live in the local Site Assets library)
  • Item thumbnail:
    • I created a site column (‘COBThumbnailUrl’), and mapped this to the ‘PictureThumbnailURL’ Managed Property
  • Item description (for display in the ‘Line 2’ value):
    • In this case I re-used the OOTB ‘Description’ Managed Property, but added a new column ‘COBDescription’ and included this in the Crawled Properties mapped to that Managed Property
    • (N.B. There are a bunch of OOTB fields named ‘Description’ in a team site, but most are in the ‘Hidden’ group and didn’t actually seem suitable for re-use)

Once the thumbnail is against that file, it’s fairly easy to surface it in search hover panels (as well as CSWP rollups):

Customized hover panel for CAD files

“Publishing pages with thumbnails” example

If the items you’re rolling up are publishing pages (e.g. news pages), then things are slightly simpler – this is because the default mappings of, say, “Picture on left, 3 lines on right” work better. Assuming you’ve configured the query correctly and the pages have a roll-up image set (e.g. a rendition of an existing image), then you’ll probably get thumbnails but no descriptions by default:

Content search web part - default publishing output

In this case, you might notice that my thumbnails aren’t high quality. This is because they are simply generated using the SP2013 publishing framework’s image renditions capability (i.e. a 100 x 100 version of the much larger image displayed on the page itself) – if I produced special images for thumbnails, the result would be higher quality (but more work).

We have no item descriptions so far, only titles. However, if we edit the Property Mappings for “Line 2” and “Line 3”, we can start to bring in other data – here I’m selecting the article byline and also the property which lists which terms this item has been tagged with:

Content search web part - editing mappings for publishing pages 
..and now we get the those details displayed by the Display Template:

Content search web part - enhanced publishing output

Next considerations – modifying the HTML/CSS/JS, and packaging/deployment

So far, we haven’t worried about changing the actual HTML which is output. My next articles look at this, for both publishing and non-publishing scenarios (the process is slightly different).

There are also a couple of considerations for working the “developer way” and provisioning these artifacts through a WSP. I’ll also discuss these next time.

6 comments:

Anonymous said...

Hi,

I've encountered a problem when deploying content search web part using solution to my custom application page with custom display template. For some reason custom template is not applied after deployment. Instead default is applied. I've created a thread on technet for this one but unfortunately there is no answers yet.
http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/b9591996-2081-4f53-adf5-21da64c60a03
Can you help me with this?

Chris O'Brien said...

Hi,

I'm not sure if this is the same problem, but I've certainly seen an issue when trying to add the Content Search web part to a page using the AllUsersWebPart element in a Feature. I think it could be a bug in SP2013, and have a blog post half-written on this. I'll publish in the next few days, but in short you have to find some way to work around it unfortunately.

Cheers,

Chris.

Anonymous said...

Hi,

Thanks for your reply!

I've found workaround on this issue but i think not all will like it - i've opened page with content search web part on it using sharepoint designer, copied web part definition and added it directly to page layout. This way it seems to work.

Anonymous said...

I love this web part and am currently working on a cross-site collection navigation solution (Managed navigation is nice but it's not security trimmed) using this but having a hard time defining the site hierarchy using either the query rules or the display templates. Any suggestions?

KarenC said...

Hi Chris,

Many thanks for your blog! How where you able to display an image to the hover for content search display template?

I am able to display it on the search results hover, but not on content search. Not sure what I am missing!

Many thanks!!

Chris O'Brien said...

@KarenC,

To do this, I implemented a fairly simple tweak to the display template. The images are uploaded separately by the author (they aren't auto-generated), and then I have an 'img' tag which uses a property of the file to build the full path to use for the 'src' attribute.

Hope that helps!

COB.