假设我有两个列表:团队和员工。每个团队都有许多员工:

Teams
  ID
  Name

Employees
  ID
  Name
  TeamID (foreign key of Teams)


是否可以在SharePoint JSOM中编写查询,以便我可以按照以下几行做一些事情(查询执行/加载之后):

var employeesListItems = teamListItem.get_item("Employees")


SharePoint对象模型是否以任何方式支持此功能?

澄清:我的意图是尽可能多地重用ClientObject。我知道我可以查询所有员工和所有团队,为每个员工创建一个自定义对象数组,然后遍历员工并将其推送到相关Team对象的“ Employees”字段中。我想避免这样做。

最佳答案

即使SharePoint CAML支持List Joins and Projections,在这种情况下,我还是建议您使用其他方法。

下面的示例演示如何使用单个请求检索父项/子项:

function getItemWithDetails(parentListTitle,childListTitle,lookupFieldName,lookupFieldValue,success,error)
{
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var lists = web.get_lists();
   var parentList = lists.getByTitle(parentListTitle);
   var parentItem = parentList.getItemById(lookupFieldValue);
   var childList = lists.getByTitle(childListTitle);
   var childItems = childList.getItems(createLookupQuery(lookupFieldName,lookupFieldValue));

   ctx.load(parentItem);
   ctx.load(childItems);
   ctx.executeQueryAsync(
     function() {
       success(parentItem,childItems);
     },
     error
   );
}

function createLookupQuery(lookFieldName,lookupFieldValue)
{
   var queryText =
"<View>" +
  "<Query>" +
    "<Where>"  +
       "<Eq>" +
           "<FieldRef Name='{0}' LookupId='TRUE'/>" +
           "<Value Type='Lookup'>{1}</Value>" +
        "</Eq>" +
    "</Where>" +
 "</Query>" +
"</View>";
    var qry = new SP.CamlQuery();
    qry.set_viewXml(String.format(queryText,lookFieldName,lookupFieldValue));
    return qry;
}


用法

var parentListTitle = 'Teams';
var childListTitle = 'Employees'
var lookupFieldValue = 1;
var lookupFieldName = 'Team';

getItemWithDetails(parentListTitle,childListTitle,lookupFieldName,lookupFieldValue,
  function(teamItem,employeeItems){
     //print parent item
     console.log(teamItem.get_item('Title'));
     //print child items
     for(var i = 0; i < employeeItems.get_count(); i++){
        var employeeItem = employeeItems.getItemAtIndex(i);
        console.log(employeeItem.get_item('Title'));
     }
  },
  function(sender,args){
      console.log(args.get_message());
  });




另一种选择是利用List Joins and Projections。下面的示例演示如何检索具有计划团队项目的员工列表项目

function getListItems(listTitle,joinListTitle,joinFieldName,projectedFields,success,error)
{
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var list =  web.get_lists().getByTitle(listTitle);
   var items = list.getItems(createJoinQuery(joinListTitle,joinFieldName,projectedFields));

   ctx.load(items);
   ctx.executeQueryAsync(
     function() {
       success(items);
     },
     error
   );
}


function createJoinQuery(joinListTitle,joinFieldName,projectedFields)
{
   var queryText =
"<View>" +
  "<Query/>" +
  "<ProjectedFields>";
  for(var idx in projectedFields) {
    queryText += String.format("<Field Name='{0}_{1}' Type='Lookup' List='{0}' ShowField='{1}' />",joinListTitle,projectedFields[idx]);
  }
  queryText +=
  "</ProjectedFields>" +
  "<Joins>" +
      "<Join Type='INNER' ListAlias='{0}'>" +
        "<Eq>" +
          "<FieldRef Name='{1}' RefType='Id'/>" +
          "<FieldRef List='{0}' Name='ID'/>" +
          "</Eq>" +
        "</Join>" +
    "</Joins>" +
"</View>";
    var qry = new SP.CamlQuery();
    qry.set_viewXml(String.format(queryText,joinListTitle,joinFieldName));
    return qry;
}


用法

var listTitle = 'Employees';
var joinListTitle = 'Teams'
var joinFieldName = 'Team';
var projectedFields = ['ID','Title'];

getListItems(listTitle,joinListTitle,joinFieldName,projectedFields,
  function(employeeItems){
     //print items
     for(var i = 0; i < employeeItems.get_count(); i++){
        var employeeItem = employeeItems.getItemAtIndex(i);
        var employeeName = employeeItem.get_item('Title');
        var teamName = employeeItem.get_item('Teams_Title').get_lookupValue();
        console.log(employeeName + ',' + teamName);
     }
  },
  function(sender,args){
      console.log(args.get_message());
  });

关于sharepoint - Sharepoint:如何使用JSOM轻松获取相关子项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25825010/

10-14 16:35