SharePoint JavaScript CSOM:处理查找字段的最佳方式? [英] SharePoint JavaScript CSOM: Best way to deal with Lookup fields?

查看:79
本文介绍了SharePoint JavaScript CSOM:处理查找字段的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 CSOM 的新手(到目前为止我一直在使用 SPServices),我正在尝试学习获取列表项的基础知识.我有一个包含很多列的列表,其中一些是对其他列表的查找.当我尝试获取并打印查找列之一时,它显示为 [Object object].我猜测查找字段将作为包含查找列表中整个条目的对象返回.那是对的吗?如果是这样,从对象中获取我想要显示的字段的最佳方法是什么?请参阅下面的代码:

I'm new to CSOM (I've used SPServices up to this point), and I'm trying to learn the basics to getting list items. I have a list with lots of columns, several of which are lookups to other lists. When I try to get and print out one of the lookup columns, it comes out as [Object object]. I'm guessing that the lookup fields are getting returned as objects that contain the whole entry from the lookup list. Is that correct? If so, what's the best way to get the fields that I want to display from the object? See my code below:

<script src="/_layouts/1033/init.js" type="text/javascript"></script>
<script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/sp.core.js" type="text/javascript"></script>
<script src="/_layouts/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/sp.js" type="text/javascript"></script>
<script type="text/javascript" src="../SiteAssets/js/jquery-1.12.3.min.js"></script>

<SCRIPT type=text/javascript>
$(document).ready(function() {

function GetListItemsFromSPList(listId) {  
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getById(listId);
    var query = SP.CamlQuery.createAllItemsQuery();
    var allItems = list.getItems(query);
    context.load(allItems, 'Include(Id, ContractType)');
    context.executeQueryAsync(Function.createDelegate(this, function () { onQuerySuccess(allItems); }),
        Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySuccess(allItems) {
    var listItemInfo = '';
    var ListEnumerator = allItems.getEnumerator();
    while (ListEnumerator.moveNext()) {
        var currentItem = ListEnumerator.get_current();
        listItemInfo += '\nID: ' + currentItem.get_id() +
        '\nContractType: ' + currentItem.get_item('ContractType');

    }

    alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
    alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}

var listId = "{STRING-OF-LISTID-CHARACTERS}";
GetListItemsFromSPList(listId);

});

</SCRIPT>

警报打印出来:编号:1合同类型:[对象对象]

The alert prints out: ID: 1 ContractType: [object Object]

推荐答案

Lookup 字段存储具有两个属性的对象(例如 5;#someValue)

The Lookup field stores object (e.g. 5;#someValue) that has two properties

  1. 查找 ID (5)
  2. 查找值 (someValue)

请尝试通过下面提到的方式获取查找值.

Please try to get lookup value by below mentioned way.

var lookupObject = oListItem.get_item('lookupFieldName');
var lookupValue = lookupObject.get_lookupValue();
var lookupId = lookupObject.get_lookupId();

这篇关于SharePoint JavaScript CSOM:处理查找字段的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