如何使用Javascript检查Sharepoint列表中的列值是空还是null?

时间:2017-08-01 20:58:42

标签: javascript sharepoint

到目前为止,我可以根据我传递的字符串甚至列名检索列表,但我无法弄清楚如何获取特定列的值。这是我到目前为止所拥有的。

function GetFieldList()
{
  var listname = document.getElementById("ListName").value;
  var ctx = SP.ClientContext.get_current();
  this.web = ctx.get_web();
  ctx.load(this.web);
  this.list = web.get_lists().getByTitle(listname);
  ctx.load(this.list);
  this.fields = this.list.get_fields();
  ctx.load(this.fields); 
  ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

顺便说一句,我使用的是SharePoint 2010。

3 个答案:

答案 0 :(得分:1)

我认为您的代码未满,客户端Context必须运行async方法来加载值 如果要从SharePoint获取值的正确方法,请阅读以下文档: https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx
或者您可以使用其他库,例如rest api或spservice 无论如何,get_fields()返回字段列表名称而不是值。

答案 1 :(得分:0)

正如您所说SP.ClientContext.get_current();是一个Javascript对象,首先您需要检查您想知道的属性是否为null或为空。

var ctx = SP.ClientContext.get_current();
isPropertyEmptyOrNull = ctx.hasOwnProperty('myProperty') && (prop.myProperty === null or prop.myProperty === '' or prop.myProperty === undefined)

您已找到更多详细信息,请访问hasOwnProperty here

答案 2 :(得分:0)

参考下面的代码。这显示了如何从SharePoint列表中检索项目。

function GetItems() {
    var context = new SP.ClientContext.get_current();
    var list = context.get_web().get_lists().getByTitle('ListName');
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("");
    collItems = list.getItems(camlQuery);
    context.load(collItems);
    context.executeQueryAsync(GetItemsSuccess, GetItemsFail);
}

function GetItemsSuccess() {

    var listItemEnumerator = collItems.getEnumerator();

    if (collItems.get_count() > 0) {

        while (listItemEnumerator.moveNext()) {

            var oListItem = listItemEnumerator.get_current();

            /*
                retrieve specific fields (e.g. Title, FirstName...)
                Here 'Title' and 'FirstName' will be the internal name of the field you want to retrieve
            */
            var title = oListItem.get_item('Title');
            var firstName = oListItem.get_item('FirstName');

        }
    }
}

function GetItemsFail(sender, args) {
    // Error handler code 
}