1

I have a Pictures Library and I want to customize the gallery with cards with picture on top and below the picture some data taken from other columns in the Pictures Library.

I've created a Thumbnail column to have a preview, but when I switch to gallery view I don't understand how to make reference in my JSON to the thumbnail picture.

I've tried using [$Thumbnail.serverRelativeUrl] or just [$Thumbnail] but it doesn't work.

Any idea please?

2
  • What is the data type & internal name of your Thumbnail column? How did you create this? Commented Oct 16, 2021 at 3:56
  • Column type is "Image" and named "Thumbnail". Created on the library setting.
    – Mark84
    Commented Oct 17, 2021 at 14:35

2 Answers 2

1

The image field object has the following properties (with example values):

{
  "fileName": "image.png",
  "id": "6bb1d843-0633-4c9a-9a16-90bc5abd1d8e",
  "serverRelativeUrl": "/teams/Discovery/SiteAssets/Lists/ad6ed939-0db2-4d85-8a39-8f3497f41eee/image.png",
  "serverUrl": "https://contoso.sharepoint.com"
}

Sourcee: Column formatting - Special string values - see "Image fields" section.

So, you should be able to get the URL of image in image column using @currentField.serverRelativeUrl or [$InternalNameOfColumn.serverRelativeUrl].

So, Please check if you are using correct internal name of your column. Follow below reference to find out the internal of column: Find the internal name of SharePoint column


Additional:

As you are using Picture library (document library), you can try using the in-built token to retrieve the URL to the thumbnail of a image/file instead of creating custom column.

  • @thumbnail.small, @thumbnail.medium, and @thumbnail.large evaluate to the thumbnail URL in 3 different predefined sizes.

Example:

{
   "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
   "elmType": "img",
   "attributes": {
      "src": "@thumbnail.small",
      "alt": "='Thumbnail of file ' + [$FileLeafRef]"
   },
   "style": {
      "width": "100%",
      "max-width": "100%"
   }
}

Source: Column formatting - Thumbnails

1
  • 1
    Thank you!! since I'm using a Picture Library src": "@thumbnail.small works perfectly. Thanks Mark
    – Mark84
    Commented Oct 18, 2021 at 16:57
1

This is the code I used to make the card shown below. My example uses a people column to extract the user's name and image and their start date to display in the card, plus a little CSS magix mixed in (border-radius) for rounded corners and the class (ms-bgColor-neutralQuaternaryAlt) for matching the site’s theme.

All in all, this answer has everything you should need to build your own solution to your question.

Exemplar Image:

User Card From A SharePoint List

Code Explainer:

Since we are importing both a way to format the view as well as the row, we need to import both the row and view formatting schema.

The hideSelection and the hideListHeader options are both set to true.

  1. "hideSelction":true removes the ability for the item to be selected by the end users (who have adequate permissions). "hideSelection":true in addition to the removal of the command bar on a page effectively renders the item un-editable for the casual user.
  2. "hideListHeader":true removes the column labels from the view.

(BTW, both these elements and more can be used on ANY list or library view of your choosing, so long as you import the View and Row Formatting schemas, as outlined below.)

  1. Next we reference the rowFormatter. We declare a container element (a DIV) and specify how it should look using the "style" property. Note: this is the equivalent of using in-line css on an element.

  2. Next we can declare other attributes that would also be in the (normally) in-line HTML , if this were written in HTML. In this case, we are ascribing values to the class attribute.

  3. Then we begin enumerating the children elements within the same principles used in the previous two steps.

    5.a: We create a child DIV that contains the image, set the attributes and style and iterate through the other children of the parent DIV which, includes an IMG sub-element.

Documentation for your reference

  1. View Formatting Documentation: https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/view-formatting

  2. Column Formatting Documentation: https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting

  3. List Formatting GitHub: https://github.com/pnp/List-Formatting This is particularly useful if you go and explore the samples. you will learn a lot.

  4. Finally, just go and read the schemas by navigating to their URLs. JSON schemas are nothing like XML schemas to read - they are un-junked up text files with normative spacing and other code conventions, without all the impositions XML makes towards self-perfection (e.g.: DTD’s, etc.) Hell, you might even want to print it out! ;)

    4.a. https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json

    4.b. https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json

Please mark this as an answer if it helped you.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json",
  "schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
  "hideSelection": true,
  "hideListHeader": true,
  "rowFormatter": {
    "elmType": "div",
    "style": {
      "float": "left",
      "display": "flex",
      "flex-wrap": "wrap",
      "flex-direction": "column",
      "align-items": "center",
      "justify-content": "center",
      "width": "250px",
      "height": "200px",
      "max-width": "250px",
      "max-height": "200px",
      "border-radius": "8px",
      "margin-right": "10px",
      "margin-bottom": "10px",
      "box-shadow": "2px 2px 4px darkgrey",
      "overflow": "hidden"
    },
    "attributes": {
      "class": "ms-bgColor-neutralQuaternaryAlt ms-fontColor-neutralDark"
    },
    "children": [
      {
        "elmType": "div",
        "children": [
          {
            "elmType": "img",
            "style": {
              "display": "block",
              "height": "auto",
              "max-height": "150px",
              "max-width": "150px",
              "border-radius": "50%"
            },
            "attributes": {
              "src": "=@currentWeb + '/_layouts/15/userphoto.aspx?size=M&accountname=' + [$Employee.email]",
              "title": "[$Employee.Title])"
            }
          }
        ]
      },
      {
        "elmType": "div",
        "style": {
          "text-align": "center",
          "width": "100%",
          "display": "block"
        },
        "attributes": {
          "class": "ms-fontColor-neutralDark"
        },
        "children": [
          {
            "elmType": "div",
            "style": {
              "font-weight": "500",
              "font-size": "1.5rem"
            },
            "txtContent": "[$Employee.title]"
          },
          {
            "elmType": "div",
            "style": {
              "font-weight": "500",
              "font-size": "1.1em",
              "overflow": "hidden",
              "max-width": "280px",
              "white-space": "nowrap",
              "text-align": "center",
              "margin-bottom": "0.5em"
            },
            "txtContent": "[$Employee.jobTitle]"
          },
          {
            "elmType": "div",
            "style": {
              "font-weight": "500",
              "font-size": "1.5em",
              "overflow": "hidden",
              "white-space": "nowrap",
              "text-align": "center"
            },
            "attributes": {
              "class": "ms-bgColor-white ms-fontColor-themePrimary"
            },
            "children": [
              {
                "elmType": "span",
                "attributes": {
                  "iconName": "AddFavorite"
                }
              },
              {
                "elmType": "span",
                "txtContent": " Welcome!"
              }
            ]
          }
        ]
      }
    ]
  }
}

Your Answer

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

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