Problem Statement

Create a SharePoint dropdown list to request asset,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Output will look like this,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

Solution

We need to create four SharePoint lists as shown the table below the problem statement.

List 1: RequestType

The list contains type of requirement (Hardware/Software)
  1. Create SharePoint List named “RequestType”

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Add the following data in title column,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

List 2: RequestAsset

The list contains sub type of the requirement,
Hardware - Input, Output
Software - Programming, Configuration
sr no
Column Name
Column Type
Description
1
RequestType
Lookup
Get inf from (RequestType list) and Column is Title

Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

List 3: AssetNames

The list contains sub types of the requirement
Input - Keyboard, Mouse, Joystick
Output - Monitor, Printer
Programming - Visual Studio, Matlab, Netbeans
Configuration - Infopath, Sharepoint Designer
  1. Create SharePoint List named “AssetNames”

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  2. Go to setting and create column Named “RequestAsset” and column type is “Lookup” as shown below.

    sr no Column Name Column Type Description
    1 RequestAsset Lookup Get inf from (RequestAsset list) and Column is Title


    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  3. All columns will look like this

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  4. Add the following data in this list

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
All data is set for cascading. Now to access this data, create a fourth list

List 4: New Requirement

This is the list where we can achieve our output
  1. Create SharePoint List named “New Requirement”

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  2. Add three lookup columns as shown below

    sr no Column Name Column Type Description
    1 RequestType Lookup Get inf from (RequestType list) and Column is Title
    2 RequestAsset Lookup Get inf from (RequestAsset list) and Column is Title
    1 AssetNames Lookup Get inf from (AssetNames list) and Column is Title

    Refer to the below three screenshots for three columns,
    1.RequestType column

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
    2.RequestAsset Column

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
    3.RequestNames Column

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  3. All columns will look like this:

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  4. Your list will look like the following. To write code for cascading, click on New item as shown below

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  5. Following new form will open

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  6. Edit the form by using Gear icon on Top Right side

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  7. Click on Insert button on Top Left side then select Media and Content, then click on Script Editor and click Add button on right

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  8. Click on edit snippet

    Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part

  9. Paste below code and click insert and stop editing the page
