簡體   English   中英

Sharepoint 2013將查找值添加到另一個列表Java列表中

[英]Sharepoint 2013 Add lookup value to list from another list Javascript

我嘗試使用Javascript在SharePoint 2013中使用以下代碼添加列表項;

function createListItem(var lookup_Value) {

var clientContext = new SP.ClientContext.get_current();

var oList = clientContext.get_web().get_lists().getByTitle('CrewNoticesAudit');

var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);

oListItem.set_item('Title', 'My New Item!');
oListItem.set_item('NoticeId', lookup_Value);  // THIS IS A LOOKUP VALUE
oListItem.set_item('User', 'administrator');
oListItem.set_item('TimeStamp', new Date());
oListItem.set_item('AuditType', 'Open');

oListItem.update();

clientContext.load(oListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), 
Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

問題是當我嘗試運行此代碼時,出現一個錯誤“您嘗試更新的字段可能是只讀的”,我需要從查找列表中獲取ID而不是查找值,是否存在一種從僅具有查找值的列表中獲取ID的方法?

一種解決方案是從查找表(在本例中為allItems)中獲取所有值,並將它們與所需的valuecomparisonItemName()進行比較,從而獲取ID。 如果查找表很小,則不會花費很長時間。

 var lookupVar = new SP.FieldLookupValue();    
  for (var i = 0; i < allItems.length; i++) {
        if (allItems[i].get_item("ItemName") == comparisonItemName) {
            lookupVar.set_lookupId(allItems[i].get_id());
        }
    }

設置項目的值時,請使用lookupVar。

PS為了獲得確切的項目,請編寫CAML以獲得所需的項目,然后獲得ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM