1

I'm trying to create a React SPFx web part which can render items from a SPO list.

I am having difficulty if the column is a Hyperlink. (I can get the value if it is a string).

Does anyone know how to render the value if it's a hyperlink?

public render(): React.ReactElement<ISplistitemsdataProps> {  

  const onRenderItemColumn = (item: ISplistitemsState, index: number, column: IColumn) => {

  const fieldContent = item[column.fieldName as keyof ISplistitemsState] as any;
  let typeOfThisObject: string = (typeof fieldContent).toString();
  let valueToReturn: string = "";

  if(typeOfThisObject == "object") {

    try {
      valueToReturn = fieldContent.toString();
      valueToReturn = 

      const values = Object.keys(fieldContent).map(key => fieldContent[key]);
      //valueToReturn = "<a href='" + values[1] + "'>" + values[0] + "</a>";
      //valueToReturn = "object:" + values[1] + "-" + values[0];

      const values2 = Object.keys(values[0]).map(key => values[0][key]);
      //valueToReturn += "object2:" + values2[1] + "-" + values2[0];

} catch (e) {
          valueToReturn = e.message.toString();
        }

      } else {
        valueToReturn = "not object:" + fieldContent;
      }
6
  • What is the value of item at first line in render method? Can you add value/object to your original question? Commented Oct 4, 2021 at 5:20
  • @GaneshSanap Hi Ganesh ... Its //export interface ISplistitemsState{ export interface IListItems{ items:[ { //"HL":string, "HL":{ Description:string; URL:string; } "Title":string, "Id":string, "Created":string; "Author":{ Title:string; } }] } Commented Oct 4, 2021 at 9:35
  • If I do this it should get me the Desription and URL? const values = Object.keys(fieldContent).map(key => fieldContent[key]); valueToReturn = "values ..." + values[1] + "-" + values[0]; Commented Oct 4, 2021 at 9:37
  • But that gives me values:two-[object Object] (it can find the Description but not the URL for some reason) Commented Oct 4, 2021 at 9:43
  • I am not sure about logic in your code. Can you simply try this first as first line in render() method? let HLDescription = this.state.items[0].HL.Description; console.log(HLDescription); Commented Oct 4, 2021 at 9:45

1 Answer 1

1

Hyperlink columns have data object/value in below format:

{
    "Description": "Google Home",
    "Url": "www.google.com"
}

So, considering name of your column is HyperlinkCol, you can get description using:

item.HyperlinkCol.Description;

& URL using:

item.HyperlinkCol.Url;
2
  • 1
    That was it... Many thanks Commented Oct 4, 2021 at 19:29
  • Great, glad it worked for you. Please Upvote(^) and accept as an Answer as it helped you & it will help others with similar question in future to find the correct answer easily. Commented Oct 5, 2021 at 5:22

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.