Recently I had the need to build a custom SharePoint 2010 search results page to allow users to preview videos and audio files stored in SharePoint and display them inline with the search results and in this posting I'll document how I acheived this. First take a look at the finished article and see what we'll be heading towards.
The task can be broken down into these steps:
Lets's get started......
Create assets document library to hold media.
Here we are going to create a regular document library based on the "Assets" template.
When you now browse to your MediaAssets document library you should be able to see each of your assets represented by the thumbnail image. Hover over one of these assets and you'll see a pop-up appear with details of the file. Click Play to watch/listen to the file.
Create search page and search results page.
In this part we are going to create a custom search and search results page. The search page allows us to provide additional search terms to SharePoint (to limit the results to only media assets) and the results page will contain the custom XSL we require to format the results as we see fit.
This custom search page and search results page don't do anything which the standard "All Sites" search doesn't do. Let's add something new....
Test your work so far by performing a search from your custom search page (http://XXXXXX/search/Pages/mediaassets.aspx) and searching for a word which is in both the metadata for one of your videos/mp3s etc AND on a regular SharePoint papge (like a wiki page). You should only see results from document libaries based on the Assets document library template. The reason this works is that in addition to what ever keyword you searched for, the Search Core Web Part automatically appended "contentclass:STS_ListItem_851" to the query we configured in step 3 above (a user won't see this in the search box or the url in the top of the browser as "Append Text to Query" entries are always hidden).
The obvious question is "what does contentclass:STS_ListItem_851 mean"?
Contentclass:XXXXXXX is a standard query we can pass to SharePoint to have it return content which is of a particular type or class.
The type or class usually relates to where to content is stored so list items stored in an Announcements list are one class, those in a Links list are another. For the developers amongst you, this MSDN page should list the "base" content classes for you: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx
From reading that page, you could infer that STS_ListItem_104 means "items which are stored in an Announcements list". In fact because the Annoucements list is one of the core SharePoint lists, it can also be known by a friendly name, ie, STS_List_Announcements (you can get a flavour of these friendly names at http://msdn.microsoft.com/en-gb/library/dd584327(office.11).aspx. - I wasn't able to find a SP2010 equivalent).
So what about our STS_ListItem_851, where does that come from?
As with the annoucements example above, it means "items in lists based on list template 851". The 851 I found out by opening the template which defines the Asset library, i.e.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\AssetLibrary\AssetLibraryTemplate.xml
and looking at the XML inside:
<!-- _lcid="1033" _version="14.0.4750" _dal="1" --><!-- _LocalBinding --><Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ListTemplate Name="AssetLibrary" Type="851" BaseType="1" SecurityBits="11" OnQuickLaunch="TRUE" DisplayName="$Resources:cmscore,List_AssetLibrary_DisplayName" Description="$Resources:cmscore,List_AssetLibrary_Description" Image="/_layouts/images/ital.png" DocumentAsEnclosure="TRUE"> </ListTemplate></Elements>
Note the Type="851". This is the same 851 which makes up part of our contentclass:STS_ListItem_851. PS. The Assest template does not have a friendly name to my knowledge.
Create required managed properties.
In the previous section we saw the use of the contentclass:XXXXX query for limiting search results to a particular class. Contentclass is an example of what is called a Managed Property and SharePoint already has lots of pre-defined managed properties shipped out of the box, eg. fileextension:docx will return all documents which have a .docx extension. It's also possible to create your own managed properties which map to a column on a list or a site column which could be used by many lists. This would allow you to provide a search like Location:London to find all documents which have "London" in a particular list or site column.
As well as allowing us to request documents with certain attributes, managed properties also provide a convenient way to refer to those columns in results returned from the search engine. For example when formatting a list of results, we can make runtime evaluations of managed properties to say something like "If Location=London then display the result this way, else display it that way". We need to specifically tell the search engine we want the metadata relating to each result and we do this by supplying some XML detailing those properties we need. More on this later.
Now don't assume just because you create a list or site coulmn called "London" that you can automatically use that column name in a query, you can't.
Let's take a look at how to set one up to that you can.
Why did we choose "ows_AlternateThumbnailUrl(Text)"? When you added the Preview Image Url for each uploaded video or mp3, the actual URL got stored in a site column called AlternateThumbnailUrl. The "ows_" bit is automatically added to the column name so we know this is a SharePoint column (as opposed to columns of data from other external systems SharePoint could be indexing) and together this is called a Crawled Property. The (Text) bit just tells us that this property contains text as opposed to an integer, boolean or date time etc.
Now create one more managed property in the same way using these values:
Basic:5(Text) is to be found under the "Basic" category. We'll use the ContentType for each document to decide how to display the results, i.e. render an img tag to display an image or an object tag to render a Silverlight player to show video or play sound.
At this point you need to perform a TWO FULL CRAWLS of the content in SharePoint. This will pickup the new managed properties and map the appropriate metadata into them. Failure do perform these crawls will result in no results when using queries which involve these properties :(
You only need to do these full crawls after adding managed properties. Regular changes in metadata are picked up as part of an incremental crawl.
Edit XSL and "fetched properties".
This is where the real work of displaying video or mp3 previews in the search results takes place.
<Columns><Column Name="WorkId"/><Column Name="Rank"/><Column Name="Title"/><Column Name="Author"/><Column Name="Size"/><Column Name="Path"/><Column Name="Description"/><Column Name="Write"/><Column Name="SiteName"/><Column Name="CollapsingStatus"/><Column Name="HitHighlightedSummary"/><Column Name="HitHighlightedProperties"/><Column Name="ContentClass"/><Column Name="IsDocument"/><Column Name="PictureThumbnailURL"/><Column Name="PopularSocialTags"/><Column Name="PictureWidth"/><Column Name="PictureHeight"/><Column Name="DatePictureTaken"/><Column Name="ServerRedirectedURL"/></Columns>
This XML fragment tells the Search Core Results web part which managed properties we want to return for each search result. If you have not defined the managed property or you mistype the name in fetched properties, the web part will not render and will display a horrible error when you try to search.
To pull back as fetched properties those managed properties we defined previously and to configure the required XSL to display the results do the following:
PS. You can download the completed XSL file if you get into trouble. Just copy and paste the contents into the XSL Editor window as described above.
If anyone want me to explain what's in the XSL file, leave a comment and I'll add some more text here when I get time.
Testing the end result.
Return to your custom search homepage:
If you want more information on customising the SharePoint 2010 Search User Interface, you'll can find all the documentation you'll need at:
Indeed a very good read! Very informative post with pretty good insight on all aspects of the topic! Will keep visiting in future too!
Hi, This really helpful and informative!!!. I am not able to view the thumbnail preview video image. Can you please explain about the xslt. Thanks.