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, there are no coding requirements to manage the hierarchical relationships (once it is in place) and you can let your users manage the content in the reference lists.

This function works with any number of options in the dropdowns as well as multi-select parent and child columns, as shown in the following table. This is significant because each of the three column types are rendered significantly differently by SharePoint.
parentColumn
<20 options 20+ options multi-select
childColumn <20 options
20+ options
multi-select

When the relationshipList contains lookup columns for both the relationshipListParentColumn and relationshipListChildColumn columns, the function uses the relationshipListParentColumn's ID rather than the relationshipList item's ID. This means that "secondary lists" are also supported.

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 OR a list column which is a lookup into another list column ("secondary list").
Countries List

Regions List

States List

Syntax

$().SPServices.SPCascadeDropdowns({
	relationshipWebURL: "",
	relationshipList: "",
	relationshipListParentColumn: "",
	relationshipListChildColumn: "",
	relationshipListSortColumn: "",
	parentColumn: "",
	childColumn: "",
	CAMLQuery: "",
	promptText: "Choose {0}...",
	completefunc: null,
	debug: false
});

relationshipWebURL
The URL of the Web (site) which contains the relationshipList. If not specified, the current site is used. Examples would be: "/", "/Accounting", "/Departments/HR", etc. Note: It's always best to use relative URLs.

relationshipList
The name or GUID of the list which contains the parent/child relationships. If you choose to use the GUID, it should look like: "{E73FEA09-CF8F-4B30-88C7-6FA996EE1706}". Note also that if you use the GUID, you do not need to specify the relationshipWebURL if the list is in another site.

relationshipListParentColumn
The StaticName of the parent column in the relationship list

relationshipListChildColumn
The StaticName of the child column in the relationship list

relationshipListSortColumn
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

promptText
Text to use as prompt. If included, {0} will be replaced with the value of childColumn. The default value is "Choose {0}...".

completefunc
If specified, the completefunc will be called each time there is a change to parentColumn. Potential uses for the completefunc: consistent default formatting overrides, additional lookup customizations, image manipulations, etc. You can pass your completefunc in either of these two ways:
completefunc: function() {
  ...do something...
},
or
completefunc: doSomething,                  // Where doSomething is the name of your function

CAMLQuery
The CAMLQuery option allows you to specify an additional filter on the relationshipList. The additional filter will be <And>ed with the existing CAML which is checking for matching items based on the parentColumn selection. CAMLQuery should contain a CAML fragment such as:
CAMLQuery: "<Eq><FieldRef Name='Status'/><Value Type='Text'>Active</Value></Eq>"
debug
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.

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, note that 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 Jan 18 at 9:05 PM by sympmarc, version 33
Comments
mooneymdp Jan 12 at 3:02 PM 
Is there a limit on the number of items that can be in the list? I was using this list to do a lookup on a company and then the employees associated with that company. When I have a small number of companies it works fine but when I go over about 100 the duplicates do not get removed from the list. Below is the code I have added.

$(document).ready(function() {
// Make sure each Company name only appears once in dropdown list
var uniqueNames = {};
$("select[title='Company'] > option").each(function () {
if(uniqueNames[this.text]) {
$(this).remove();
} else {
uniqueNames[this.text] = this.value;
}
});
// restrict the Contact Name List to selection in Company dropdown
// From http://spservices.codeplex.com/wikipage?title=%24%28%29.SPServices.SPCascadeDropdowns&referringTitle=Documentation
$().SPServices.SPCascadeDropdowns({
relationshipList: "Contacts",
relationshipListParentColumn: "Company",
relationshipListChildColumn: "Title",
parentColumn: "Company",
childColumn: "Contact Name",
debug: true
});

sympmarc Jan 17 at 7:30 PM 
mooneymdp:

Please see this thread in the Discussions for my reply on this: http://spservices.codeplex.com/Thread/View.aspx?ThreadId=81101

Just a note: There's no way for me to set an alert for comments on the Documentation pages, so I didn't see your comment until now. Because of this, it's probably better to use the Discussions instead with a link back to the Documentation page.

M.

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