Locked RelatedImage throws System.Reflection.TargetInvocatio

Posted by Community Admin on 04-Aug-2018 14:07

Locked RelatedImage throws System.Reflection.TargetInvocationException

All Replies

Posted by Community Admin on 27-Jul-2016 00:00

I have a Dynamic Module that has a related image field. Once the dynamic content item is published, if someone has accidentally locked the related image in the Image Library (usually using the back button) whenever I try to get the image to get the url for the widget I get a System.Reflection.TargetInvocationException. 

public static string GetRelatedImageLink(this DynamicContent dynamicContent, string fieldName)
    Image image = dynamicContent.GetRelatedItems<Image>(fieldName).SingleOrDefault();
 
    if (image != null)
    
        return image.ResolveMediaUrl();
    
 
    return string.Empty;

Does anyone have a better way to get the related images url? I would it rather just not get the url and not show anything that throw a controller error that hoses the entire widget in stead of not displaying the image.

This thread is closed