admin管理员组文章数量:1384098
I'm trying to get list items from a SharePoint list with specific fields only. The list contains lookup fields to other lists and I need the LookupId only for these.
I know two cases to narrow down the returned fields.
The first is with CAML. I can use the LookupId="TRUE" option to suppress the LookupValue in this case, but the server returns other system fields (eg. Modified By, Created By, etc.) which I don't need to.
The second is using 'Include' in ClientContext.load method. In this case the server not sends the system fields but I don't know how to specify (if possible) to receive the LookupId only for lookup fields. I tried several versions in Include (project.id, projectId, project.lookupId, project.Inlude(id)) but none of them worked.
So, my question is: how to retrieve only specified fields of a SharePoint list with LookupIds only for lookup fields?
I'm trying to get list items from a SharePoint list with specific fields only. The list contains lookup fields to other lists and I need the LookupId only for these.
I know two cases to narrow down the returned fields.
The first is with CAML. I can use the LookupId="TRUE" option to suppress the LookupValue in this case, but the server returns other system fields (eg. Modified By, Created By, etc.) which I don't need to.
The second is using 'Include' in ClientContext.load method. In this case the server not sends the system fields but I don't know how to specify (if possible) to receive the LookupId only for lookup fields. I tried several versions in Include (project.id, projectId, project.lookupId, project.Inlude(id)) but none of them worked.
So, my question is: how to retrieve only specified fields of a SharePoint list with LookupIds only for lookup fields?
Share Improve this question asked Oct 11, 2015 at 17:07 h2oh2o 211 silver badge2 bronze badges2 Answers
Reset to default 4You were very close to archive the desired result, you need to bine two techniques in order to retrieve id part of lookup value as demonstrated below:
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(createLookupQuery('Project'));
ctx.load(items,'Include(Project)');
ctx.executeQueryAsync(
function(){
if(items.get_count() > 0){
var item = items.getItemAtIndex(0);
console.log(item.get_fieldValues()['Project'].get_lookupId());
}
},
function(sender,args)
{
console.log(args.get_message());
});
function createLookupQuery(lookupFieldName){
var qry = new SP.CamlQuery();
qry.set_viewXml("<View><ViewFields><FieldRef Name='" + lookupFieldName + "' LookupId='TRUE' /></ViewFields></View>");
return qry;
}
- In CAML query we specify attribute
LookupId='TRUE'
to retrieve Id part of lookup field value - since CAML query includes also system field values, we utilize
SP.ClientContext.load method
to return specific field value only
how to retrieve only specified fields of a SharePoint list with LookupIds only for lookup fields?
The question is a bit unclear, but assuming you want to fetch the lookup id, you can do as follows.
Suppose you've retrieved the SP.ListItem
with JSOM and stored it in to a variable, your lookup field will be representeed by a SP.LookupFieldValue object.
You can access it from the retrieved item
object as below.
var lookupFieldValue = item.get_item('FieldName');
var lookupId = lookupFieldValue.get_lookupId();
本文标签: javascriptHow to retrieve only lookup id in Sharepoint with JSOMStack Overflow
版权声明:本文标题:javascript - How to retrieve only lookup id in Sharepoint with JSOM - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744506488a2609620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论