2015-03-02 123 views
1

假設我有2所列出:員工。每隊有許多員工:更新ProjectedField與SharePoint JOIN使用JavaScript客戶端對象模型

Teams 
    ID 
    Name 

Employees 
    ID 
    Name 
    TeamID (foreign key of Teams) 

如果我創建的員工LEFT JOIN,可能我再set_itemupdate在團隊中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領域:

var first = listItemsCollection.get_data()[0]; 
first.set_item("TeamName", "something"); 
first.update(); 

ctx.executeQueryAsync(onSuccess, onError); 

,是否支持? (我目前還沒有嘗試這種方式)

如果不是,可以選擇什麼?

回答

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); 
} 
相關問題