How to change file icon in document library of SharePoint online programatically.

Good Admin 30 Reputation points
2023-10-30T05:26:26.19+00:00

In SharePoint online I am not able to change file icon in Document library while accessing library over internet browser. Netither found any property or ContentType to change at list and site level.

While using CSOM I found some properties like IconOverlay and FileType, On Chnaging FileType it also impacting behaviour of file during access and not able to load IconOverlay.

Is there any straight forward or indirect way by which I can able to access path for icon image and able to change mapping to that image based on file type.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} vote

Accepted answer
  1. Anonymous
    2023-10-31T09:27:45.81+00:00

    Hi @Good Admin,

    You alse can use if judgment to display different UI according to the file type

    Firendly Name:

    ExcelDocument
    

    User's image

    {
      "$schema":    "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
     "elmType": "div",
    "txtContent": "[$File_x0020_Type]",
      "attributes":{
      "iconName": "=if([$File_x0020_Type]=='pdf','PDF',if([$File_x0020_Type]=='xlsx','ExcelDocument',''))"}
    }
    

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng


1 additional answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-10-30T08:48:49.89+00:00

    Hi @Good Admin,

    Currently there is no such function to change the icon by csom. As a workaround, I would recommend you to use json formating to custom the icon.

    You can use predefined icons from Office UI Fabric. For details, see the Fabric website.

    enter image description here

    enter image description here

    {
      "$schema":    "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
     "elmType": "div",
      "attributes":{
      "iconName": "PDF"}
    }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.