Sharepoint RichImageField set default image

Craig McKeachie picture Craig McKeachie · Aug 17, 2009 · Viewed 7.4k times · Source

In my custom page layout I declare a RichImageField

<PublishingWebControls:RichImageField id="InteriorHeaderImage" FieldName="InteriorHeaderImage" InputFieldLabel="Header Image"  runat="server" DisplayWidth="960" DisplayHeight="242"  />

I'm trying to figure out how to set the default image so the control always shows an image even the first time a page is created using the layout.

Ideally, I would like a declarative approach for example:

<PublishingWebControls:RichImageField id="InteriorHeaderImage" DefaultImageURL="[it should be this easy]" />

It appears that the value property of the control is assigned an ImageFieldValue object which I could probably figure out how to set in a code-behind but this approach seems harder than it should be: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.fields.imagefieldvalue.aspx

Solution: I finally ended up creating a code behind for my page layout by creating a class file that inherits from Microsoft.SharePoint.Publishing.PublishingLayoutPage as described here: http://msdn.microsoft.com/en-us/library/bb986729.aspx

The code of course ended up only being a couple of lines:

protected void Page_Load(object sender, EventArgs e)
        {

            ImageFieldValue imageField = InteriorHeaderImage.Value as ImageFieldValue;
            if (imageField!=null)
            {
                if(string.IsNullOrEmpty(imageField.ImageUrl))
                    imageField.ImageUrl = "/Style Library/assets/images/img1small.jpg";
            }

        }

Answer

dariom picture dariom · Aug 17, 2009

I can't test this out at the moment, but the RichImageField field control has a Value property that can be set. Can you try setting this declaratively?

Maybe this will work?:

<PublishingWebControls:RichImageField id="InteriorHeaderImage" Value="[it should be this easy]" />

Alternatively, you could set in the code-behind for the page layout which should be easier than manipulating the ImageFieldValue (as per your link).