Freigeben über


How to index the PublishingRollupImage field and map a metadata property in SharePoint 2007

The title of this post is a bit misleading because it’s actually not possible to index this field.  You can map it to a metadata property but you won’t ever get any results back.  The fact is, indexing this field is by design not supported in SharePoint 2007.  The good news is I have a solution to work around this problem.  What you can do is create a second text only field, I’ve called it “ThumbnailImageURL”, and make sure to always sync that field up with the URL of the image in the “PublishingRollupImage” field.  You can do this using a custom event receiver which was actually the topic of my last post.  I’ll paste the code here again to make it easy:

    1: try
    2: {
    3:     this.DisableEventFiring();
    4:  
    5:     SPListItem item = properties.ListItem;
    6:     string imageUrl = string.Empty;
    7:     if (item["PublishingRollupImage"] != null)
    8:     {
    9:         imageUrl = ((Microsoft.SharePoint.Publishing.Fields.ImageFieldValue)(item["PublishingRollupImage"])).ImageUrl;
   10:         if (imageUrl != null && imageUrl.StartsWith("/"))
   11:             imageUrl = item.Web.Site.Url.AsDomain() + imageUrl;
   12:         item["ThumbnailImageURL"] = imageUrl;
   13:         item.SystemUpdate();
   14:     }                
   15: }
   16: catch (Exception ex)
   17: {
   18:     Utilities.LogError(properties.ListItem.Web, ex);
   19: }
   20: finally
   21: {
   22:     this.EnableEventFiring();
   23: }

 

There is a method included in here called “AsDomain()”.  This method makes sure to append the correct URL onto the beginning of the image URL in case we want to access this image from a different site collection.  The image URL’s SharePoint returns from the ImageFieldValue field is a relative URL, which can cause broken images across site collections.  Here is the code for the “AsDomain()” extension method:

    1: /// <summary>
    2: /// Parses the domain from a URL string or returns the string if no URL was found
    3: /// </summary>
    4: /// <param name="url"></param>
    5: /// <returns></returns>
    6: public static string AsDomain(this string url)
    7: {
    8:     if (string.IsNullOrEmpty(url))
    9:         return url;
   10:  
   11:     var match = Regex.Match(url, @"^http[s]?[:/]+[^/]+");
   12:     if (match.Success)
   13:         return match.Captures[0].Value;
   14:     else
   15:         return url;
   16: }

After you have created the ThumbnailImageURL field you will need to populate it in at least one place and run a full crawl before you will be able to map your metadata property to it.  That is about all it takes in order to be able to index Image URL’s from the PublishingRollupImage field, or any field for that matter.  If you have other solutions to this problem I’d love to hear about them.

Comments

  • Anonymous
    October 25, 2010
    But you could have used the Uri class to resolve the image url, that would work under more varying situations.