I've created some custom formatting for a document library view.
I'm displaying file type icons depending on file type.
My problem is identifying when the item is actually a folder rather than a file - currently folders use my fallback generic image so aren't identifiable. if([$File_x0020_Type] doesn't seem to accept folder or directory - guessing this isn't the right approach anyway.
Any help much appreciated.
Relevant bit of json
"children": [
{
"elmType": "img",
"attributes": {
"src": "= 'https://res-1.cdn.office.net/files/fabric-cdn-prod_20240610.001/assets/item-types/20/' + if([$File_x0020_Type] == 'png', 'photo.svg', if([$File_x0020_Type] == 'docx', 'docx.svg', if([$File_x0020_Type] == 'doc', 'docx.svg', if([$File_x0020_Type] == 'ppt', 'pptx.svg', if([$File_x0020_Type] == 'pptx', 'pptx.svg', if([$File_x0020_Type] == 'xls', 'xlsx.svg', if([$File_x0020_Type] == 'xlsx', 'xlsx.svg', if([$File_x0020_Type] == 'pdf', 'pdf.svg', if([$ItemType] == 'Folder', 'folder.svg', 'genericfile.svg'))",
"alt": "file icon"
},
"style": {
"width": "20px",
"max-width": "20px",
"height": "20px",
"margin-right": "10px"
}
},
{
"elmType": "a",
"txtContent": "[$FileLeafRef]",
"style": {
"text-decoration": "none",
"width": "100%"
},
"attributes": {
"href": "= [$FileRef]",
}
}
]
Full json
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
"hideColumnHeader": true,
"hideSelection": true,
"rowFormatter": {
"elmType": "div",
"style": {
"display": "flex",
"flex-direction": "column",
"align-items": "flex-start"
},
"children": [
{
"elmType": "div",
"attributes": {
"class": "ms-List-cell"
},
"style": {
"display": "=if(@rowIndex == 0, 'flex', 'none')",
"font-weight": "bold",
"font-size": "14px",
"font-family": "Public Sans",
"width": "100%",
"border-bottom": "1px solid #ddd"
},
"children": [
{
"elmType": "div",
"txtContent": "File",
"style": {
"width": "70%",
"padding": "8px 5px"
}
},
{
"elmType": "div",
"txtContent": "Modified",
"style": {
"width": "30%",
"padding": "8px 5px"
}
}
]
},
{
"elmType": "div",
"attributes": {
"class": "sp-row-listpadding"
},
"style": {
"border-bottom": "1px solid #ddd",
"font-family": "Public Sans",
"width": "100%"
},
"children": [
{
"elmType": "div",
"style": {
"text-align": "left"
},
"children": [
{
"elmType": "div",
"style": {
"float": "left",
"font-size": "14px",
"padding": "8px 5px",
"width": "70%",
"display": "flex"
},
"attributes": {
"class": "sp-row-listPadding"
},
"children": [
{
"elmType": "img",
"attributes": {
"src": "= 'https://res-1.cdn.office.net/files/fabric-cdn-prod_20240610.001/assets/item-types/20/' + if([$File_x0020_Type] == 'png', 'photo.svg', if([$File_x0020_Type] == 'docx', 'docx.svg', if([$File_x0020_Type] == 'doc', 'docx.svg', if([$File_x0020_Type] == 'ppt', 'pptx.svg', if([$File_x0020_Type] == 'pptx', 'pptx.svg', if([$File_x0020_Type] == 'xls', 'xlsx.svg', if([$File_x0020_Type] == 'xlsx', 'xlsx.svg', if([$File_x0020_Type] == 'pdf', 'pdf.svg', if([$ItemType] == 'Folder', 'folder.svg', 'genericfile.svg'))",
"alt": "file icon"
},
"style": {
"width": "20px",
"max-width": "20px",
"height": "20px",
"margin-right": "10px"
}
},
{
"elmType": "a",
"txtContent": "[$FileLeafRef]",
"style": {
"text-decoration": "none",
"width": "100%"
},
"attributes": {
"href": "= [$FileRef]"
}
}
]
},
{
"elmType": "div",
"txtContent": "=toLocaleDateString([$Modified])",
"style": {
"float": "left",
"font-size": "14px",
"padding": "8px 5px"
},
"attributes": {
"class": "sp-row-listPadding"
}
}
]
}
]
}
]
}
}

