how to retrieve look up column value using spfx

kem104 160 Reputation points
2024-07-16T20:46:21.36+00:00

Hi, I am creating custom webpart using SPFX which will display the Items values in table format. when I am trying to get string column value it is working fine, but the look up column value give 'undefined'. What code I have to add in table so it will display the actual value from look up column.

My main List is called Authoritative Document and the field 'Issuing Authority' and 'Enforcement Status' are the look up columns.

What code I should replace with <td>${item.issuing_Authority}</td> to get actual value from primary list.

Here is my code for your reference.

User's image

User's image

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,046 questions
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 26,771 Reputation points Microsoft Vendor
    2024-07-17T08:58:28.2966667+00:00

    Hi @kem104 ,In this case, the value is usually not obtained correctly. You can refer to this post and try again:

    https://sharepoint.stackexchange.com/questions/297453/sharepoint-spfx-dealing-with-lookup-field


    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2024-07-16T20:46:21.3633333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To retrieve the actual value from a lookup column in SPFX, you need to use the ID of the item in the lookup column to get the corresponding item from the lookup list and then retrieve the value of the desired field from that item. Here's an example of how you can modify your code to get the actual value from the lookup column:

    private _renderList(items: ISPListItem[]): void {
      let html: string = '<table class="TFtable" border=1 width=100% style="border-collapse: collapse;">';
      html += '<th>Title</th><th>Issuing Authority</th><th>Enforcement Status</th>';
      items.forEach((item: ISPListItem) => {
        html += `
          <tr>
            <td>${item.Title}</td>
            <td>${item.Issuing_x0020_AuthorityId ? this._getLookupValue(item.Issuing_x0020_AuthorityId, "Authoritative_x0020_Documents", "Title") : ""}</td>
            <td>${item.Enforcement_x0020_StatusId ? this._getLookupValue(item.Enforcement_x0020_StatusId, "Authoritative_x0020_Documents", "Title") : ""}</td>
          </tr>
        `;
      });
      html += '</table>';
      const listContainer: Element = this.domElement.querySelector('#spListContainer');
      listContainer.innerHTML = html;
    }
    
    private _getLookupValue(lookupId: number, listName: string, fieldName: string): string {
      let lookupValue: string = "";
      const endpoint: string = `${this.context.pageContext.web.absoluteUrl}/_api/web/lists/getbytitle('${listName}')/items(${lookupId})?$select=${fieldName}`;
      return this.context.spHttpClient.get(endpoint, SPHttpClient.configurations.v1)
        .then(response => {
          if (response.ok) {
            return response.json();
          } else {
            console.log(`Error getting item with ID ${lookupId} from list ${listName}: ${response.statusText}`);
          }
        })
        .then(data => {
          if (data) {
            lookupValue = data[fieldName];
          }
          return lookupValue;
        })
        .catch(error => {
          console.log(`Error getting item with ID ${lookupId} from list ${listName}: ${error}`);
          return lookupValue;
        });
    }
    

    In the _renderList method, we check if the lookup column has a value (item.Issuing_x0020_AuthorityId and item.Enforcement_x0020_StatusId) and if it does, we call the _getLookupValue method to retrieve the actual value from the lookup list. The _getLookupValue method constructs the REST API endpoint to get the item with the specified ID from the lookup list and then retrieves the value of the specified field (fieldName) from the returned item.

    References:

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.