2

Here is the scenario, that you can reproduce very easily :

  1. Create a Blog subsite on your SharePoint Web.
  2. Add a post and set the "Published" DateTime field (internal name: PublishedDate)
  3. Go to the home page of your Blog site, which should normally display the ListView webpart with the "All Posts" view. You'll observe that the PublishedDate is displayed.
  4. Now create a JSLink (add a "JavaScript Display Template" in your Masterpage gallery) to override that view. Here's my code :

    (function () {
    
        var myCtx= {};
        myCtx.Templates = {};
        myCtx.Templates.Fields = {
            "PublishedDate": { 
                "View": func1
            },
            "Title" : {
                "View": func2
            },
            "Author": {
                "View": func3
            }
        };
    
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
            myCtx
        );
    })();
    
    function func1(ctx) {
        return "I replaced PublishedDate";
    }
    
    function func2(ctx) {
        return "I replaced Title";
    }
    
    function func3(ctx) {
        return "I replaced Author";
    }
    

If you did everything correctly, then when you go back to your Blog home page, you'll see the post's title and author replaced. However the PublishedDate field is not replaced.

I've found out that something strange is going on in SharePoint's native javascript: if you enable the debug verison of SP's .js files and debug the script in your browser, you'll see that SP iterates on the fields to render ("Author", "Title", "PublishedDate"...) but for PublishedDate it actually compares it to PublishedDate.LongDate. Therefore the strings never match and the rendering override never happens for that field.

So, what is going on here? Is it a bug? Am I doing something wrong, or is there a workaround? I could (maybe) fix the field in PostRender but that's a really dirty hack.

More generally, the question is: how to override the rendering of fields that are transformed beforehand by SharePoint, which calls a method of the field object?

  • I've found a workaround by adding this into func2: ctx.CurrentItem["PublishedDate.LongDate"] = "Whatever value"; Since ctx.CurrentItem is populated only once at the beginning of the rendering, it works. – M.B. Sep 14 '16 at 15:26

Your Answer

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

Browse other questions tagged or ask your own question.