How to get Image URL from PublishingPageImage field in a SharePoint 2013 or 2010 Sandbox Solution

Ashok Raja
 
Solutions Architect
March 19, 2013
 
Rate this article
 
Views
2401

The conventional SharePoint 2013 or 2010 farm solution model accepts ImageFieldValue for parsing Publishing Image tag. But this approach does not work out for Sandboxed solution. Although usage of ImageFieldvalue does not throw any error during build and deployment ,it will throw and error on execution. To overcome this issue use the below code to extract the Image URL from PublishingPageImage field

Sandboxed Solution

 private string GetPublishingPageImageURL(SPListItem item)
 {
     try
     {
         string ImgTag = item.GetFormattedValue("PublishingPageImage");
         return String.IsNullOrEmpty(ImgTag) ? "#" : XElement.Parse(ImgTag + "</img>").Attribute("src").Value;
     }
     catch
     {
         return "#";
     }
 }

Conventional Method by referring Microsoft.SharePoint.Publishing.Fields namespace

 private string GetPublishingPageImageURL(SPListItem item)
 {
     try
     {
         ImageFieldValue pageImage = item["PublishingPageImage"] as ImageFieldValue;
         return string.IsNullOrEmpty(pageImage.ImageUrl) ? "#" : pageImage.ImageUrl;
     }
     catch  
     {
         return "#";
     }
 }
Category : Tips

Author Info

Ashok Raja
 
Solutions Architect
 
Rate this article
 
I am Ashok Raja, Share Point Consultant and Architect based out of Chennai, India. ...read more
 

Leave a comment