Cascading dropdowns in SharePoint text fields – populated by lookup in another list (version 2)


03.11.2010 Updated the code to eliminate the need to refer other external resources than jQuery. Removed the argument “lookupSourceListURL” from the function call.

The code is tested in IE, Chrome and Firefox. Opera is not supported.

NOTE: When updating existing script you must modify the function call and remove the argument “lookupSourceListURL”.


I have previously posted a solution for creating cascading dropdowns from SharePoint single line text fields. Populated based on a query against another list.

I have reviewed the script to add a few enhancements.

  • No more need to create a calculated column in the “lookup list”
  • Less arguments to pass to the function – sleeker code
  • Overcomes a possible bug regarding the previous version using “BeginsWith” rather than “Eq” to match the items

This release (as the previous) has these features:

  • “Converts” standard SharePoint single line text fields to dropdowns
  • Uses the converted SharePoint single line text fields to hold the values
  • Populates the dropdowns by querying any SharePoint list or library
  • Dynamically fills or clears the hidden fields holding the selected value to adapt to changes in the selections – thus preventing “impossible combinations”
  • Preserves selections during page refresh due to form validation
  • Reads back and fills the dropdowns if used in EditForm

As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”). Upload jQuery and the file “spjs_CascadingDropDowns_v2.js” to that library.

The jQuery-library is found here. The sourcecode refers to jquery-1.4.2.min. Some of the functions are not supported in previous releases.

The sourcecode for the file “spjs_CascadingDropDowns_v2.js” can be found below.

Read here how to add a CEWP to the NewForm or EditForm, how to find the list Guid of your list, and how to find the FieldInternalName of your columns.

You can pull any values from another list – an example provided below.

Source list:
IMG
The field “Make” is the native “Title” field.

Target list:
IMG

The CEWP code – place below the Form:

<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/spjs_CascadingDropDowns_v2.js"></script>
<script type="text/javascript">
   // Use the FieldInternalName and not the Displayname.
   providerArr = ['Title','carModel','carColor','carInterior','carMilage'];
   consumerArr = ['Title','Model','Color','Year','Milage'];
   cascadingDropDowns_v2(providerArr,consumerArr,'{83eb224b-03fa-4a8b-b493-80253373a962}','<select>',5,true,false);
</script>

Parameters explained:

  • providerArr: Array of FieldInternalNames of the fields in the source-list
  • consumerArr: Array of FieldInternalNames of the fields in the list where the dropdowns will be created
  • lookupSourceListGuid: GUID of the source-list
  • dropDownDefaultvalue: The default value of dropdowns that are not empty – ex. <select>
  • numberOfDropdowns: Number of levels of cascading dropdowns – (2-5)
  • debug: true or false – if true: the textfield that holds the dropdownvalue is visible and some alerts are displayed

The code for the file “spjs_CascadingDropDowns_v2.js” can be found here

How to use these scripts in a customized form

Ask if anything is unclear.

Alexander

