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 "#";
}
}
Leave a comment