SPFX Get Lookup Field Value

%3CLINGO-SUB%20id%3D%22lingo-sub-2281056%22%20slang%3D%22en-US%22%3ESPFX%20Get%20Lookup%20Field%20Value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2281056%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20new%20to%20SPFX%20and%20I%20am%20having%20some%20problems%20getting%20the%20values%20from%20a%20lookup%20field%20on%20a%20list.%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20code%20I%20am%20using%20for%20testing%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%2F%2FHere%20%22My%20List%22%20is%20Sharepoint%20list%0A%20%20%20%20pnp.sp.web.lists.getByTitle(%22My_List%22).items.select(%22Lookup_x0020_Field%22%2C%20%22Lookup_x0020_FieldId%22%2C%20%22Lookup_x0020_Field%2FTitle%22).expand(%22Lookup_x0020_Field%22).getPaged().then(p%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20console.log(JSON.stringify(p.results%2C%20null%2C%204))%3B%0A%20%20%20%20%20%20%20%20var%20itemColl%20%3D%20p.results%3B%0A%20%20%20%20%20%20%20%20for%20(var%20index%20%3D%200%3B%20index%20%26lt%3B%20itemColl.length%3B%20index%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20element%20%3D%20itemColl%5Bindex%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20title%20%3D%20element%5B%22Title%22%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20id%20%3D%20element%5B%22Id%22%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20lookUpFld%20%3D%20element%5B%22Lookup_x0020_Field%22%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20lookUpFldId%20%3D%20lookUpFld%5B%22Lookup_x0020_FieldId%22%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(%22Lookup%20Field%20%22%20%2B%20lookUpFld%20%2B%20%22%20Lookup%20Field%20ID%3A%20%22%20%2B%20lookUpFldId)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D)%3B%0A%20%20%20%20return%20Promise.resolve()%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPart%20of%20the%20Log%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%20%7B%0A%20%20%20%20%20%20%20%20%22odata.type%22%3A%20%22SP.Data.MyListItem%22%2C%0A%20%20%20%20%20%20%20%20%22odata.id%22%3A%20%22GUID%22%2C%0A%20%20%20%20%20%20%20%20%22odata.etag%22%3A%20%22%5C%222%5C%22%22%2C%0A%20%20%20%20%20%20%20%20%22odata.editLink%22%3A%20%22Web%2FLists(guid'GUID')%2FItems(37)%22%2C%0A%20%20%20%20%20%20%20%20%22Lookup_x0020_Field%40odata.navigationLinkUrl%22%3A%20%22Web%2FLists(guid'GUID')%2FItems(37)%2FLookup_x0020_Field%22%2C%0A%20%20%20%20%20%20%20%20%22Lookup_x0020_Field%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22odata.type%22%3A%20%22SP.Data.Lookup_x005f_List_x0020_DataListItem%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22odata.id%22%3A%20%22ID%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Title%22%3A%20null%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22Lookup_x0020_FieldId%22%3A%20116%0A%20%20%20%20%7D%2C%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELookup_x0020_Field%20is%20the%20name%20of%20the%20field%20on%20the%20list%20I%20am%20trying%20to%20display%20the%20data.%3C%2FP%3E%3CP%3E%3CSPAN%3ELookup_x0020_Field%3C%2FSPAN%3EId%20is%20the%20ID%20of%26nbsp%3B%3CSPAN%3ELookup_x0020_Field%3C%2FSPAN%3E.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20are%20more%20list%20that%20I%20need%20to%20display%20data%20from%20that%20have%20more%20than%20one%20lookup%20fields.%20Getting%20this%20one%20working%20will%20help%20with%20the%20other%20lists.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20need%20to%20know%20how%20to%20display%20images%20and%20URL's%20from%20lists.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIdeas%20or%20help%20will%20be%20very%20much%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2282459%22%20slang%3D%22en-US%22%3ERe%3A%20SPFX%20Get%20Lookup%20Field%20Value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2282459%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1033243%22%20target%3D%22_blank%22%3E%40lantonio1974%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20want%20to%20%3CSPAN%3Edisplay%20images%20and%20URL's%20from%20same%20list%20(%22My%20List%22)%20or%20the%20lookup%20list%20where%20the%20lookup%20fields%20are%20being%20stored%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EIf%20you%20are%20trying%20to%20show%20the%20fields%20from%20lookup%20list%20then%20it%20is%20not%20possible%20to%20expand%20the%20Image%2FURL%20columns%20as%20lookup%20columns%20only%20supports%20below%20column%20types%3A%3C%2FSPAN%3E%3C%2FP%3E%3CUL%3E%3CLI%3E%3CSPAN%3ESingle%20line%20of%20text%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%3ENumber%3C%2FSPAN%3E%3C%2FLI%3E%3CLI%3E%3CSPAN%3EDate%20and%20Time%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%3CSPAN%3ECheck%20below%20documentation%20for%26nbsp%3B%3CSTRONG%3ESupported%20%26amp%3B%20Unsupported%26nbsp%3BColumn%20Types%3C%2FSTRONG%3E%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%3CA%20title%3D%22Create%20list%20relationships%20by%20using%20unique%20and%20lookup%20columns%22%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Foffice%2Fcreate-list-relationships-by-using-unique-and-lookup-columns-80a3e0a6-8016-41fb-ad09-8bf16d490632%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20list%20relationships%20by%20using%20unique%20and%20lookup%20columns%3C%2FA%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CHR%20%2F%3E%3CP%3EPlease%20click%26nbsp%3B%3CSTRONG%3EMark%20as%20Best%20Response%3C%2FSTRONG%3E%26nbsp%3B%26amp%3B%20%3CSTRONG%3ELike%3C%2FSTRONG%3E%20if%20my%20post%20helped%20you%20to%20solve%20your%20issue.%20This%20will%20help%20others%20to%20find%20the%20correct%20solution%20easily.%20It%20also%20closes%20the%20item.%20If%20the%20post%20was%20useful%20in%20other%20ways%2C%20please%20consider%20giving%20it%20%3CSTRONG%3ELike%3C%2FSTRONG%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2281061%22%20slang%3D%22en-US%22%3ERe%3A%20SPFX%20Get%20Lookup%20Field%20Value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2281061%22%20slang%3D%22en-US%22%3EI%20was%20able%20to%20get%20the%20data.%20Looks%20like%20the%20problem%20was%20with%20the%20name%20of%20the%20column%20in%20the%20list%20where%20the%20lookup%20field%20are%20being%20store.%20Instead%20of%20the%20Title%20field%20it%20was%20using%20another%20field.%3CBR%20%2F%3EAny%20other%20suggestions%20are%20more%20than%20welcome.%3CBR%20%2F%3EThank%20you!%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi,

 

I am new to SPFX and I am having some problems getting the values from a lookup field on a list. 

Here is the code I am using for testing:

 

//Here "My List" is Sharepoint list
    pnp.sp.web.lists.getByTitle("My_List").items.select("Lookup_x0020_Field", "Lookup_x0020_FieldId", "Lookup_x0020_Field/Title").expand("Lookup_x0020_Field").getPaged().then(p => {
        console.log(JSON.stringify(p.results, null, 4));
        var itemColl = p.results;
        for (var index = 0; index < itemColl.length; index++) {
            var element = itemColl[index];
            var title = element["Title"];
            var id = element["Id"];
            var lookUpFld = element["Lookup_x0020_Field"];
            var lookUpFldId = lookUpFld["Lookup_x0020_FieldId"];
            console.log("Lookup Field " + lookUpFld + " Lookup Field ID: " + lookUpFldId);
        }
    });
    return Promise.resolve();

 

Part of the Log

 

 {
        "odata.type": "SP.Data.MyListItem",
        "odata.id": "GUID",
        "odata.etag": "\"2\"",
        "odata.editLink": "Web/Lists(guid'GUID')/Items(37)",
        "Lookup_x0020_Field@odata.navigationLinkUrl": "Web/Lists(guid'GUID')/Items(37)/Lookup_x0020_Field",
        "Lookup_x0020_Field": {
            "odata.type": "SP.Data.Lookup_x005f_List_x0020_DataListItem",
            "odata.id": "ID",
            "Title": null
        },
        "Lookup_x0020_FieldId": 116
    },

 

 

Lookup_x0020_Field is the name of the field on the list I am trying to display the data.

Lookup_x0020_FieldId is the ID of Lookup_x0020_Field

 

There are more list that I need to display data from that have more than one lookup fields. Getting this one working will help with the other lists. 

I also need to know how to display images and URL's from lists. 

 

Ideas or help will be very much appreciated.

 

Thank you!

2 Replies
I was able to get the data. Looks like the problem was with the name of the column in the list where the lookup field are being store. Instead of the Title field it was using another field.
Any other suggestions are more than welcome.
Thank you!

@lantonio1974 

Do you want to display images and URL's from same list ("My List") or the lookup list where the lookup fields are being stored?

 

If you are trying to show the fields from lookup list then it is not possible to expand the Image/URL columns as lookup columns only supports below column types:

  • Single line of text
  • Number
  • Date and Time

Check below documentation for Supported & Unsupported Column Types:

Create list relationships by using unique and lookup columns 


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.