103 Responses to “Cascading dropdowns in SharePoint text fields – populated by lookup in another list (version 2)”

  1. Peter Says:

    Hi Alex,

    I have your cascading drop down controls on an upload doc form after the user selects a document they then specify some meta-data that utilizes your solution. It works the majority of the time but every now and then on specific files it defaults the first drop down list with a value instead of letting the user select it. This is not that big of a deal in of itself but it somehow breaks everything after the user selects the right fields and tries to upload. They then get an error and I can’t figure what is wrong

  2. vinay s Says:

    I have the applied the code with below script

    // Use the FieldInternalName and not the Displayname.
    providerArr = ['Title','Task'];
    consumerArr = ['Title','Tasklist'];
    cascadingDropDowns_v2(providerArr,consumerArr,’{E46DF04E-EC51-4F2B-A7AD-DCC166291D0E}’,”,2,true,false);

    however feild are not gettign converted to drop down and also it shows a message ” Please wait while scripts are loaded

    Can u pls help

    • vinay s Says:

      // Use the FieldInternalName and not the Displayname.
      providerArr = ['Title','Task'];
      consumerArr = ['Team','Tasklist'];
      cascadingDropDowns_v2(providerArr,consumerArr,’{E46DF04E-EC51-4F2B-A7AD-DCC166291D0E}’,”,2,true,false);

  3. vinay s Says:

    I fixed my above issue however i got a new one , sorry to trouble you more , Can you please advise why i am getting new error as “The fieldname specified as “consumerArr Nr1″ does not exist. You must use “FieldinternalName” to identify the fields”
    I have crossed check twice or thrice on the code however still the problem persit

    <script type="text/javascript" src="/sites/infyetd/WebDoc/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="/sites/infyetd/WebDoc/spjs_CascadingDropDowns_v2.js"></script>
    <script type="text/javascript">
       // Use the FieldInternalName and not the Displayname.
       providerArr = ['Title','TaskN'];
       consumerArr = ['Title','TasklistS'];
       cascadingDropDowns_v2(providerArr,consumerArr,'{E46DF04E-EC51-4F2B-A7AD-DCC166291D0E}','<select>',5,true,false);
    </script>
    
    • Alexander Bautz Says:

      Hi,
      From the error message i would guess that ‘TasklistS’ is not the correct FieldInternalName. FieldInternalName’s are case sensitive so please check it a forth time :-)

      Alexander

    • Alexander Bautz Says:

      Sorry, my mistake
      consumerArr Nr1 would be “Title”…

      This is a “built in” field and should be correct. Is the list modified in any way?

      Alexander

    • vinay s Says:

      No, Not at all.
      I tried doing it again with new list and i am getting the same error

    • Sudha Says:

      I have the same problem explained by Vinay. I am getting the same error “The fieldname specified as “consumerArr Nr1″ does not exist. You must use “FieldinternalName” to identify the fields”
      It is working fine in 2007 but not on 2010. Any help would be greatly appreciated. Alex any thoughts?

    • Alexander Bautz Says:

      Hi,
      What kind of list is it you are trying to use it with?

      Alexander

  4. larry Says:

    Playing with tis script I am trying to use a people picker at the end of the the DD. It is not the result I am looking for, but it does populate a dropdown with the poeple picker information from the sourcelist poeple picker field. Is there something that can be easily changed to allow for the use of a people picker field on the target list?

    • Alexander Bautz Says:

      Hi Larry,
      This script deals with text-fields only. You could however use another script in PreSaveAction() to populate the real people picker based on the cascading dropdown value.

      Alexander

  5. Larry Says:

    bugging you again. I know this script hides the original fieldsand displays the dropdowns. If I wanted to set the first dropdown value, from a query string value. Is it possible without modification to the original script?

    • Alexander Bautz Says:

      Yes, just set the underlaying text filed to a valid choice before calling the cascading dropdown script. It will pick up the value in the text field and preselect it.

      Alexander

  6. Melissa Says:

    Do you have any information on how to use PreSaveAction()? I have would like to create a cascading drop down with the first dropdown (department) as a regular text box and the second a people picker that automatically populates the manager’s name for each department.

    Thanks!

    • Alexander Bautz Says:

      Hi,
      If present, the function PreSaveAction is executed before save on both NewForm and EditForm in SharePoint.

      The content you put in the function is up to you. I do not think there are more to say about it?

      Alexander

  7. Melissa Says:

    This code works awesome! Is there anyway to manipulate it so that after the first dropdown is chosen it will automatically choose the other two instead of defaulting to select?

    The way my site is designed is that you chose a person’s name then their managers will populate 2 cascading dropdowns. Each person will always have the same managers that populate.

    Thanks!

    • Alexander Bautz Says:

      Hi,
      Sorry for the late reply. This solution can not do that as it uses single line of text fields only. It is possible to create such solution, but i do not think i have anything like this posted.

      Alexander

  8. Tom Says:

    Hi Alexander,

    I need your help again. What would I need to change in this script to populate a editform with column data from another list based on a lookup.

    So I have one list with lots of columns [Field1, Field2, Field3..] and a second list with the same columns plus a lookup linked to the first list. When the lookup is selected the edit form is populated with the data from the first list.

    Thanks again

    Tom

  9. Jeff H Says:

    Will this script work with SharePoint 2010?

  10. Tony Says:

    Is it possible to default to a value by location in the list

    • Alexander Bautz Says:

      Not quite sure what you want, but if you insert a valid value into the text field before calling the “cascading dropdown code”, the value will be picked up.

      Alexander

  11. Tony Says:

    for some reason the edit form does not update, it displays and I can make chnages but it does apply the changes to the text field?

  12. Tony Says:

    Well tried to copy code but didnt work, anyways the code works on newform like a charm but on edit form it displays the drop down and works as advertised but does not store value……

  13. Vitalii Says:

    Hi!
    I have used your script. It helped me in my job. But I have a question: can I use my first value as a default?

    • Alexander Bautz Says:

      Hi,
      Yes, set a default value for the first column in the list settings. As long as this is a valid choice, the dropdown should pick it up.

      Alexander

  14. Manny Tihan Says:

    The link for the spjs_CascadingDropDowns_v2.js JQuery is not working. Could you please post the working JQuery link or email me the JQuery ?

    Thanks
    1MT

  15. Manny Tihan Says:

    Hi Alexander,

    Thanks for the reply. I got the JQuery from jquery.com for Jquery version v1.6.4 but what i meant to ask was the link where code for file “spjs_CascadingDropDowns_v2.js” could be found is not working.
    The hyperlink to the following is not working in your article.
    “The code for the file “spjs_CascadingDropDowns_v2.js” can be found here.”
    So could you lease email me the code so that i could create the spjs_CascadingDropDowns_v2.js file.

    Thanks
    1MT

    • Alexander Bautz Says:

      Hi,
      There should have been a redirect from the old server to the new… I have updated the link.

      Alexander

  16. Larry Says:

    hey A,
    have a question about this script. I have a unique scenario where it goes 4 levels down. at the fourth level I have 4 different fields, but all four of them should be filterd by the third field. the all four fields at the fourth level pull the same data, but when I try to add the script a second time it replicates the dropdowns.
    is there a way to have it work like
    field1 filters field2
    field2 filters field3
    field3 filters field4,field5 ,field6, field7

    • Alexander Bautz Says:

      This is not possible using the “out of the box solution”, but it is possible using another script to pull the four parallel dropdowns separately based on the selection in the last “cascading dropdown”.

      It would have to be custom made though.

      Alexander

  17. Larry Says:

    Hey A,
    I figured it’s about time to bug you one again. I have implemented this script on a SP 2010 discussion board. NewForm, works great, EditForm, works great. When User clicks reply, the cascade works perfectly, but the dropdowns are not populated. Is there something I can do or add to get this value set to what was selected on the NewForm, like the editform works?

    • Alexander Bautz Says:

      Like this:

      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
      <script type="text/javascript" src="http://spjsfiles.com/SharePoint%20JavaScripts/spjs-utility/28.02.2012/spjs-utility.js"></script>
      <script type="text/javascript">
      var arrOfFieldsToSet = ['Level1'];
      ExecuteOrDelayUntilScriptLoaded(getValuesfromParent, "sp.js");
      
      function getValuesfromParent(){
      	var pid, item;
      	pid = GetUrlKeyValue('DiscussionParentID');
      	if(pid!==''){
      		item = spjs_getItemByID({listName:_spPageContextInfo.pageListId,id:pid,viewFields:arrOfFieldsToSet});
      		if(item!==null){
      			$.each(arrOfFieldsToSet, function(i,fin){
      				setFieldValue(fin,item[fin]);
      			});
      		}
      	}
      	// Call "Cascading dropdowns" here
      }
      </script>
      

      Put this code in NewForm.aspx and change the array “arrOfFieldsToSet”

      Note that you have to update spjs-utility.js – I have linked it directly from my download site, but I recommend you download the file locally.

      Alexander

  18. Mary Says:

    I am using the cascading dropdown code on one of my sites and I am getting the following script error: ‘consumerArr.1′ is null or not an object

    I am referencing the most recent version of the cascading dropdowns code and using jquery 1.6.4-min.js

    My form is not custom.

    Have you every seen this error before?

    Thanks

    • Mary Says:

      also i have sharepoint 2007

    • darkwizard50 Says:

      I’m having the same issue. After selecting from the first dropdown then the error shows. I am using 2007 and have only 2 dropdowns. I have tried jquery 1.6.4 and 1.7.1 to no avail….

      Alex, can you please at least look into this?

    • Alexander Bautz Says:

      Hi,
      Sorry for the late reply. I have a few questions:
      Do you have other scripts in the same form? – if so there might be a conflict with other code. Could you try removing all other scripts to test?

      Which browser are you using?

      Alexander

  19. Naseer Says:

    Hi every one ,
    i have Created two Lists (Custom Share point list)
    List1 and List2, List1 is Having columns ProductName and Supplier both (single line of text)

    Similarly

    List2 has ProductName and Supplier as Lookups columns of List 1 respectively
    I have created javascript code as follows:
    ————————————————————————————-

    // Use the FieldInternalName and not the Displayname.
    providerArr = ['Title','ProductName','Supplier'];
    consumerArr = ['Title','ProductName','Supplier'];
    cascadingDropDowns_v2(providerArr,consumerArr,’{eec99d33-8a4b-4a3e-8c9c-fd7333d7c8df}’,”,2,true,false);

    ————————————————————————————-
    Have changed the GUID for the resource List ,
    But the system is showing some error :
    “The fieldname specified as “consumerArr Nr1″ does not exist.
    You must use “FieldInternalName” to identify the fields.”

    Many thanks Alexander

    • Alexander Bautz Says:

      Could you try using another field as “consumerArr Nr1 (Not the “Title” field)?

      Alexander

    • Rashmi Says:

      It works if you dont use Title in the arrays in SharePoint 2010…

  20. Philo Janus Says:

    Looks like with SharePoint 2010 if you have the Silverlight capabilities enabled this script won’t work – the javascript loads when the page loads, and the edit controls aren’t there for it to hook. When you click “edit” to open the edit form, the javascript is already dead, so it doesn’t run.

    • Alexander Bautz Says:

      Hi, What do you mean by “Silverlight capabilities”? is it the opening in a modal dialog?

      Alexander

  21. Rashmi Says:

    Hi Alexander,
    Great solution , I got it working in 1st attempt :D
    Although-
    I tried having two different source lists for two dropdowns each using 2 CEWPs…One works , but the other doesnt show its cascading dropdown. any idea how i can make it to work?

    if i toggle the positions of the CEWPs the bottom most doesnt work…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers