Condividi tramite


How to: Add Publishing Image Field in your Search results

Hi,

This has been a recurring problem in SharePoint: Although you add a publishingImage field to your layout and it gets indexed, the managed property mapped to this crawled property has no value.

This is related to two issues:

  • Compound controls does not get indexed. Specifically only text is get indexed, and HTML is stripped out. So an image <img /> get an empty string, but summary links returns text.
  • SharePoint does not understand PublishingImage field. It may index all img.src of a page, but no specific parsing.

There are several solutions to resolved this scenario, but all they seem too complex (ex.: create a second field and update it with the url with an event receiver). Finally we get a simple solution without changing the product ;)

  1. Create a webcontrol that will read from the current item the field value and write in the pageadditionalhead placeholder a meta tag
  2. Add the webcontrol to your page layout
  3. Run an incremental crawl to get the property
  4. Create a managed property that will be mapped to the crawled property named in uppercase as the name of the meta tag, in the Web category

A sample of the webcontrol code:

 using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.ComponentModel; 
using Microsoft.SharePoint.Utilities; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Publishing.Fields; 
 
namespace Microsoft.Services.WebControls 
{ 
 [DefaultProperty("Text"), 
 ToolboxData("<{0}:IndexableImage runat=\"server\" />")] 
 public class IndexableImage : WebControl 
 { 
 private static string getImageUrl(){ 
 ImageFieldValue imgField = null; 
 
 if (SPContext.Current.ListItem["metaImage"] != null) 
 { 
 imgField = (ImageFieldValue)SPContext.Current.ListItem["metaImage"]; 
 
 if (imgField != null) 
 { 
 return imgField.ImageUrl; 
 } 
 } 
 
 return string.Empty; 
 } 
 
 protected override void Render(HtmlTextWriter output) 
 { 
 output.Write("<meta"); 
 output.WriteAttribute("name", "metaImage"); 
 output.WriteAttribute("content", getImageUrl()); 
 output.Write("/>"); 
 output.WriteLine(); 
 } 
 } 
}

Namaste!

Comments

  • Anonymous
    December 12, 2011
    Could you please explain it more briefly....