Problem Statement
Create a SharePoint dropdown list to Request Asset.
Output will look like this,
Solution
We need to create four SharePoint lists as shown the table below Problem statement.
List1. RequestType
The list contains Type of requirement (Hardware/Software)
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
List 3. AssetNames
The list contains sub type of the requirement,
Input - Keyboard, Mouse, Joystick
Output - Monitor, Printer
Programming - Visual Studio, Matlab, Netbeans
Configuration - Infopath, Sharepoint Designer
sr no
Column Name
Column Type
Description
1
RequestAsset
Lookup
Get inf from (RequestAsset list) and Column is Title
List 4. New Requirement
This is the list where we can achieve our output,
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 below 3 screenshots for three columns,
RequestType column
RequestAsset Column
RequestNames Column
Output
If you select Hardware in first column, Request Asset will filter with Input and Output Only
If you select Software in first column, Request Asset will filter with Configuration tools and Programming Only.
Following piece of code is for Second level cascading dropdown,
Output
If you select Request Type as Hardware and Request asset as Input then
Third column will filter with “Keyboard”, ”Mouse”, Joystick” dropdown options.
If you select Request Type as Hardware and Request asset as Output then
Third column will filter with “Keyboard”, ”Mouse”, Joystick” dropdown options.
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>