Calculated Columns - Displaying Images

by oscar 12/1/2007 12:51:00 PM

Recently, I was working on a project that required the  creation of an IT Project Management Site Template.  I thought, hmm, pretty straight forward, but wait, I ran into an issue that I am positive, could have been solved in other ways (List Event Handler)

The requirement was to display an image based on a certain condition, and this condition had to do with lookup of other List Column values. 

If you want to display images for a calculated column here are the steps you need to take:

  1. Modify the FLDTYPES.XML file(or make a copy of it!)
  2. Search for the field definition "Calculated"
  3. On the default rendering pattern, paste the following

    <Switch>
    <Expr>
    <GetFileExtension><Column/></GetFileExtension>
    </Expr>
    <Case Value="giF">
    <HTML><![CDATA[<IMG SRC="]]></HTML><Column HTMLEncode="TRUE"/><HTML>"</HTML>
    </Case>
    <Default>
    <Column HTMLEncode="TRUE" AutoHyperLink="TRUE"
    AutoNewLine="TRUE"/>
    </Default>
    </Switch>

    Create a new Column of type Calculated - put some logic in it like so

    =IF(AND(Impact="3",Probability="1"),"/_layouts/images/KPIDefault-1.giF",
    IF(AND(Impact="3",Probability="2"),"/_layouts/images/KPIDefault-2.giF",
    IF(AND(Impact="3",Probability="3"),"/_layouts/images/KPIDefault-2.giF",
    IF(AND(Impact="2",Probability="1"),"/_layouts/images/KPIDefault-1.giF",
    IF(AND(Impact="2",Probability="2"),"/_layouts/images/KPIDefault-1.giF",
    IF(AND(Impact="2",Probability="3"),"/_layouts/images/KPIDefault-2.giF",
    IF(AND(Impact="1",Probability="1"),"/_layouts/images/KPIDefault-0.giF",
    IF(AND(Impact="1",Probability="2"),"/_layouts/images/KPIDefault-0.giF",
    IF(AND(Impact="1",Probability="3"),"/_layouts/images/KPIDefault-1.giF","other")))))))))

    NOTE: The Impact and Probability are columns in the SharePoint List.  Based on this logic, I am re-using the images from the KPIs in MOSS and simply pointing to them.

    And that's it!   You now can output images for your List.  Here is how it looks

    image

Currently rated 3.8 by 4 people

  • Currently 3.75/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

WSS 3.0 | MOSS 2007 | CAML | Calculated Fields

Related posts

Comments

2/11/2008 12:17:54 AM

hi,
Am new to Sharepoint development. and am not really getting what does this statement mean.
Create a new Column of type Calculated - put some logic in it like so
=IF(AND....

Could you give some more information on how to create a column of calculated.

Thanks.
Manju.

Manjunath us

2/12/2008 6:58:35 AM

Hi Manju,

Thanks for stopping by Smile

Most of the information you will need is here:
office.microsoft.com/.../HA011609471033.aspx

They have sample common formulas that you can tweak to get the results you are looking for.

Is there anything in particular that you are trying to accomplish?

Cheers,
Oscar

Oscar us

2/14/2008 11:26:58 PM

Hey Oscar, this is awesome... i have been looking all over for information on adding images in a calculated field...

I have but one request. Can you expand the view of the XML excerpt? To include some of the information surrounding the <Switch></switch> section?

Thanks
Phil

Phil

2/26/2008 1:58:35 AM

Hello,
How can i change the MenuTemplateId of SPMenuField in an SPGRIDVIEW? Basically i need different MenuItems in each row. [Otherwise, disabling some MenuItems in some row would do.] i assumed that it could be possible with the following idea.


public void SPGriView1_OnRowCreated(object sender, GridViewRowEventArgs e)
{
SPMenuField spMenuField = (SPMenuField)e.Row.Cells[1].FindControl("spMenuField"Wink;
try
{
if (e.Row.DataItem != null)
{
//datatablePublications --> DataSource i.e. Datatable
DataRow drPublication = datatablePublications.Rows[e.Row.DataItemIndex];
string strStatus = drPublication["Locked"].ToString();
if (strStatus == "true"Wink
spMenuField.MenuTemplateId = "PublicationLocked";
else if (strStatus == "false"Wink
spMenuField.MenuTemplateId = "PublicationUnlocked";

}
}

catch { }

//spMenuField.MenuTemplateId = "PublicationUnlocked";
}

but it ended up with compilation error, saying that-cannot convert from "System.Web.UI.Control" to "Microsoft.SharePoint.WebControls.SPMenuField"


Is this a the way i can accomplish this.Or please let me know, if there is a better way of doing it.

Manjunath us

2/27/2008 5:31:47 AM

Excellent post, and the perfect solution to a problem i have been having.

I have one question however, if someone uses an image of a large size, will this display those images as well on the list? the concern here being that large images will cause a performance issue.

Thanks
Phil

Phil

3/11/2008 5:54:43 AM

Hi Phil,

Good question! Since the rendering pattern can be manipulated, you should be able to add image width and height properties to that img tag. This will control the display size. However, for the actual checking of the files size being uploaded, perhaps you can create an Event Handler on the Library that images are uploaded to, and check for file size, if too large, then display a friendly error.

Let me know if that works.

Oscar

Oscar us

4/17/2008 10:47:11 AM

Thanks Oscar - that was fantastic.
I'm sure you've helped hundreds of people, or possibly more!

Have a good day.

Steven au

7/11/2008 4:14:59 AM

Hi Turk, make sure you are not pasting any extra characters, spaces etc. It is only suppose to render the image as the example shows. So again, try and paste the code on notepad, then onto your calculated column, see if that helps!

Cheers,
Oscar

Oscar us

7/11/2008 4:15:53 AM

Steven, glad to hear my post helped you, thanks for the positive feedback!

Cheers,
Oscar

Oscar us

Add comment


 

  Country flag





Live preview

11/21/2008 3:39:28 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About Me

Oscar Medina I am a SharePoint Solutions Architect based out of San Francisco, California.
You can read more about me here

E-mail me Send mail
View Oscar Medina's profile on LinkedIn

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in