Join the Team Forms community

Updated 6 months ago

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

At a glance
The community member is working on a POC using Microsoft Forms and is trying to get the first value of a filtered SharePoint list as the default value in a text field. A community member provided a solution using JavaScript to retrieve the first item from the filtered list and set it as the value of the text field. The community member confirmed that the solution worked perfectly. <answer>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: Get the id for on your data source Drag-n-drop a text field into your form 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. 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()</answer>

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 :)