【问题标题】:Sharepoint clientContext currentUser in CAML queryCAML 查询中的 Sharepoint clientContext currentUser
【发布时间】:2018-10-10 18:47:29
【问题描述】:

我目前正在使用多种方式来获取 SharePoint 2013 中的当前用户,以便我可以自动查询登录用户的数据集。

我遇到了一个奇怪的错误,如果我没有在我的 retrieveListItems 函数中使用警告框,我会得到 SCRIPT438:对象不支持属性或方法“get_$y_0”。如果我放一个简单的 alert('Hi');在此函数开始时,我没有收到错误,并且我的图表为用户正确呈现。

下面是我的(草率的)代码,但如果有人能提供任何关于它为什么会这样做以及我如何确保在不使用任何警报框的情况下解决错误的见解,我将非常感激。

ExecuteOrDelayUntilScriptLoaded(init,"sp.js");

var currentUser;


function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate (this,this.retrieveUser), Function.createDelegate(this,this.onQueryFailed2));
}

function onQueryFailed2(){
    alert('2');
}

function retrieveUser(){
    currentUser = currentUser.get_title();
    //alert(currentUser);
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(
        Function.createDelegate(this,this.retrieveListItems),
        Function.createDelegate(this,this.onQueryFailed2)
    )
}

    //retrieve list data from above sharepoint site based on List Name

function retrieveListItems() {

    clientContext.load(currentUser, 'Title');
    //alert('Rendering...');
    //var clientContext = new SP.ClientContext(siteUrl);  

    var oList = clientContext.get_web().get_lists().getByTitle('APS Portfolio');
    var camlQuery = new SP.CamlQuery(); 
    //currentUser = 'Fox, Natalie G';
    //clientContext.load(currentUser);                  
    camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='AIT_x0020_App_x0020_Manager'/><Value Type='Text'>" +currentUser+ "</Value></Eq></Where></Query></View>");   
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

我是使用 clientContext 功能的新手,但我非常有兴趣了解有关它的更多信息以及我可以做些什么来解决此错误。

谢谢!

【问题讨论】:

    标签: javascript sharepoint caml


    【解决方案1】:

    不太清楚为什么retrieveListItems() 的第一行是:

    clientContext.load(currentUser, 'Title');
    

    无论如何,这个库大大简化了你的代码,所以我推荐这个: https://aymkdn.github.io/SharepointPlus/

    代码类似于:

    $SP().list("My List").get({
      fields:"Title",//whatever fields you want
      where:"Author = '[Me]'"//[Me] is the current user that is logged in
    }).then(function(row) {
      console.log(row[0].getAttribute("Title"));
    });
    

    关于这个问题的解释,我怀疑它与异步有关,即在之前的函数为其分配相关值之前正在使用一个变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多