0

我有一个 Sharepoint 2010 列表。每个项目都可以在第二个列表(“扩展”)中有一个“相关项目”。我使用以下代码来确定该相关项目是否存在(使用 list.getItems)。

如果它确实存在,我想读取该项目的 ID(来自“listCollection”)并使用它来打开该项目的显示表单。但是如何从我的 listCollection 中的对象中获取项目的 ID?

    var listCollection;
    function getExtended() {
        try {
            var context = new SP.ClientContext.get_current();
            var web = context.get_web();
            var list = web.get_lists().getByTitle('Extended');
            var query = '<View Scope=\'RecursiveAll\'>'+
                            '<Query>'+
                                '<Where>'+
                                '<Contains>'+
                                    '<FieldRef Name=\'MainId\'/>' +
                                    '<Value Type=\'Integer\'>' + GetId() +'</Value>'+
                                '</Contains>'+
                                '</Where>'+
                            '</Query>'+
                                 '</View>';
            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml(query);

            this.listCollection= list.getItems(camlQuery);
            context.load(this.listCollection, 'Include(MainId, Status, ID)');
            context.executeQueryAsync(Function.createDelegate(this, this.listReceived), Function.createDelegate(this, this.failed));
        }
        catch (e) {
            alert(e);
        }
    }

    function listReceived() {

                   var count=this.listCollection.get_count();
            if (count==0){
                $("#extendButton").html("<input type='button' onclick='NewExt()' value='Create new extended'/>");
            } else {
                $("#extendButton").html("<button>Show extended</button>");
            }

    }
4

1 回答 1

0

It's interesting to re-read my question and not be entirely clear what I was asking myself. But the code I used in the end was simply:

var listItemEnumerator = this.listCollection.getEnumerator();

while (listItemEnumerator.moveNext()) {    
      var oListItem = listItemEnumerator.get_current();
      childId= oListItem.get_item('ID');
} 

Which at least worked, even if I'm handling the (one-item) list in an overly-complex way.

于 2012-05-23T08:24:17.630 回答