Text field in SPFx, how to pass textbox entry in SPFx framework as a parameter - SharePoint Online

Text Field in SPFx: How to Pass Textbox Entry in SPFx Framework As a Parameter?

No comments

Loading

Text field in SPFx โ€“ in this article, we will learn about how to pass textbox entries in the SPFx framework as a parameter. Like any other programming language, in the SPFx framework also we can design a user interface to enter the input from the user which can be passed on to the SPFx framework coding. In this demo, we will see how to pass two textbox entries like โ€œList Nameโ€, and โ€œList Descriptionโ€ from the SPFx web part user interface that can be passed on to the coding.

Key-Highlights: Text field in SPFx

  • Demo โ€“ Display web part in SharePoint online page
  • Explanation of coding โ€“ web part rendering
  • Button click event method (function) to create the list
  • Registration of createNewCustomListToSPO() method in AddEventListeners() collection method
  • Registration of AddEventListeners() method inside the render() method
  • How to add an event listener for a button click in the SPFx Web part?
  • Where to add an event listener when using SPFx?

Demo (Text field in SPFx) โ€“ Display web part in SharePoint online page

Display the SPFx web part on the SharePoint Online workbench page โ€“ globalsharepoint2020.sharepoint.com/_layouts/15/workbench.aspx.

Users will enter โ€œList Nameโ€ and โ€œList Descriptionโ€ from the below two text boxes which will be passed on to the SPFx coding thru the โ€œCreate Listโ€ button click event.

Text field in SPFx, Pass textbox entry in SPFx framework as a parameter - demo
Pass textbox entry in SPFx framework as a parameter โ€“ demo

Explanation of coding โ€“ web part rendering (Text field in SPFx)

Now, I will explain how does the โ€œList Nameโ€ and โ€œList Descriptionโ€ pass on to SPFx coding to create the list in SharePoint Online?

The below-highlighted HTML coding is responsible for list name and list description rendering.

HTML Input in SPFx framework coding, Text field in SPFx
HTML Input in SPFx framework coding
<divย data-role="main"ย class="ui-content">ย ย ย ย 

 ย ย ย ย ย ย ย ย ย ย ย ย <div>ย ย ย 
 ย ย ย ย ย ย ย ย ย ย ย  
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Listย Name:ย <inputย id="listTitle"ย ย placeholder="Listย Name"/>ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Listย Description:ย <inputย id="listDescription"ย ย placeholder="Listย Description"/>

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <buttonย id="createNewCustomListToSPO"ย ย type="submit"ย >Createย List</button>ย ย ย ย 
 ย ย ย ย ย ย ย ย ย ย ย ย ย  
ย ย ย ย ย ย ย ย ย ย ย ย ย </div>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย </div>ย 

<br>ย ย 

<divย id="ListCreationStatusInSPOnlineUsingSPFx"ย />

ย 

Button click event method (function) to create the list (Text field in SPFx)

privateย CreateListInSPOUsinPnPSPFx():ย voidย 

