Daves Occasional SharePoint Thoughts.....

Thinking about SharePoint....and some other dev stuff that floats me boat.

Previewing audio and videos in SharePoint 2010 search results.

Previewing audio and videos in SharePoint 2010 search results.

Rate This
  • Comments 2

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:

  1. Create an assets document library to hold media.
  2. Create a search page and search results page.
  3. Configure the search web parts to return content only from assets document libaries.
  4. Create required managed properties.
  5. Edit XSL and "fetched properties".

Assumptions:

  1. You're using SharePoint 2010 (Standard or Enterprise) and not SharePoint 2010 Foundation.
  2. Your site collections are configured to use a single central search center site and not to use the /_layouts/OSSSearchResults.aspx page.
  3. The search center is based on the standard SharePoint search engine (i.e. not FAST) and was created using the Enterprise Search Center template.
  4. You have admin rights on the Search Application to allow you to create managed properties.

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.

  1. From the homepage of your site, click Site Actions->View All Site Content. This will take you to http://XXXXXXX/_layouts/viewlsts.aspx.
  2. Click the Create link at the top of the page and choose the Assets Library" template, call it MediaAssets or something similar and click the Create button.
  3. Upload some videos & mp3s etc. to this new gallery and ensure any videos are in a format supported by Silverlight.
  4. The Assets library comes preconfigued with an Image, Video & Audio content type so besure to tag your videos and mp3 files as required. TIP: Failure to do so will result in them not being displayed in our custom search page which uses the content type to decide how to display the results.
  5. Create small thumbnail images (.png format should do) to represent your media assets and upload these to any other document library (perhaps one based on the Picture Library template).
  6. Edit the metadata of each of the uploaded videos and mp3's and provide it with the "Preview Image Url". This is the Url of the actual thumbnail image uploaded in the previous step (not the dispform.aspx?ID=xxxxx page.)
  7. Set a crawl running so the content is ready for searching later on (full or incremental - it doesn't matter).

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.

  1. Navigate to your Search Center Site (usually something like http://XXXXX/search).
  2. From the Site Actions menu, click Edit Page to enter edit mode.
  3. You should now see two new links which appear under the "All Sites" and "People" tabs. Click the "Add New Tab" link.
  4. Enter "MediaAssets" for the tab name & "mediaassets.aspx" for the page. Click Save. PS. We haven't created mediaassets.aspx yet so don't fret!
  5. Back on the search homepage, hit F5 to reload the page and view your new tab (you'll get a 404 if you actually click on the new MediaAssets tab - let's fix that now).
  6. From the homepage of your search site, click Site Actions->View All Site Content. This will take you to http://XXXXXXX/search/_layouts/viewlsts.aspx.
  7. Click Pages in the Document Library section. This will take you to http://XXXX/search/Pages/Forms/AllItems.aspx
  8. From the Document tab on the ribbon, click New Document. This will take you to "Create Page".
  9. Enter "Media Asset Search" as the title your your new page and "mediaassets" for the Url (this ties is with the page referenced in step 4).
  10. For the Page Layout, choose "(Welcome Page) Search box".
  11. Clicking Create will generate a new search page with the configued URL.
  12. Browse back to your search homepage (http://XXXXXXX/search/) and click on the MediaAssets tab.
  13. Put the page into edit mode.
  14. From the Search Box webpart underneath the tabs, click it's properties downdown (the little arrow on the right hand side) and choose Edit Web Part.
  15. From the properties box which now appears in the top right hand side of the screen, open the Miscellaneous section and in the "
  16. Repeat steps 6 - 11 above but this time enter a page title "Media Assets Results", a URL name of "mediaassetsresults" and a page layout of "(Welcome Page) Search results".
  17. Browse back to your search homepage (http://XXXXXXX/search/) and click on the MediaAssets tab. Enter a keyword to search for (something you know will produce results) and click enter (or the little magnifying glass icon). Hopefully you'll see some results.

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....

  1. Browse to your new media search results page (http://XXXXXX/search/Pages/mediaassetsresults.aspx) and put it into edit mode.
  2. Scroll down the page to the "Bottom Zone" and notice a web part called "Search Core Results". This is the real web part which does the actual work of taking your query, passing it to the search engine receiving back the results and formatting it as we dictate. Open the properties window for this web part as shown previously.
  3. Open the "Results Query Options" section and in the "Append Text to Query" box enter contentclass:STS_ListItem_851
  4. Click OK to close the properties box and "Save and Close" to save changes to the page.

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.

  1. Open your Search Application Administration Page from within Central Administration (the URL is of the form http://hostname:portnumber/searchadministration.aspx?appid=someguid).
  2. On the left hand menu under Queries and Results, click Metadata Properties.
  3. Click new managed property.
  4. Enter a property name of VideoPreviewImage and leave the type as text.
  5. Under the Mappings to Crawled Properties section select the "" radio button and press the "Add Mapping" button.
  6. When the dialog  labeled "Crawled Property Selection" opens choose "SharePoint" from the catagories dropdown.
  7. Page through the properties until you find one called "ows_AlternateThumbnailUrl(Text)". Select this and click OK.
  8. Back on the New Managed Properties page, click OK at the bottom to save the changes.

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:

Managed Property Name Crawled Property to map to Notes
ContentTypeforSearch Basic:5(Text) and also to ows_ContentType(Text)

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.

  1. Browse to the mediaassetsresults.aspx page, put it into edit mode and open the properties for the Search Core Results web part.
  2. Expand the Display Properties section and untick the "" option. This will allow us to provide some custom XML & XSL is display our results.
  3. Copy all the text in the Fetched Properties box and paste it into a notepad. The text looks like this:

<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:

  1. Add the following 2 new fetched properties before the closing </Columns> element: <Column Name="ContentTypeforSearch"/><Column Name="VideoPreviewImage"/>(these are the 2 managed properties we created earlier).
  2. Copy the whole single line of XML back into the Fetched Properties box in the browser and click Apply in the properties box.
  3. Now click the XSL Editor button and in the resulting pop-up, copy all the XSL into a new notepad.
  4. Press CTRL-G and enter 206 to browse to that line number. You should be at a section which starts: <xsl:template match="Result">. This section of XSL is called for every result in the list of results returned from SharePoint. Warning to non-developers or devs with no XSL experience: This might start to look like foreign teritory, it is! Take the simple XSL tutorial at http://www.w3schools.com/xsl/ if you need to know the basics.
  5. Replace line 212 with: <xsl:when test="contentclass[. = 'STS_ListItem_851']">. This means the XSL following this line should be called when ever a result of contentclass = STS_ListItem_851 is found. This should be all of them since we already configured the Search Core Result web part to return only items of this class.
  6. Locate the    <div class="srch-picture1"><img src="http://blogs.msdn.com/_layouts/images/imageresult_16x16.png" /></div> code block about two lines under the line you just modified and delete it. This is the code which displays the little icon to tell us what file type it is....we don't need it.
  7. Locate the    <div class="srch-picture2"> <img class="srch-picture" src="{picturethumbnailurl}" alt="" /> </div> code block right under the stuff you just removed and replace the opening <div> tag with <div class="srch-picture2" style="width:400px;height:200px">. This ensures our video will display at a sensible size.
  8. Delete the entire img tag and replace it with the code from this file. <-- This is the real meat in this whole document. The XSL outputs HTML to render a Silverlight player and passes in paramaters for the preview image url and video/mp3 url.
  9. Copy and paste the XSL back into the XSL Editor window in your browser, press Save to save the data and OK to close the web part properties window.
  10. Save the page by clicking "Save & Close" in the ribbon.

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:

  1. Click on the MediaAssets tab and enter the name of a video/mp3. Hopefully you should see video/audio previews in the search results.
  2. Try also entering a query of contenttype:audio to return just audio content types from all asset libaries.

If you want more information on customising the SharePoint 2010 Search User Interface, you'll can find all the documentation you'll need at:

http://msdn.microsoft.com/en-gb/library/ee819904.aspx

  • 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.

    radhaprabha@rediffmail.com

Page 1 of 1 (2 items)
Leave a Comment
  • Please add 7 and 7 and type the answer here:
  • Post