2

I have 3 elements in a field and I would like the CQWP to display red as a bg colour of the field or amber or green.

I guess the starting point is

<xsl:if test="RAG = 'Red'">background-color: #FFFF00;</xsl:if>

or something similar, please correct,

but I am not sure where to place it.

Consider I am using the standard one

        <div class="description">
            <xsl:value-of select="@Description" />
        </div>

2 Answers 2

2
+50

If you do not wish to add style sheet as explain by @JJD then just update your item style sheet as below :

<div class="description">   
<xsl:attribute name="style">
    <xsl:choose>
        <xsl:when test="(@RAG = 'red')">background-color: red;</xsl:when>
        <xsl:when test="(@RAG = 'amber')">background-color: amber;</xsl:when>
        <xsl:when test="(@RAG = 'green')">background-color: green;</xsl:when>
    </xsl:choose>
</xsl:attribute>
<xsl:value-of select="@Description" /> 
</div>

enter image description here

8
  • this code broke the whole thing. I have replaced the 2 original lines of description with this and the webpart gives an error message
    – susan
    Sep 3, 2018 at 9:28
  • my rag is non-capital and my colours are written with capital letter (Red, Amber etc) , I have adapted yours to my situation but it is still not working
    – susan
    Sep 3, 2018 at 9:33
  • RAG - is column name where if value is 'red' it will apply background as red to your description div. This is tested works in my SPO environment. Sep 3, 2018 at 9:36
  • I hope you are updating fields to display section of web part properties. Sep 3, 2018 at 9:44
  • ok, there was one div too much, the colour amber does not display. Can I replace it with hex numbers?
    – susan
    Sep 3, 2018 at 9:46
1

Are you using an xsl file? In that case it would be something like

<div>   
<xsl:attribute name="class">
    <xsl:choose>
        <xsl:when test="(@RAG = 'red')">Descriptionred</xsl:when>
        <xsl:when test="(@RAG = 'amber')">Descriptionamber</xsl:when>
        <xsl:when test="(@RAG = 'green')">Descriptiongreen</xsl:when>
    </xsl:choose>
</xsl:attribute>
<xsl:value-of select="@Description" /> 
</div>

assuming 'red', 'green' and 'amber' are values from a 'RAG' column

basic styling

    <style>
    .Descriptionred{background:red;}
    .Descriptionamber {background:orange;}
    .Descriptiongreen{background:green;}    
    </style>

Descriptionred ,Descriptionamber and Descriptiongreen are classes which gets applied to div which renders description. Stylesheet for these columns needs to added on the page where you will add content query webpart.

2
  • i am using the original itemstyle.xsl in SharePoint. It is something very close to what is displayed in this page tdemeul.bunnybesties.org/2018/04/…
    – susan
    Aug 31, 2018 at 17:51
  • Are you able to put your script one and 2 in the same template?
    – susan
    Aug 31, 2018 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.