0

I want to change the value of one of my column in SharePoint list with the help of jslink. For that I have done

enter image description here

and linked this file with webpart using

enter image description here

but for me, the function "ViewTemplate(ctx)" is not firing. Can anybody suggest a solution for my problem.

1
  • Double check if your JS is loading properly...May be Place an alert/console log at the top in your JS file to verify the same. Commented Aug 1, 2017 at 9:41

2 Answers 2

2

May be MDS is enabled on your SharePoint site. In this case you will need to use RegisterModuleInit() function.

You need to do two modifications to achieve this.

First, you need to change the anonymous entry point function to a function with an entry point signature. Then, you need to call RegisterModuleInit() function to register an override of this code on MDS enabled sites.

For example:

RegisterModuleInit("CSRAssets/CSRFieldType.js", RegisterFavoriteColor); // CSR-override for MDS enabled site
RegisterFavoriteColor(); //CSR-override for MDS disabled site (because we need to call the entry point function in this case whereas it is not needed for anonymous functions)

function RegisterFavoriteColor() {
    var favoriteColorContext = {};

    // You can provide templates for:
    // View, DisplayForm, EditForm and NewForm
    favoriteColorContext.Templates = {};
    favoriteColorContext.Templates.Fields = {
        "FavoriteColorField": {
            "View": favoriteColorViewTemplate
        }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
        favoriteColorContext
        );
}

// The favoriteColorViewTemplate provides the rendering logic
//  the custom field type when it is displayed in the view form.
function favoriteColorViewTemplate(ctx) {
    var color = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
    return "<span style='background-color : " + color +
        "' >&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;" + color;
}

Reference: https://blogs.msdn.microsoft.com/sridhara/2013/02/08/register-csr-override-on-mds-enabled-sharepoint-2013-site/

1

Try putting an alert in the beginning of the ViewTemplates function. I have shared the implementation that assigns values to Column C by comparing the values for Column A & Column B using CSR and JSLink.

function TargetProgress(ctx) {
var totalSalesVal =ctx.CurrentItem.Total_x0020_Sales.replace(",", "");
var targetSalesVal =ctx.CurrentItem.Sales_x0020_Target.replace(",", "");

  if(parseInt(totalSalesVal) > parseInt(targetSalesVal))
  {
     return "On Target";
  }
  else if(parseInt(targetSalesVal)- parseInt(totalSalesVal) <=1000 )
  {
     return "Reaching Target";
  }
  else
 {
     return "Long way to target !";
  }
}

Assign Column Values

1
  • 1
    Please add more details from your link and add the link as a reference to avoid the broken links in the future, thanks! Commented Aug 1, 2017 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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