The following piece of code is for first level cascading dropdown,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Output
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
If you select Hardware in first column, Request Asset will filter with Input and Output Only,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
If you select Software in first column, Request Asset will filter with configuration tools and programming only,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
The following piece of code is for second level cascading dropdown,
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Output
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
If you select Request Type as Hardware and Request asset as Input then the third column will filter with “Keyboard”, ”Mouse”, Joystick” dropdown options.
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
If you select Request Type as Hardware and request asset as Output then the third column will filter with “Keyboard”, ”Mouse”, Joystick” dropdown options.
Three Level Cascading Dropdown SharePoint List By Using JavaScript And Script Editor Web Part
Common method for above both piece of code is $().methoddropdown(Dropdownlist); and the whole code is as follows,
  1. <script src="//code.jquery.com/jquery-3.4.1.min.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function() {
  4. var Dropdownlist = new Array();
  5. Dropdownlist.push({
  6. rootlistname: "RequestType", //1st (root) list Display name
  7. leaflistname: "RequestAsset", //2nd (leaf) List name
  8. leaflookupcolumn: "Title", //Internal field name in leaf List used in lookup
  9. mainformcolumnleafref: "RequestAsset", //main form (4th list)2nd column Display name
  10. mainformcolumnrootref: "RequestType", // main form (4th list) 1st column Display name
  11. defaultddwn: "< Select a Asset Type >"
  12. });
  13. Dropdownlist.push({
  14. rootlistname: "RequestAsset", //2nd (root) list Display name
  15. leaflistname: "AssetNames", //3rd (leaf) List name.
  16. leaflookupcolumn: "Title", //Internal field name in leaf List used in lookup
  17. mainformcolumnleafref: "AssetNames", //main form (4 th list) 3rd column Display name
  18. mainformcolumnrootref: "RequestAsset", // main form (4 th list) 2nd column Display name
  19. defaultddwn: "< Select a Asset Name >"
  20. });
  21. $().methoddropdown(Dropdownlist);
  22. });
  23. $.fn.methoddropdown= function (dropdownvalueArray)
  24. {
  25. var ddwndropdownvalue = new Array();
  26. var mainform = getParameterByName("ID") == null;
  27. $.fn.methoddropdown.Cascade = function(root,ddwnindex)
  28. {
  29. if (ddwnindex!= null && ddwnindex+1 > ddwndropdownvalue.length)
  30. {
  31. return;
  32. } else if(ddwnindex== null) {
  33. ddwnindex= $(root).attr("methoddropdownIndex");
  34. }
  35. var params = ddwndropdownvalue[ddwnindex];
  36. var rootID = $(root).val();
  37. if (root == null)
  38. {
  39. rootID = $("select[Title='"+params.rootlistname+"'], select[Title='"+
  40. params.rootlistname+" Required Field']").val();
  41. }
  42. if (rootID == undefined)
  43. {
  44. rootID = 0;
  45. }
  46. var leaf = $("select[Title='"+params.mainformcolumnleafref+"'], select[Title='"+
  47. params.mainformcolumnleafref+" Required Field']," +
  48. "select[Title='"+params.mainformcolumnleafref+" possible values']");
  49. var selectedoption = params.selectedoptionue;
  50. ddwndropdownvalue[ddwnindex].selectedoptionue = 0;
  51. $(leaf).empty();
  52. var dropdownvalue = "<option value='0'>"+params.defaultddwn+"</option>";
  53. var varstore = $.ajax({
  54. url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('"+params.leaflistname+
  55. "')/items?$select=Id,"+params.leaflookupcolumn+","+params.mainformcolumnrootref+
  56. "/Id&$expand="+params.mainformcolumnrootref+"/Id&$filter="+params.mainformcolumnrootref+
  57. "/Id eq "+ rootID+"&$orderby=" + params.leaflookupcolumn,
  58. type: "GET",
  59. dataType: "json",
  60. headers: {
  61. Accept: "application/json;odata=verbose"
  62. }
  63. });
  64. varstore.done(function (data,textStatus, jqXHR){
  65. for (index in data.d.results)
  66. {
  67. dropdownvalue += "<option value='"+ data.d.results[index].Id +"'>"+
  68. data.d.results[index][params.leaflookupcolumn]+"</option>";
  69. }
  70. $(leaf).append(dropdownvalue);
  71. if(!mainform)$(leaf).val(selectedoption);
  72. $().methoddropdown.Cascade(null,Number(ddwnindex)+1);
  73. });
  74. varstore.fail(function (jqXHR,textStatus,errorThrown){
  75. alert("Error retrieving information from list: " + params.leaflistname + jqXHR.responseText);
  76. $(leaf).append(dropdownvalue);
  77. });
  78. }
  79. for (index in dropdownvalueArray)
  80. {
  81. var thisCascade = dropdownvalueArray[index];
  82. if(thisCascade.rootlistname != null)
  83. {
  84. var root = $("select[Title='"+thisCascade.rootlistname+"'], select[Title='"+
  85. thisCascade.rootlistname+" Required Field']");
  86. $(root).attr("methoddropdownIndex",index);
  87. $(root).change(function(){
  88. $().methoddropdown.Cascade(this,null);
  89. });
  90. }
  91. thisCascade.selectedoptionue = $("select[Title='"+thisCascade.mainformcolumnleafref+"'], select[Title='"+
  92. thisCascade.mainformcolumnleafref+" Required Field']," +
  93. "select[Title='"+thisCascade.mainformcolumnleafref+" possible values']").val();
  94. ddwndropdownvalue.push(thisCascade);
  95. }
  96. $().methoddropdown.Cascade(null,0);
  97. function getParameterByName(key) {
  98. key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
  99. var match = location.search.match(new RegExp("[?&]"+key+"=([^&]+)(&|$)"));
  100. return match && decodeURIComponent(match[1].replace(/\+/g, " "));
  101. }}
  102. </script>