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();