Retrieve Lookup Items by Search

Good morning,

I have the code below which does a lookup by a CostCentre and returns all the items related to it and populates this into the lookup fields

//This code will auto-populate all the Org Hierarchy fields v2   
window.searchHierarchyV2 = function populateFields(value) {
    //get CostCenter
    var costCentreV2 = fd.field('CostCentre').value;

    //make sure both values are selected
    if(costCentreV2) {
        var filter = encodeURIComponent("Cost_x0020_Centre eq '" + costCentreV2 + "'");
        
        //filter list and get item values
        pnp.sp.web.lists.getByTitle('Organisational Hierarchy').items.filter(filter).get().then(function(item){
//Populates items into lookup fields
            fd.field('DepartmentV2').value = item[0].Id;
            fd.field('SpecialtyV2').value = item[0].Id;
            fd.field('DirectorateV2').value = item[0].Id;
            fd.field('DivisionV2').value = item[0].Id;
            fd.field('HospitalV2').value = item[0].Id;
    
        });
    }
}

Instead of it being populated into lookup fields, would be it be possible to populate the items into Single Line Text fields? I tried doing this but it populated the fields with the item ID number.

Thank you!

Dear @DryChips,
Sure, you can try to retrieve Title or any other field, for example:

         pnp.sp.web.lists.getByTitle('Organisational Hierarchy').items.select('Title').filter(filter).get().then(function(item){
            //Populates fields
            fd.field('DepartmentV2').value = item[0].Title;
            fd.field('SpecialtyV2').value = item[0].Title;
            fd.field('DirectorateV2').value = item[0].Title;
            fd.field('DivisionV2').value = item[0].Title;
            fd.field('HospitalV2').value = item[0].Title;
        });

Hey Nikita,

Thank you for this!

I tried using this but it's only returned the header name.

Where did you get this code from? Can I see the documentation so I can see if I can retrieve the items related to the cost centre code.

Dear @DryChips,
Based on the official pnpjs documentation - List Items - PnP/PnPjs

Hey, thank you for that.

I figured it out!

     pnp.sp.web.lists.getByTitle('Organisational Hierarchy').items.select().filter(filter).get().then(function(item){
        //Populates fields
        fd.field('DepartmentV2').value = item[0].Department;
        fd.field('SpecialtyV2').value = item[0].**#** (Add Field Header Name HERE);
        fd.field('DirectorateV2').value = item[0].**#**;
        fd.field('DivisionV2').value = item[0].**#**;
        fd.field('HospitalV2').value = item[0].**#**;
    });

Oh man, another annoying problem.

The values are being retrieved from the Source list using the code but I can't see the data in the SharePoint List!!!

Dear @DryChips,
Fields are looking strange, is that some CSS customization? Also, are you saving the form with the values? When is the code running?

Strange? In what sense?

This is the code that is powering that search functionality:

//This code will auto-populate all the Org Hierarchy fields v2   
window.searchHierarchyV2 = function populateFields(value) {
    //get CostCenter
    var costCentreV2 = fd.field('CostCentre').value;

    //make sure both values are selected
    if(costCentreV2) {
        var filter = encodeURIComponent("Cost_x0020_Centre eq '" + costCentreV2 + "'");
        
        //filter list and get item values
        pnp.sp.web.lists.getByTitle('Organisational Hierarchy').items.select().filter(filter).get().then(function(item){
            fd.field('DepartmentV2').value =  item[0].Id;
            fd.field('SpecialtyV2').value = item[0].Org_x0020_L6;
            fd.field('DirectorateV2').value= item[0].Org_x0020_L5;
            fd.field('DivisionV2').value = item[0].Org_x0020_L4;
            fd.field('HospitalV2').value = item[0].Title;
    
        });
    }
}

Dear @DryChips,
This is strange. Refreshing the page doesn't help? Please, check the browser's console for errors.

The fields look okay BTW, didn't realize it was a standard form on the screenshot above.

Hey Nikita,

Nope, no errors in console and refreshing the list doesn't do anything.

I created a new form and it works! Don't worry.

Thank you for your help as always! :slight_smile:

1 Like