Function

$().SPServices.SPCascadeDropdowns

Functionality

The SPCascadeDropdowns function lets you set up cascading dropdowns on SharePoint forms. What this means is that you can enforce hierarchical relationships between column values. The function uses the GetListItems operation of the Lists Web Service to refresh the allowable values based on relationships which are maintained in reference lists. By implementing this function, you can remove all of the coding requirements to manage the hierarchical relationships (once it is in place) and let your users manage the content.

This function should work with any number of options in the dropdowns. It will not work with multi-select columns, though that enhancement is planned.

Note that "multiple cascades" are supported, such as Country -> Region -> State. In this example, we have two "cascades" in place: Country -> Region, and Region -> State. There's not a lot to show here, but the available options in the dropdowns will change based on the relationships defined in the lists shown below. So, if you choose Country = United States, the options for Region will be limited to Northeast, Southeast, Midwest, Mountain, Southwest, Northwest. If you choose Country = Canada, the options for Region would be Eastern Provinces, Western Provinces.

Demo Page

Take a look at our demo page.

Prerequisites

  • Relationship list contains at least two columns: relationshipListParentColumn and relationshipListChildColumn
  • The dropdown for childColumn is a lookup into relationshipList's relationshipListChildColumn column
Countries List

Regions List

States List

Syntax

$().SPServices.SPCascadeDropdowns({
	relationshipWebURL: "",		// [Optional] The name of the Web (site) which contains the relationships list
	relationshipList: "",		// The name of the list which contains the parent/child relationships
	relationshipListParentColumn: "",	// The StaticName of the parent column in the relationship list
	relationshipListChildColumn: "",	// The StaticName of the child column in the relationship list
	relationshipListSortColumn: "",		// [Optional] If specified, sort the options in the dropdown by this column,
					// otherwise the options are sorted by relationshipListChildColumn
	parentColumn: "",			// The DisplayName of the parent column in the form
	childColumn: "",			// The DisplayName of the child column in the form
	debug: false			// If true, show error messages; if false, run silent
});
By StaticName above, we mean the underlying column name, e.g., Region_x0020_Name . The DisplayName would be Region Name, i.e., the name of the column which is shown on the form.

Setting debug: true indicates that you would like to receive messages if anything obvious is wrong with the function call, like using a column name which doesn't exist. I call this debug mode. As with all of the functions in the library, this function "runs silent", meaning that no messages or alerts are presented to the user if something goes wrong. This means that no inherent functionality is lost due to a problem. By using debug mode, you can receive messages to help you get things set up; I recommend turning debug mode off once everything is in place and running correctly.

Examples

This is the sum total of what you'll need to add to your page to make the function work for the example above. The first two lines simply pull the script files into the page, and the $(document).ready(function() line is a jQuery function that says "Run this script when the page has been fully rendered". In the first call to the function, we're turning debug mode on by setting debug: true.
<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery-1.3.2.js"></script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery.SPServices-0.4.5.min.js"></script>
<script language="javascript" type="text/javascript">
	$(document).ready(function() {
		$().SPServices.SPCascadeDropdowns({
			relationshipList: "Regions",
			relationshipListParentColumn: "Country",
			relationshipListChildColumn: "Title",
			parentColumn: "Country",
			childColumn: "Region",
			debug: true
		});
		$().SPServices.SPCascadeDropdowns({
			relationshipList: "States",
			relationshipListParentColumn: "Region",
			relationshipListChildColumn: "State",
			relationshipListSortColumn: "ID",
			parentColumn: "Region",
			childColumn: "State"
		});
	});
</script>
Last edited Nov 12 at 5:32 AM by sympmarc, version 15
Comments
Toadmyster Aug 28 at 12:10 AM 
Beautiful! I have a feature suggestion for this function. How about an option to display either the child column name OR parent column name in the default choice of the child control? In other words, instead of the dependant column State displaying "Choose State" instead have it read "Choose Region" to indicate that you can't choose a state until a region has been selected. If it did both automatically (displayed "Choose Region" until a region was chosen and then switched to "Choose State" after a valid region has been selected) that would be sweet!

EdwardDavis Sep 30 at 5:32 PM 
It worked perfectly. But after changing the list design, the form displayed prompts in the drop-downs. We are are unable to re-create those. The drop-downs initially showed nothing for a default value. After the changes, the drop-downs show "Choose Author... , where "Author" is the name of the relathionshipListChildColumn. This looks professional and is well received by the client. We've tried everything to re-create this default with the drop-downs, with no success. Feature or corruption?

branleejo Sep 30 at 9:04 PM 
I am new to sharepoint and codeplex, and the above all seems to make sense, although it feels like some information is missing. Perhaps not since others were able to get it working, but I am not able to make this work on my site and I would really appreciate some help. I have been searching for a couple of weeks for a solution to cascading drop-downs with SharePoint. If anyone can help me, please feel free to contct me.

sympmarc Sep 30 at 10:53 PM 
EdwardDavis: I'm not sure I understand your question. It sounds like things were working, you made some changes to the list design, and then things didn't work. What changes did you make?

branleejo: I'd be happy to help. Why don't you post your questions in the Discussions area; they may help others as well. (Each time someone needs help, it's given me a reason to tweak the docs, but obviously I'm not quite there yet.)

branleejo Oct 1 at 3:05 PM 
Marc: thanks for the reply. I'm new to the SharePoint product, but to this point, everything has been extremely straightforward and simple to understand. Now I'm to the point that I need some more advanced functioanality that apparently isn't available out of the box, like your cascading dropdowns. I have read, and re-read this page, but I seem to be missing how to put this all together, and I appologize for that because I'm sure it's due to my being new to the product.

I understand that you have created two Custom Lists - Regions and States.

The Regions List contains the columns: Title (Region) and Country
The States List contains the columns: State, State Abbreviation and Region

Do I have it correct that these are the only two lists that were created, and that these the only fields?

Also, I understand that the Region field in one of the tables has to be a lookup to the data in the Region field of the other table, but I'm not clear on which table contains the data and which is the lookup.

I have downloaded the two jQuery files and saved them into the specified folder on my site. Now this will be a dumb question to others, but which page are you editing with the code above. Is it the NewForm.aspx page for one of the lists?

sympmarc Oct 1 at 8:28 PM 
branleejo: I'm going to copy this over to the Discussions area and answer you there. I can't get email notifications of your comments here and I don't want to miss what you post.

M.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987