1

I work for an organization that has crippled much of the functionality when using SharePoint 2013. Furthermore, I am still rather new to SharePoint.

I am seeking a way to apply conditional formatting to my list, such that when a date field has become greater than the current date (i.e. expired), the field turns red.

There is no column settings for me to edit, and I cannot use SharePoint Designer. Any thoughts? I also tried using InfoPath, but that only modify's the form, not the list.

Not sure if I can use JS or HTML to do what I want?

All the constraints:

a. Uploading of solution files (wsp) are prohibited.

b. Uploading or running any type of executable files are prohibited.

c. Use of workspace templates are prohibited.

d. Custom 3rd party templates including but not limited to; site, page, list, library, app, web part are prohibited.

e. Use of 3rd party SharePoint Apps is prohibited.

f. Use of Access Apps are restricted.

g. Use of Performance Point Services is prohibited.

i. Running PowerShell scripts are prohibited.

j. Uploading of media (audio/video) files are prohibited.

k. Farm level search configuration changes are prohibited.

l. Social features are restricted (Follow, My Site, My Task, etc.).

m. Upload size is limited to 250MB.

n. List threadshold is capped at 5,000 items.

o. Any use of SharePoint features that are not supported by the Web Presence & Collaboration team is prohibited.

------------- UPDATE -------------

So I have a JS which I believe should simply bold some text. This is not my code, I found it online and simply modified for my site field. I have no experience with JS (but do have programming experience in other languages).

(function () {

//   Initialize the variables for overrides objects
    var overrideCtx = {};
    overrideCtx.Templates = {};

    alert("Override call worked"); // This works, so I know file is read

    //  Use BaseViewID and ListTemplateType to narrow focus/scope on 
    //  which web parts on the page are affected
    //  overrideCtx.BaseViewID = 1;
    //  overrideCtx.ListTemplateType = 100;

    /*
     * Using the Fields override leaves the rest of the rendering intact, but 
     * allows control over one or more specific fields in the existing view
     */
    overrideCtx.Templates.Fields = {
        'Site': { 'View' : '<b><#=ctx.CurrentItem.MoreText#></b>' }
    };

    /*
     * Register the template overrides.
     */
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

I placed the JS file in my master pages folder following this guide. I have tried linking the file, but nothing happens. No errors, no change of any kind. I feel like I may be missing something very obvious here?

4
  • That looks a little confusing - you are registering an override for a field with the internal name Site, but what you are telling it to render (instead of doing the default rendering for the value of the Site field) is the value of a different field with the internal name of MoreText. Is that really what you want to do? Also, have you uncommented the alert to make sure the override is getting called? Apr 2, 2019 at 15:41
  • To be honest, I just pulled this from a website guide, so not really sure what's going on in the code. I just wanted to do something super simple, to see if I can get this functionality to work. I am very ignorant in JS (my apologies). I will uncomment the alert as you have suggested. Should I change .MoreText to .Site?
    – Shinobii
    Apr 2, 2019 at 15:52
  • I uncommented the Alert and it worked! Nothing happened to the text, but the Alert did pop up! Some success!
    – Shinobii
    Apr 2, 2019 at 15:58
  • Great! You could try .Site, or you could also try ctx.CurrentItem[ctx.CurrentFieldSchema.Name]. Also, I have never really done it using that <#= #> replacement token notation. I usually register a function, build the HTML string in the function and return the string. That also gives you the ability to set a breakpoint in your function to see if the override got registered correctly. Where that alert is in your code only tells us that the script did get loaded. Apr 2, 2019 at 18:11

2 Answers 2

2

This sounds like it might be a situation where you can use Client Side Rendering.

If you are unfamiliar with CSR, I highly recommend reading this article to get an understanding of what you can do with it. Conditional formatting is one thing it's really good at, and I'm sure you'll be able to find many resources (probably a lot on this site) that have examples of using CSR to do conditional formatting based on a date.

To address some of the other obstacles you are facing, you should know that there is a whole sort of development pattern with SharePoint where you write JS/CSS/HTML, put those files in a document library on a site somewhere, and pull them to where you need them using various OOB webparts.

Typically the document libraries people tend to use are the built in Site Assets or Style Library libraries. Using the Style Library will give you version control (it's turned on for the Style Library by default), which gives you a couple advantages:

  • Version control
  • Any updates you make to your files there will be treated as a minor version, which means you will be able to see the effects of your updates, while everyone else will not, until you publish a major version. This will let you tweak things and fix bugs before "releasing" your updates to the rest of the site users by publishing a major version.

That being said, I personally tend to use the Site Assets library, because I have a different system for version control, and to me "style library" sounds like it should be limited to CSS files, and HTML and JS code files fall under the umbrella of "assets".

But anyway - you can upload the files you need there, and then link to them via various OOB methods. In the case of CSR, you can use JSLink. JSLink is a property that is exposed on many SharePoint objects that lets you "attach" JS files to them such that whenever that object gets loaded, the JS files you defined in the JSLink property also get loaded. This is perfect for CSR, because that's exactly what you want to happen for CSR.

Say you had a custom field that you wanted to apply conditional formatting to. In your field definition, you just set the JSLink property to include the path to your CSR file, and whenever that field shows up (in list views, in forms, etc.) your CSR file will get loaded. Now, you say you can't deploy WSPs, so no custom field definitions for you. You also say you can't use Powershell, so you can't use that to set the JSLink property of the field directly in the site. (If you can use Powershell, it is definitely an option you could use to set JSLink properties.)

Not to worry - there is a JSLink property exposed as a text box in the List View Web Part editor pane, so you can get to it through the UI. Just go to the list where you want to apply the CSR, Edit the LVWP, scroll down in the editor pane to the Miscellaneous section, and set the JSLink property to point to your CSR script (wherever it happens to be on your site).

Keep in mind - and I'm not 100% sure of this, but I think it's the case - when you set the JSLink property that way, it's not setting it for the entire list (as you would think), it's only setting it for the currently selected View. So if someone changes views or creates a new view, your CSR conditional formatting will disappear (until you go in and re-link it through the JSLink for the new view).

So that's how I would do it - make a CSR script to do the conditional formatting, upload it to the Site Assets library, go the the list I want it applied to and set the JSLink property in the List View Web Part.

JSLink can also be used to pull in non-CSR scripts, so if you had to get some other JS pulled in, you could use JSLink, but there is another very common pattern people use: the Content Editor Web Part.

When you add a Content Editor Web Part (CEWP) to a page and edit it, right at the top of the editor pane is a "Content Link" textbox. If you put the path to a file in there, when the page loads, the CEWP will inject the contents of that file directly into the HTML of the page, at the position where the CEWP is on the page. If you link to a pure JS file, it will dump your code as text into the page, so you will probably want to wrap your code in <script> tags so that it's treated as inline code.

However, it's also really useful to be able to inject a little HTML into a page, and in fact you can use HTML <link>s to stylesheets and <script src=""> script links to pull in other files.

Just today I used that method to jQuery-accordion-ize a long Edit form. I uploaded an HTML file, a CSS file, and a JS file to the Site Assets library. I went to the Edit form in question, added a CEWP at the top of the page, and added the path to the HTML file in the CEWP Content Link.

My HTML file looked something like this:

<link rel="stylesheet" href="/sites/MySite/SiteAssets/CSS/jquery-ui.min.css">
<link rel="stylesheet" href="/sites/MySite/SiteAssets/CSS/MyCustom.css">
<script type="text/javascript" src="/sites/MySite/SiteAssets/Scripts/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="/sites/MySite/SiteAssets/Scripts/jquery-ui.min.js"></script>
<script type="text/javascript" src="/sites/MySite/SiteAssets/Scripts/MyCustomForm.js"></script>

<div id="accordion">
    <h3>Section 1</h3>
    <div id="Section1"></div>

    <h3>Section 2</h3>
    <div id="Section2"></div>

    <h3>Section 3</h3>
    <div id="Section3"></div>
</div>

and in the MyCustomForm.js file I had all the code necessary to pull the different form controls out of the regular form, put them in the new sections in my custom HTML, and then accordion-ize the while thing.

If I need to update the script or anything, I can just update the JS file and upload it to the Site Assets, and everything should update right away because the linkage through the CEWP to the HTML file is still there, and the linkage from the HTML file to the script file is still there.

Another OOB web part you can use is the Script Editor Web Part, which gives you a little editor window where you write JS or link to other files (CSS or JS). I'm not a fan of the Script Editor because I am used to all the benefits of using a real code writing tool (VS Code or VS), and the editor window is tiny and things get jumbled up pretty quick. But, it is a viable OOB solution to get code onto a page.

1
  • Wow, much appreciate the level of detail. Indeed, this was the solution I have been looking into. Your answer really clears up the confusion I had. I am currently working on a solution as we speak. I will let you know how it goes!
    – Shinobii
    Apr 1, 2019 at 21:26
2

This article should help you out. Find the column on which you would like to customize and use this article to format it using JSON. You will see the JSON editor within the column's settings.

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

2
  • No, I do not have access to Column Settings unfortunately. So this method, which would be so dang easy, will not work.
    – Shinobii
    Apr 2, 2019 at 14:27
  • Ohh bummer :(, yeah that's literally super easy if you can get to the column settings. Perhaps ask your site admin to do it? Quick change with improved visual cues. Apr 2, 2019 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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