2

I have a lookup Column in my list which can take multiple values. So I need to count the total number of items added in that lookup field.

What is the simplest way to do this?

2
  • using what js or c#? Commented Nov 23, 2015 at 5:49
  • using js or OOB things..
    – KumarV
    Commented Nov 23, 2015 at 5:51

2 Answers 2

2

As you said in comment that JS solution will do for your. So make a GET request to the the list and $expand the lookup column. Let's lookup column's name is Department

/_api/web/lists/GetByTitle('List Name')/Items?$select=Title,Department/Title&$expand=Department

In response, you will get an array of Departments. Like

"Department": {
      "results": [
        {
          "__metadata": {
            "id": "0e5b6244-0d4a-40d4-a8c6-d125a2ae7e9b",
            "type": "SP.Data.ParentListItem"
          },
          "Title": "B"
        },
        {
          "__metadata": {
            "id": "96abb8a2-151a-4da3-b0b6-fdcff2a2aa51",
            "type": "SP.Data.ParentListItem"
          },
          "Title": "C"
        },
        {
          "__metadata": {
            "id": "b74c8509-7a87-4c55-8b87-c8c4693065e6",
            "type": "SP.Data.ParentListItem"
          },
          "Title": "D"
        }
      ]
    }

So total number of items added in that lookup field are Department.results.length

Using JSOM

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var listItem = list.getItemById(1);   
context.load(listItem);
context.executeQueryAsync(
   function() {
       var lookupVals = listItem.get_item(fieldName); //get multi lookup value (SP.FieldLookupValue[])
       var totalLookupVals = lookupVals.length;
   },
   function(sender,args){
       console.log(args.get_message());
   }
);

Let me know for more explanation or modification.

11
  • Now if I have to insert this value of totalLookupVals to the same item in a new column then can we do it using rest
    – KumarV
    Commented Nov 23, 2015 at 9:08
  • will it work for data: JSON.stringify({ '__metadata': { 'type': 'SP.Data.HpListListItem' }, 'gb3c': totalLookupVals}),
    – KumarV
    Commented Nov 23, 2015 at 9:11
  • I could not get... Commented Nov 23, 2015 at 9:13
  • I want to enter the value totalLookupVals in another column in same list
    – KumarV
    Commented Nov 23, 2015 at 9:15
  • Then it should work? make sure SP.Data.HpListListItem is the valid ListItemEntityTypeFullName and gb3c is the valid internal name Commented Nov 23, 2015 at 9:19
2

I used JSLink in the listview to retrieve this field, which comes back in the context object as an array. I then use the length property to render the count to the field I want.

Note that this is only good for view purposes as JS link does not actually update the value of the field itself, unless you explicitly write code to do so.

Steps are:

  1. Create a js script file eg: "lookupcount.js" somewhere, like your style library.
  2. In your listview webpart properties, under miscellaneous, you'll find a place to reference your JSLink. enter image description here
  3. In your javascript file, use the following code. NOTE: FieldToUpdate and LookUpField should be changed to your field names.

    (function () { 
    
    var priorityFiledContext = {}; 
    priorityFiledContext.Templates = {}; 
    priorityFiledContext.Templates.Fields = { 
        // Apply the new rendering for Priority field on List View 
        "FieldToUpdate": { "View": priorityFiledTemplate } 
    }; 
    
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(priorityFiledContext); 
    
    })(); 
    

    function priorityFiledTemplate(ctx) {

    var lookupfield = ctx.CurrentItem.LookUpField; var number = lookupfield.length; ctx.CurrentItem.FieldToUpdate; var returnRowStyle = "" + ctx.CurrentItem.FieldToUpdate + ""; return returnRowStyle; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.