SPClientTemplates未定义

时间:2018-09-04 16:53:30

标签: javascript sharepoint-online

我想通过在sharepoint的JS链接中上传一个javascript文件来自定义sharepoint中的列表。所以我写了下面的代码。 该代码的目的是获取Nom_x0020_Du_x0020_Projet列的值,该值是我要在sharepoint中自定义的列表的列的内部名称,它实际上是一个链接,后跟其描述。因此,代码将该列中项目的值拆分为一个具有两个值的数组:地址和说明。

(function(){
      // Create object that have the context information about the field that we want to change it's output render
      var nom_projetContext = {};
      nom_projetContext.Templates = {};
      nom_projetContext.Templates.Fields = {
        //Apply the new redenring for nom du projet field on the list view
        "Nom_x0020_Du_x0020_Projet" : {"View" : nomProjetModifie}
      };
      SPClientTemplates.TemplateManager.RegisterTemplateOverrides( nomProjetModifie);
})();
var adresse;
//this function provides our purpose
function nomProjetModifie(ctx){
  var nomComplet = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];

  //slice nomComplet into to substrings
  var arrayOfSubstrings = nomComplet.split(",");
  adresse = arrayOfSubstrings[0];
  var description = arrayOfSubstrings[1];

  return "<span onclcik='redirection()'>"+description+"</span>";
}

function redirection(){
  document.location.href = adresse;
}

但是无论如何,我都遇到相同的错误“未定义SPClientTemplates”。 我该如何解决?

1 个答案:

答案 0 :(得分:0)

我已经找到了解决问题的方法。 首先,我们必须执行以下语句:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function(){
    //All our code goes here
})

所以对于我的问题,最终的代码将是这样

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function(){
    (function(){
  // Create object that have the context information about the field that we want     to change it's output render
  var nom_projetContext = {};
  nom_projetContext.Templates = {};
  nom_projetContext.Templates.Fields = {
    //Apply the new redenring for nom du projet field on the list view
    "Nom_x0020_Du_x0020_Projet" : {"View" : nomProjetModifie}
  };
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides( nomProjetModifie);
})();
var adresse;
//this function provides our purpose
function nomProjetModifie(ctx){
  var nomComplet = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];

  //slice nomComplet into to substrings
  var arrayOfSubstrings = nomComplet.split(",");
  adresse = arrayOfSubstrings[0];
  var description = arrayOfSubstrings[1];

  return "<span onclcik=\'redirection()\'>"+description+"</span>";
}

function redirection(){
  document.location.href = adresse;
}
}) 

它有效,并且我不再收到此错误消息