views:

360

answers:

4

I'm trying to write a custom rendering template for use in the DisplayForm of a list whose fields include Title, HeaderImageUrl, Body. I've got the following so far:

<SharePoint:RenderingTemplate ID="NewsListForm" runat="server">
 <Template>
  <h1><SharePoint:FieldValue FieldName="Title" runat="server"/></h1>
  <div>
      <p>
          <SharePoint:UrlField FieldName="HeaderImageUrl" runat="server"></SharePoint:UrlField>
          <SharePoint:RichTextField FieldName="Body" runat="server" />
      </p>
  </div>
 </Template>
</SharePoint:RenderingTemplate>

The HeaderImageUrl field is configured to be an Image URL, but when I use the UrlField control, it renders a link.

I was thinking there might be a way to grab field values and put them in attributes, so something like this:

<asp:Image ImageUrl="<% FieldValue["HeaderImageUrl"] %>" runat="server"/>

If this kind of thing isn't possible, then is there another control in Microsoft.SharePoint.WebControls that would render an image from a URL?

+2  A: 

All the examples I've seen of calling a URL field to be displayed as an image have been through C# code. Which is quite annoying since most of web part bits can be written without using C# code. You might also need to go down that road here... perhaps a custom field control or user control.

Even if your code above did get the value for HeaderImageUrl, it wouldn't work as Url values are (confusingly):

[the actual url specified][some wierd delimeter string][the description field of the url (usually the actual url again if your don't specify it)]

So you would need to split the string with te delimeter values and use the [0] (the first) as what you inject into your image url.

Please do post if you find a way to do this without writing C# code.

p.s. Have you considered using a DataFormWebPart which has a xsl section in which you might be able to do extractly what you need?!

Example: You can stick the following syntax into any aspx / ascx file:

<%@ Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register tagprefix="WebPartPages" namespace="Microsoft.SharePoint.WebPartPages" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" 
NoDefaultStyle="TRUE" ViewFlag="0" Title="My List Display" 
 Default="FALSE" ID="dfwp_MyListDisplay"
 DisplayName="My List Display">
 <DataSources>
        <SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" 
        selectcommand='&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&quot;Category&quot;/&gt;&lt;Value Type=&quot;Text&quot;&gt;Header&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;' id="Utility_x0020_Navigation1"><SelectParameters><WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="My List Display"/>
        <asp:Parameter Name="ListName" DefaultValue="LIST NAME" />
        <asp:Parameter Name="WebUrl" DefaultValue="<% $SPUrl:~SiteCollection/ %>" />
        </SelectParameters>
        </SharePoint:SPDataSource>
    </DataSources>
    <ParameterBindings>
    <ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
    <ParameterBinding Name="PageURL" Location="ServerVariable(URL)" DefaultValue=""/>
    <ParameterBinding Name="SiteUrl" Location="ServerVariable(SERVER_NAME)" DefaultValue=""/>
</ParameterBindings>
    <datafields>@Title,Title;@URL,URL;@Category,Category;@Parent_x0020_item,Parent_x0020_item;@Sort_x0020_Order,Sort Order;@ID,ID;@ContentType,Content Type;@Modified,Modified;@Created,Created;@Author,Created By;@Editor,Modified By;@_UIVersionString,Version;@Attachments,Attachments;@File_x0020_Type,File Type;@FileLeafRef,Name (for use in forms);@FileDirRef,Path;@FSObjType,Item Type;@_HasCopyDestinations,Has Copy Destinations;@_CopySource,Copy Source;@ContentTypeId,Content Type ID;@_ModerationStatus,Approval Status;@_UIVersion,UI Version;@Created_x0020_Date,Created;@FileRef,URL Path;</datafields>
    <xsl>
    YOUR XSLT HERE
    </xsl>

</WebPartPages:DataFormWebPart>
Zeb
One of the reasons I don't want to use a DataFormWebPart is because it requires SharePoint Designer. I want to have everything I deploy be in a solution(s).
Ben Collins
Hey,You don't need SharePoint Designer to have create a DataFormWebPart. You can just add the DataFormWebPart syntax to any ascx or aspx file including any page layouts you might be deploying. Have a look at the edit I've made to my post.
Zeb
A: 

Unable to test it atm (no environment) but I am quite sure I did this before.

Try these:

<SharePoint:FieldValue FieldName="HeaderImageUrl" runat="server"/>

or:

<img src="<SharePoint:FieldValue FieldName="HeaderImageUrl" runat="server"/>" />

I'll check back on this one and do an actual test by monday if those didn't work and you still need help.

F.Aquino
+1  A: 

The following code will get you the url of an imagefield:

SPListItem item = GetItem();
string imagefieldhtml = item["HeaderImageUrl"].ToString();
ImageFieldValue imagefield = new ImageFieldValue(imagefieldhtml);

var url = imagefield.ImageUrl;

So maybe altering your to something like this will help you, or maybe help you out atleast...?

<asp:Image ImageUrl="<% (ImageFieldValue ["HeaderImageUrl"]).ImageUrl %>" runat="server"/>
Johan Leino
A: 

Turns out that my problem was simply that I didn't have that image column set up to render as an image, rather than as a link. The builtin Microsoft.SharePoint.UrlField control worked just fine, as it turned out.

Ben Collins