ย ย ย ย {ย ย 

ย ย ย ย ย ย ย ย letย myWebย =ย newย Web(this.context.pageContext.web.absoluteUrl);ย ย 

ย ย ย ย ย ย ย ย //letย mySPFxListTitleย =ย "CustomList_using_SPFx_Framework";ย 

ย ย ย ย ย ย ย ย letย mySPFxListTitleย =ย document.getElementById('listTitle')["value"];ย ย 

ย ย ย ย ย ย ย ย //letย mySPFxListDescriptionย =ย "Customย listย createdย usingย theย SPFxย Framework";ย 

ย ย ย ย ย ย ย ย letย mySPFxListDescriptionย =ย document.getElementById('listDescription')["value"];ย ย ย 

ย ย ย ย ย ย ย ย letย listTemplateIDย =ย 100;ย ย 

ย ย ย ย ย ย ย ย letย boolEnableCTย =ย false;ย ย 

ย ย ย ย ย ย ย ย myWeb.lists.add(mySPFxListTitle,ย mySPFxListDescription,ย listTemplateID,ย boolEnableCT).then(function(splist)

ย ย ย ย ย ย ย ย {ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย document.getElementById("ListCreationStatusInSPOnlineUsingSPFx").innerHTMLย +=ย `Theย SPOย newย listย `ย +ย mySPFxListTitleย +ย `ย hasย beenย createdย successfullyย usingย SPFxย Framework.`;ย ย 

ย ย ย ย ย ย ย ย });ย ย 

ย ย ย ย }
In the above coding, using the document.getElementById(โ€˜listTitleโ€™)[โ€œvalueโ€] coding, passing the โ€œList Titleโ€ and using the document.getElementById(โ€˜listDescriptionโ€™)[โ€œvalueโ€] passing the โ€œList Descriptionโ€ in the SPFx type script coding.
Note:
  • If we observe the above coding, we can see it is exactly the traditional javascripting DOM object model coding which reads the input control thru its ID (document.getElementById(โ€˜listTitleโ€™)[โ€œvalueโ€] and document.getElementById(โ€˜listDescriptionโ€™)[โ€œvalueโ€].

ย 

By now we have created the method to handle the list creation logic based on the user input (list title and list description), now we need to register this list creation method to handle the button click (Create List) event in the event listeners collection.

Registration of createNewCustomListToSPO() method in AddEventListeners() collection method (Text field in SPFx)

privateย AddEventListeners()ย :ย void

ย ย ย ย {ย ย ย ย 
 ย  
ย ย ย ย ย document.getElementById('createNewCustomListToSPO').addEventListener('click',()=>this.CreateListInSPOUsinPnPSPFx());ย ย ย ย 

ย ย ย ย }

In the above coding, โ€˜createNewCustomListToSPOโ€˜ is the ID of the โ€œCreate Listโ€ button in the web part, the rendering part is defined here:

ย  ย  ย  ย  ย  <div >ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Listย Name:ย <inputย id="listTitle"ย ย placeholder="Listย Name"/>ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Listย Description:ย <inputย id="listDescription"ย ย placeholder="Listย Description"/>

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <buttonย id="createNewCustomListToSPO"ย ย type="submit"ย >Createย List</button>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย </div>

ย 

Registration of AddEventListeners() method inside the render() method (Text field in SPFx)

We have now, a list creation custom method (function), and the event listeners collection is ready โ€“ we need to call this event listeners collection method โ€“ AddEventListeners() inside the render method like below:

Call event listener method inside the SPFx render method, Text field in SPFx
Call event listener method inside the SPFx render method
publicย render():ย void

ย ย ย ย {ย ย 

ย ย ย ย ย ย ย ย this.domElement.innerHTMLย =ย `

<divย class="${styles.createListUsingSpfx}">ย ย 

<divย class="${styles.container}">ย ย 

<divย class="ms-Grid-rowย ms-bgColor-themeDarkย ms-fontColor-whiteย ${styles.row}">ย ย 

<divย class="ms-Grid-colย ms-u-lg10ย ms-u-xl8ย ms-u-xlPush2ย ms-u-lgPush1">ย ย 

<spanย class="ms-font-xlย ms-fontColor-white"ย style="font-size:28px">Welcomeย toย SPFxย learningย (createย listย usingย PnPย JSย library)</span>ย ย 

<pย class="ms-font-lย ms-fontColor-white"ย style="text-align:ย left">Demoย :ย Createย SharePointย Listย inย SPOย usingย SPFx</p>ย ย 

</div>ย ย 

</div>ย ย 

<divย class="ms-Grid-rowย ms-bgColor-themeDarkย ms-fontColor-whiteย ${styles.row}">ย ย 

<divย data-role="main"ย class="ui-content">ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย <divย >ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Listย Name:ย <inputย id="listTitle"ย ย placeholder="Listย Name"/>ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Listย Description:ย <inputย id="listDescription"ย ย placeholder="Listย Description"/>

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย <buttonย id="createNewCustomListToSPO"ย ย type="submit"ย >Createย List</button>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย ย ย </div>ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ย </div>

<br>ย ย 

<divย id="ListCreationStatusInSPOnlineUsingSPFx"ย />ย ย 

</div>ย ย 

</div>ย ย 

</div>`;ย ย 

ย ย ย ย ย ย ย ย this.AddEventListeners();ย ย 

ย ย ย ย }

Summary: Text field in SPFx (how to pass textbox entry in SPFx framework as a parameter)

Thus, in this article, we have learned about how to pass the textbox input parameter to SPFx web part coding, the learning can be summarized as below:

  • We have seen the demo of how to display the web part on the SharePoint online page.
  • Explanation of coding โ€“ how does the web part get rendered?
  • How to handle the button click event method (function) to create the list
  • How to register a new method inside AddEventListeners() collection method
  • How to call or register the AddEventListeners() method inside the render() method
  • How to add an event listener for a button click in the SPFx Web part?
  • Where to add an event listener when using SPFx?

See Also: SPFx SharePoint Tutorial

You may also like the below SharePoint SPFx articles:

ย 

About Post Author


Discover more from Global SharePoint

Subscribe to get the latest posts sent to your email.

Do you have a better solution or question on this topic? Please leave a comment