1

我正在使用 PNP SP 来查询列表项及其附件。如何编写当前函数的正确且简化的版本?

下面的函数有效,我从第一个查询中得到结果,它检索 ID、TITLE、LINK。但是我没有从第二个查询 Title、Link.URL、FileName、ServerRelativeUR 中得到任何结果——但是当我调试代码时,我可以看到第二个查询正在执行并返回值,但函数在完成第二个查询之前就离开了。如何让这个函数正确地查询这两个东西并将它们返回给调用者?

private GetCompleteData() : Promise<any>
{
    let result : string = "";

    return sp.web.lists.getByTitle('LIST').items.select('Id, Title, Link').get().then( response => {
      response.forEach( item => {
        let attachments = sp.web.lists.getByTitle('LIST').items.getById(item.Id);        

        attachments.attachmentFiles.select('FileName, ServerRelativeUrl').get().then( responseAttachments => {
          responseAttachments.forEach( attachmentItem => {
            result += item.Title                       + "<br/>" +
                      item.Link.Url                    + "<br/>" + 
                      attachmentItem.FileName          + "<br/>" + 
                      attachmentItem.ServerRelativeUrl + "<br/><br/>";
          });
        });
      });

      return result;
    });
}
4

2 回答 2

2

expand您可以使用该属性在第一个 REST 调用本身中获取数据,而不是对每个列表项进行REST 调用。

您可以从以下示例代码中对其进行修改:

sp.web.lists.getByTitle("LIST").items.select("Title", "ID", "Links" "AttachmentFiles").
expand("AttachmentFiles").get().then((response) => {
      console.log(response);
});

您可以根据需要对其进行修改。这将为您提供一系列列表项的附件,如下所示:

在此处输入图像描述

于 2018-08-01T12:33:09.880 回答
-1

这是我的有效测试代码。

sp.web.lists.getByTitle('MyList').items.select('Id', 'Title', 'EncodedAbsUrl').get().then( response => {
      response.forEach( item => {
        let _Item = sp.web.lists.getByTitle("MyList").items.getById(item.ID);      
        _Item.attachmentFiles.select('FileName', 'ServerRelativeUrl').get().
        then( responseAttachments => {
          responseAttachments.forEach( attachmentItem => {
            result += item.Title                       + "<br/>" +
            item.EncodedAbsUrl                    + "<br/>" + 
            attachmentItem.FileName          + "<br/>" + 
            attachmentItem.ServerRelativeUrl + "<br/><br/>";
          });
        });        
      });
    })
于 2018-07-13T01:39:25.213 回答