Reputation: 1704
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";
}
}
Upvotes: 2
Views: 7665
Reputation: 323
The answer of Andro Selva was correct but i have to revise the code a little bit. Since my Code Behind is custom as well, instead of using:
RichImageField image = (RichImageField)this.Parent.FindControl("rifPageImage");
I used:
Protected RichImageField rifPageImage;
and I just replaced all image to rifPageImage. I was getting a null value for this.Parent.FindControl since the .cs file is not really linked to my .aspx file.
Upvotes: 0
Reputation: 1
Have you tried supplying a default value for your field when you provision your layout into the masterpage gallery?
<Property Name="RichImageFieldName" Value="<![CDATA[<IMG SRC="images/pathtoimage.jpg"></IMG>]]>" />
Upvotes: 0
Reputation: 1
I had the same need and ended up creating a custom control that would add the placeholder/default image to the control on prerender. The custom control is then added right next to the RichImageField control in the page layout.
Code:
if ((PublishingPage.IsPublishingPage(SPContext.Current.ListItem)) &&
(SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New))
{
RichImageField image = (RichImageField)this.Parent.FindControl("rifPageImage");
if (image != null)
{
ImageFieldValue imgValue = (ImageFieldValue)image.Value;
if (imgValue.ImageUrl == string.Empty)
{
image.Value = new ImageFieldValue("<img src='/_layouts/images/GD/placeholder.png' />");
}
}
}
Upvotes: 0
Reputation: 77540
Could you create an ImageFieldValue
inline?
<PublishingWebControls:RichImageField id="InteriorHeaderImage" runat="server"
DefaultImageURL='<%# new ImageFieldValue("<img src=\"test.png\" />") %>' />
This would only work if the control's DataBind()
method is called at some point.
Upvotes: 0
Reputation: 706
The ImageFieldValue inherits from the HtmlTagValue class. The ImageFieldValue has a constructor that accepts the complete <img /> element. Have you tried setting the default value using the complete <img /> element, like <img src="test.png" />?
Upvotes: 0
Reputation: 4578
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).
Upvotes: 1