Join the Team Forms community

Select as a defaut value the first item of a "sharepoint data component"

Updated 4 months ago

Hi there,

I’m working on a POC using team Forms.

I try to get a single value of a sharepoint list by addind a filter options on a « sharepoint Data Component » :


 

My items list is filtered and I’m quite happy. But in fact, I want to select the first value of the list as a default value…

 

I try to modify the « calculated value »  as below :

 

So my question : How can I calculate in a  TEXT FIELD or select the first item of a filtered sharepoint data component ?

 

Thank you for support !

Marked as solution

Hi Hugues,


I'll just preface this by saying that this can get quite complicated because the values in the drop-down menu are not calculated until the user opens the drop-down menu. Therefore, you will actually need to perform the same logic/filtering in your text field that would normally be handled by the SharePoint Data component.


Your can automatically set the value of a text field to what would be the first item of your SharePoint data (drop-down) component using the steps below:

  1. Get the id for on your data source

  2. Drag-n-drop a text field into your form

  3. In the calculated value of the text field add the following JavaScript. Remember you will need to modify this for your specific forms and filters.

JavaScript
async function getFirstItem(){
    const allRows = await tf.getDataSource('insertDataSourceId')
    const filteredRows = allRows.filter(row => (row?.Title === 'Mr')) // Apply your own filter here
    return filteredRows?.[0]?.ColumnName
}

value = getFirstItem()


View full solution
E
H
1 comment·2 replies

Hi Hugues,


I'll just preface this by saying that this can get quite complicated because the values in the drop-down menu are not calculated until the user opens the drop-down menu. Therefore, you will actually need to perform the same logic/filtering in your text field that would normally be handled by the SharePoint Data component.


Your can automatically set the value of a text field to what would be the first item of your SharePoint data (drop-down) component using the steps below:

  1. Get the id for on your data source

  2. Drag-n-drop a text field into your form

  3. In the calculated value of the text field add the following JavaScript. Remember you will need to modify this for your specific forms and filters.

JavaScript
async function getFirstItem(){
    const allRows = await tf.getDataSource('insertDataSourceId')
    const filteredRows = allRows.filter(row => (row?.Title === 'Mr')) // Apply your own filter here
    return filteredRows?.[0]?.ColumnName
}

value = getFirstItem()


Thank you ! It works perfectly !

I'm glad to hear it worked :)