Mastering Custom JSON List Formatting In SharePoint

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
feeling ready to become the next SharePoint  expert on your team I'm here to help you with   that by the end of this video you'll have the  tools and knowledge you need to create custom   Json formatters from scratch let's elevate your  skills to a whole new level I'll also show you   a free tool that makes all of this much easier so  the truth is Json formatting is just intimidating   for a lot of SharePoint admins especially if they  have never used Json before I'm going to guide you   through this step by step and give you tips along  the way to help you better understand what it is   you're even looking at let's get into it so we're  going to start with just a blank list there's   nothing in here we're going to add some columns  and we're going to go through a few different   examples of how you can create this formatting  from scratch the first thing we need to do is   add an extension for the browser you're looking  for SP formatter and you're going to add that to   your browser I'll show you what this does in the  video but if you like this absolutely try this   out this can work great by itself but it works  even better with the visual studio code extension   which is how we're going to be using it so I've  got this added let's create a brand new [Music] column I'm just going to create a basic text  column and then we're going to add a new [Music] [Music] item okay just something in here so that we can  see what the impact is as we are adding our Json   formatting now we're going to go into column  settings and format this column and we're going   to go straight to advanced mode then we go up to  our extensions and we click on SP formatter now   we're going to turn it on for this particular tab  I do like that it's always off unless you choose   to turn it on so we're going to turn it on here  and it changes the column formatting pane we see   the the enhanced formatter is enabled it also  cleared out the schema since that's not really   needed in this case and as you're typing you'll  see that it will autop populate the options here   so for the element type if we just open this up  you can do a control space to trigger that auto   complete as well it'll tell you the what options  you've got to create a new element so if we're   want to create a div it's as easy as that we can  add a comma here it'll tell you what options we   can place inside this Json object so if we wanted  to add CSS Styles we can choose the style if we   want to set the text content that's going to be  displayed then we can choose the text content or   we can change other attributes on this div using  the attributes option you've got children on here   basically everything that you want to be able to  do inside this formatting it's going to help you   understand what options you can use but I said  we're going to be using visual studio code so   let's fire that up and I'll show you how that  works so I'm in here and I've got SharePoint   on the left and I've got Visual Studio code  on the right if you haven't downloaded Visual   Studio code uh it's a free program so there's no  reason to not use it it sure beats notepad so to   get started what we're going to do is download an  extension this extension is going to be called SP   formatter just like the browser extension but this  is one specific for vs code it's free as well so   you're just going to download that and notice that  it's got some instructions on here it it'll show   how to configure that you're going to be right  clicking on a Json file and you'll see start a new   session is available you also need to name your  Json files with a particular naming convention so   that this knows that these are going to be column  formatter so I've already done that I've got one   file called Json 1. colum dform matter. Json and  if I right click on this you'll see an option here   start a new session I'll click that and then in  SharePoint you can just toggle the formatting   off and back on and now you'll see VSS codes now  connected and if I type in here like if I type in   the curly brackets to specify a Json object you'll  see that it's automatically synced to the browser   not just that but as we're typing out this Json  formatter it's going to automatically be applied   in SharePoint and we'll see that preview live for  our samp our Simple Text field so to get started   we're going to open up a set of quotes and we'll  see that same auto complete here like we saw in   the browser so this is why I said it you could use  the browser extension by itself but if you use it   in conjunction with vs code it gives you more room  to see that Json formatting but most importantly   it gives you the option to save those files  because this is already saved on my file system a   quick contrl s will save this file I can keep all  of my Json formatters all together in one area and   apply it anytime I need to I don't have to worry  about which site that I apply one to everything's   right there so it is great for organization and  for building out a library of things that you   can use whenever the situation calls for it to  get started with this we're going to create a   new element so we use the elm type field and it  gives us the options here what elements we're   allowed to create in this formatter we're going  to create a div and you see everything's already   synced over in SharePoint in that formatter  pane so now we're going to add in a comma and   you notice that the text is already erased on our  preview uh inside the SharePoint list itself uh   it doesn't show us the value anymore what we're  going to do to get that value back is we need to   set the text content of this div because right  now it's just rendering an empty div on the page   so to get that value back we're going to tell  it that the text content should be the current   field now this is the current field value of the  column so if we select this then our text is now   back and this is essentially the default renderer  for that single line of text field there's a lot   of other placeholder variables like this current  field that you can add in there but getting into   those examples is probably going to be for another  video so if you want to know when those come out   make sure you're subscribed to the channel so  that you know when the video goes live but for   now let's start formatting this text let's start  doing something with it that you would commonly do   with uh formatter so what if we set the background  color of this uh this whole section here here so   let's open up some quotes and what we're going to  do is add in a style tag a style tag is going to   let us set the CSS properties of this div which  is how we would control things like color so as   soon as we pick the style tag it knows that the  style is going to be another Json object inside   this formatting block so it gives us the curly  brackets and I'm just going to hit enter to give   me a little bit of room to work with and now we'll  add in our background color and you see it right   here at the top but if I didn't know what that was  called I could scroll down through here to find   the CSS property that I think would represent that  value if you pick the wrong one you'll know it uh   because it won't cause the change that you're  expecting to see in the preview but I know that   the background color is going to be set with well  not border color background color and if you see   the pattern that I've been using with all of these  different lines you see that there's something   in quotes then there's a colon and then there's  something else in quotes other than the style tag   of course which is an object but what the pattern  is that you're seeing here is that there's going   to be an instruction in quotes like Elm type and  that's specifying you want to set the element   type to something that value is going to be in  quotes as well and then a colon will separate the   instruction from the value with the style tag it's  the same thing the style is in quotes but then   since there's an object we use the curly brackets  so for the background color that's just going to   be a value so we're going to add more quotes  and then it gives us all the available colors   if we do a dark gray then we see immediately in  the preview window that we've got that dark gray   background but now the text isn't very legible so  let's update the color of the text the foreground   color so we're going to need to add a comma to  this and then we'll add in color in CSS just the   word color will always represent the foreground  color colon and then we'll add in white and now   we've got the white text on a dark gray background  if we wanted this to be black text we can simply   do that that actually looks a little bit easier to  read than the white we'll leave it with black but   what if we wanted to Center this text inside that  div CU you see right now it's left aligned well   for that we're just going to add another comma and  then end quotes for a new style rule specify the   justifi content option and the value is going to  be Center so if we do that we see that our text is   now centered so if you wanted it centered instead  of left aligned it's very easy to do with one line   now what if we wanted to make this text bold well  that's easy enough we'll add in another Rule and   in this case the font weight is what we're wanting  to change the font weight will determine how bold   or unbold the uh the text is going to look so  we've got bold here and you see that reflected   and if we were to do Bolder then it'll be an even  stronger bold now what if you are making a change   and you don't see that change being reflected  looks like nothing you're doing is actually   updating in SharePoint well you may have an error  in your Json so for instance if I delete this   comma then you're going to see a red underline  on this formatter and if you hover over this it's   even going to tell you and it's expecting a comma  right up at the top of that popup window so if   you're making a change and if I try and change the  color to white nothing's going to happen over in   the preview because we have invalid Json and so it  can't apply that into SharePoint now let's add our   comma back in and see that everything's applied  again so as soon as you correct that Json format   it's going to start applying again so if you start  to see it's not working look at your formatting   look at what you've recently done and look for any  of those red underlines or other indicators that   you've got an error somewhere let's change this  back to black though and let's suppose we're done   with this for now okay we've got a basic example  it's working how are we going to start saving this   well first I'm going to save the file with just  a quick crl s but then over in SharePoint you're   just going to click save and you can close it  it's done it's saved if we refresh the page you   see our formatting is applied so there's a very  basic example of how you can start to change the   formatting on a particular field now in this  case it was a single line of text field what   if you had a different kind of column what if we  had a choice column let's add one of these and see   how that would work I'll just call it choice you  see that it's already got some colors in here and   that's just because as soon as you add that choice  column it's going to apply a default formatter   we're going to scrap all of that and do this from  scratch so this is good for now I'm just going to   click save and now we'll go into column settings  format the column you see it's already chosen the   choice pills here but we're going to go straight  into advanced mode and what we need to do at this   point is start with a new file so I'm going to  create a new file that's a control n by the way   that I just did uh just to quickly create a new  blank file but I need to save this so that the   formatter knows that this is going to be something  it needs to to run on but I need to save this so   let's just I can pick this name just to get the  naming convention right maybe we call this choice.   colum formatter or. Json you definitely want to  use some descriptive names because it's going to   be hard to tell when you look at a file name what  exactly it does but for now I'm just going to call   this one choice and then like we did before we  right click start a new session you even have to   toggle the enhanced format in that example because  as soon as we connected you see the vs code is   connected uh over here in SharePoint so we can  just start creating stuff now we'll do what we did   before we'll create an element of div We'll add in  our text content and set it equal to the current   field oh and I know why we're not seeing any  output over on SharePoint is because we don't have   a value yet I'm just going to quickly save this  file even though I know that formatting is not correct and I'm going add in I'm we'll just set  this to equal to Choice one for now now we can   go back into formatting there we go so our our  formatting is now applied you're not seeing the   choice pills anymore it's just straight text  now let's add in our formatting so there's   our value that's in the preview over there and  it's already set to Choice one we don't have the   default formatting applied anymore we are just  using our own Json formatter so what we want to   do at this point is start to customize this a bit  more now we're going to get a little bit fancier   I'm going to set the background color like I  did before but this time I want it to change   the background color depending on what value  we've got in that choice column so we add our   comma and we're going to add a style tag there's  our blank object now we we add in the background   color but instead of setting a static value that  that's applied no matter what choice is selected   for that list item what we're going to do is we're  going to use an Excel style formula in this value   here and this formula is going to give us the  ability to use if statements so if the value   is Choice one we can set the background color  to one color but if it's Choice two or choice   three we can set it to another color so first  let's start out with the single if statement   which will start out with equals if and then  a set of parentheses now the if statement is   going to use three different values inside the  parentheses the first value is going to be the   expression to look at in this case we're going  to be seeing if the current field is equal to   Choice One if so then it'll go to the second  value value so let's add in our expression first if current field is equal to Choice One  and we'll end it with a comma because that lets   it know we are done with our expression and now  it's going to be moving to the second parameter   for our if statement so if the current field is  equal to Choice one it's going to apply this value   to our background color so let's set it to Green  so this applies a green background to Choice one   and if it's not Choice one we can do yellow just  to see how this looks now there's an error with   this and it's actually because I'm using double  quotes and I need to use single quotes here and   the reason it's throwing an arrow with that is  because I started this whole thing with double   quote so as soon as it sees a double quote inside  here it gets confused because it doesn't know   if the double quote that was here is supposed to  match up with this or some some other one so what   you'll do is inside your double quotes you can  use these single quotes to surround your string   so we've got a a basic if statement here I'm going  to save this if Choice one is the value it's going   to be a green background and if it's not Choice  one it's going to be a yellow background we can   do a quick save over in SharePoint and we can  change the value and just see is that what's   happening and it and it is and you even see that  formatter being applied inside the details pain   for this list item as well so that'll take care  of choice one but we want to change the color   for all three options what we're going to do  with that is we're going to get rid of yellow   and now if the choice is not green instead of just  spitting out a particular color we're going to add   another if statement because these can be nested  so we'll do another if we'll add our parenthesis   and we'll add another expression if current  field is equal to again single quotes Choice two then our comma to move on to what color we  want this to be let's add this let's set this   to be yellow and now we go to what if it's not  Choice 2 now again we're going to add a third if statement if with parenthesis to start out with  now we add our expression if current field is   equal to Choice 3 now if it's Choice 3 we will set  it to let's just use blue for something different   and if it's not choice three well it doesn't  really matter in our example we only have those   three choices but maybe you wanted a default one  of white as the background so it just kind of   looks like normal you know a SharePoint background  so let's go back through this whole expression and   walk through what this thing is doing so we're  setting the background color to if the current   field is equal to Choice one then we're going to  be using green if it's not Choice one then we go   to our second expression if it's equal to Choice  two then it's going to be yellow and if it's not   Choice One or Choice two then we go to our third  expression if it's equal to choice three now it's   going to be blue and if it's not any one of  these like maybe it's a fourth value that's   been added it's going to be white it won't have  any formatting applied to the background color   and you can keep nesting these if you would like  so now we're using basically conditional logic   is what this is and this is the same as you could  do through the conditional logic formatter in as   part of the SharePoint UI but we're doing this all  by hand which gives us a lot more power and most   importantly understanding of how these things  work so let's save the vs code file now notice   I'm not in the formatting window right now if  I click on this it's still the old formatters   so if I go back into format this column and let's  see what's in this window everything's already in   here because as soon as I open the formatter pane  back up then vs code copied those values straight   into here let's just save this and edit our list  item and see what the options are and everything   is working Choice 2 is yellow Choice 3 is blue  obviously the text color for Choice 3 isn't even   legible it's just not uh a great color combination  well let's fix that so let's go back into the formatter and let's add in another style  tag let's control the foreground color   with another expression so so we'll set the  color we'll do another set of if statements   now I'm all for shortcuts so here's what I'm  going to do I'm just going to copy this whole   expression and we'll paste it right in  here because this is already giving us   the decisions we need to make is it Choice  one choice 2 choice three or some new choice   but now we're going to be applying this to  the foreground color so if it's green then   we want to set this text to let's see black  yep black looks fine you see it updated over   there on the preview pane if it's Choice  two then we're going to make sure this is black and we know that for choice three we need  to change the foreground color so we will set   this to White and our default is white but we  actually want this to be black because we know   that the background color is going to be white by  default so just to make sure we can read the text   of any new value the foreground color for the  default is going to be black so we'll save our   file we will save this formatting pane everything  is applied and saved to the column now let's edit   this list item and this is looking great this is  already a big Improvement we can read all of the   text here now the yellow and black looks good  the blue and the white looks good the green is   a little bit hard to read what if we wanted  to set bold on this field so that the black   stood out a little bit more against the green  background we know how to make something bold   let's add another comma there so we can start a  new line we'll go to font weight just like we did   before in this case we're just going to apply  bold if the choice is Choice One and that's   easy to do with another if statement so we'll  add equals if and then our set of parenthesis   we will check for current field whether it  is set to Choice one and if so we're going to add bold if it's not Choice one then we'll set  it to normal now let's save this so the format   is back open all of our changes have been saved  there's our new font weight let's save this and   then open the details pain and see how it looks  so Choice 2 looks fine choice three looks fine   that notice they're not bold and then there's our  green and it's got bold it's standing out better   so that's how you can apply CSS properties  selectively depending on the current value   and if you want to see like a syntax reference or  something for Json formatting in the description   of this video you'll find those links don't  forget to subscribe to the channel so you know   when these new videos are coming out because  if you like this kind of content absolutely   keep creating these videos and if you want to see  what's possible with more complex Json I've made   videos highlighting some outstanding examples  click up into this playlist to learn [Music] more
Info
Channel: Steve Corey - MVP
Views: 10,457
Rating: undefined out of 5
Keywords: sharepoint list formatting, json formatting examples sharepoint, json formatting sharepoint online list, sharepoint json formatting, sharepoint list formatting json, sharepoint online json formatting, sharepoint view formatting json examples, sharepoint view formatting json generator, sharepoint view formatting json hide column, sharepoint view formatting samples, steve corey, sharepoint list formatting tutorial, sharepoint json formatting tutorial
Id: 2zkQYEjmdRk
Channel Id: undefined
Length: 23min 20sec (1400 seconds)
Published: Mon Oct 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.