Conditional color marking in a Sharepoint List for multiple columns (PowerShell PnP?)

Mark Gorelyk 61 Reputation points
2022-01-12T15:00:51.72+00:00

Could someone advice an approach for Conditional color marking in a SharePoint List for multiple columns? Ideally via PoerShell PnP, but any other mehod would be also appreciated.

Use case: A lot of identical choice columns to be formatted in the same manner. E. g. yellow for value "Normal", red for value "Bad", green for value "Good".

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,563 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Allen Xu_MSFT 13,821 Reputation points
    2022-01-13T02:47:27.157+00:00

    Hi @Mark Gorelyk ,

    Apply below JSON code to your choice columns:

    {  
        "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",  
        "elmType": "div",  
        "txtContent": "@currentField",  
        "attributes": {  
            "class": "=if(@currentField == 'Normal', 'sp-css-backgroundColor-BgGold', if(@currentField == 'Bad', 'sp-css-backgroundColor-BgCoral', if(@currentField == 'Good', 'sp-css-backgroundColor-BgMintGreen', '')))"  
        }         
    }  
    

    Test result on my end:
    164535-image.png

    You can add above JSON code to a column using advanced mode or using PnP PowerShell below:

    $SiteURL = "https://contoso.sharepoint.com/sites/test"  
    $ListName="Config"  
    $FieldName="choice" #Internal Name  
    $JsonFormat = @"  
    {  
     "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",  
     "elmType": "div",  
     "txtContent": "@currentField",  
     "attributes": {  
         "class": "=if(@currentField == 'Normal', 'sp-css-backgroundColor-BgGold', if(@currentField == 'Bad', 'sp-css-backgroundColor-BgCoral', if(@currentField == 'Good', 'sp-css-backgroundColor-BgMintGreen', '')))"  
         }         
     }  
    "@  
     
    Connect-PnPOnline -Url $SiteURL -Interactive  
    
    $Field =  Get-PnPField -Identity $FieldName -List $ListName   
    $Field | Set-PnPField -Values @{CustomFormatter = $JsonFormat}  
    

    If you want to batch apply this JSON code to all your choice columns at once, I'm afraid it is impossible. As an alternative, you can go to Site settings > Site columns > Create a site choice column > add JSON code to Colnmn Formatting section. Next time you add this site choice column to lists/libraries, you won't need to add JSON code to them manually again and again.
    164582-image.png

    ----------

    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.

    1 person found this answer helpful.

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.