使用Javascript客户端对象模型在Sharepoint中使用JOIN更新ProjectedField

时间:2015-03-02 10:24:11

标签: sharepoint sharepoint-clientobject

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

Teams
  ID
  Name

Employees
  ID
  Name
  TeamID (foreign key of Teams)

如果我创建了员工 LEFT JOIN 团队的加入查询,那么我可以set_itemupdate {{1}团队中的字段?

Name

然后,更新var ctx = SP.ClientContext.get_current(); var list = clientContext.get_web().get_lists().getByTitle('Employees'); var queryText = "<View>" + "<Query></Query>" + "<ProjectedFields>" + "<Field Name='TeamName' Type='Lookup' List='Team' ShowField='Name' />" + "</ProjectedFields>" + "<Joins>" + "<Join Type='INNER' ListAlias='Team'>" + "<Eq>" + "<FieldRef Name='TeamID' RefType='Id'/>" + "<FieldRef List='Team' Name='ID'/>" + "</Eq>" + "</Join>" + "</Joins>" + "<ViewFields>" + "<FieldRef Name='TeamName'/>" + "</ViewFields>" + "</View>"; var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml(queryText); var listItemsCollection = list.getItems(camlQuery); ctx.executeQueryAsync(onSuccess, onError); 字段:

TeamName

支持吗? (我目前无法尝试这个)

如果没有,可能有什么替代方案?

1 个答案:

答案 0 :(得分:0)

不,只有那些包含列表项的字段才能更新。但您可以考虑使用以下方法更新投影字段

由于投影字段作为查找值返回,您可以检索投影列表项并更新其属性,如下所示:

var listTitle = 'Employees';
var joinListTitle = 'Teams' 
var joinFieldName = 'TeamID';
var projectedFields = ['Name'];


getListItems(listTitle,joinListTitle,joinFieldName,projectedFields,
  function(items){

     var item = items.get_data()[0]; //get first item
     var itemId = item.get_item('TeamsName').get_lookupId(); //get projected list item id
     var propertiesToUpdate = {'Name': 'New Team Name'};

     updateListItem(joinListTitle,itemId,propertiesToUpdate,function(item){
        console.log('List Item has been updated');
     },
     logError);


  },
 logError);

,其中

function createJoinQuery(joinListTitle,joinFieldName,projectedFields,joinType)
{
   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='{2}' 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,joinType));
    return qry;
}

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,'INNER'));

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



function updateListItem(listTitle,itemId, propertiesToUpdate,success,error)
{
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var list =  web.get_lists().getByTitle(listTitle);
    var listItem = list.getItemById(itemId);
    for(var name in propertiesToUpdate) {
        listItem.set_item(name,propertiesToUpdate[name]);   
    }
    listItem.update();
    ctx.executeQueryAsync(function() {
       success(listItem);
    },error);
}