0

I am trying to add an image via calculated column in document library. It works perfectly well if i am doing it via UI as explained here.

But when i am adding it via xml from visual studio, so i am getting following error:

The element FormulaDisplayNames cannot contain child element img' because the parent element's content model is text only.

Below is my code:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID="{a8c7fe64-5791-416b-a837-26e93a3d056b}" Type="DateTime" Name="ArchivedDate" DisplayName="Archived Date"></Field>
  <Field ID="{b8581e64-01d1-4bef-9af9-06d190583664}" Type="Calculated" Name="Status" DisplayName="Status" ResultType="Number">
    <FormulaDisplayNames>
      =IF(ISBLANK([Archived Date]), 
          "<img src='/SiteAssets/red.png' style='height:20px; width:20px;'/>",
          ("<img src='/SiteAssets/green.png' style='height:20px; width:20px;'/>"))
    </FormulaDisplayNames>
    <FieldRefs>
      <FieldRef Name="ArchivedDate" />
    </FieldRefs>
  </Field>
</Elements>  

Can someone help me to find out what am i doing wrong? Or is it even possible to it via xml?

Thanks

0

You need to define the Formula in:

<Formula>
  ="<img src='/SiteAssets/"&IF([Archived Date],"green","red")&".png' style='height:20px; width:20px;'/>"
</Formula>

disclaimer: I shortened but did not test your formula

I have looked into my files, never defined anything inside FormulaDisplayNames, but I can't recall if you can omit

<FormulaDisplayNames></FormulaDisplayNames>

Update 1

I looked at my code, from some years back, in more detail;
seems you also have to encode in HTML entities

        _Formula = _Formula.replace(/</gi, '&lt;'); //      <
        _Formula = _Formula.replace(/>/gi, '&gt;'); //      >
        _Formula = _Formula.replace(/&/g, "&amp;"); //      &
0

Here is the final solution that works.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID="{a8c7fe64-5791-416b-a837-26e93a3d056b}" Type="DateTime" Name="ArchivedDate" DisplayName="Archived Date"></Field>
  <Field ID="{b8581e64-01d1-4bef-9af9-06d190583664}" Type="Calculated" Name="Status" DisplayName="Status" ResultType="Number">
    <Formula>
      =IF(ISBLANK([Archived Date]),
      "&lt;img src='/SiteAssets/red.png' style='height:20px; width:20px;' /&gt;",
      ("&lt;img src='/SiteAssets/green.png' style='height:20px; width:20px;' /&gt;"))
    </Formula>
    <FieldRefs>
      <FieldRef DisplayName="Archived Date" />
    </FieldRefs>
  </Field>
</Elements>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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