Jan 20, 2010

No-code cascading lists in Sharepoint

Here's how to do cascading lists in Sharepoint without resorting to custom webparts:

References:
Steps:

  1. Create the master list.
  2. Create the slave list using a lookup column to link elements to the master list
  3. In your page, create an SPDataSource to the master list
  4. Create a DropdownList linked to the first SPDataSource
  5. Create a second SPDataSource with a ControlParameter linked to the first dropdownlist and the required changes to the view query to accept this parameter.
  6. Create a dropdownlist connected to the second SPDataSource.
Example:


<SharePoint:SPDataSource runat="server" DataSourceMode="List"
UseInternalName="true"
selectcommand="<View></View>" id="categoriaspdatasource">
<SelectParameters>
<asp:Parameter Name="ListID" DefaultValue="3C001A55-F90B-4829-80BA-5374B939524E"/>
</SelectParameters>
</SharePoint:SPDataSource>
<asp:DropDownList runat="server" id="CategoriaDropDownList"
DataSourceID="categoriaspdatasource"
DataTextField="Label_ES"
DataValueField="Title" autopostback="true">
</asp:DropDownList>

<SharePoint:SPDataSource runat="server" DataSourceMode="List"
UseInternalName="true"
selectcommand="<View><Query><Where><Eq><FieldRef Name='Categoria'/><Value Type='Text'>{Categoria}</Value></Eq></Where></Query></View>"
id="subcategoriaspdatasource">
<SelectParameters>
<asp:Parameter Name="ListID" DefaultValue="FBB80C2B-EC23-426C-ACBE-9A595E20EE0C"/>
<asp:ControlParameter Name="Categoria" ControlID="CategoriaDropDownList" PropertyName="SelectedValue" />
</SelectParameters>
</SharePoint:SPDataSource>
<asp:DropDownList runat="server" id="SubcategoriaDropDownList"
DataSourceID="subcategoriaspdatasource"
DataTextField="Label_ES"
DataValueField="Title" EnableViewState="false">
</asp:DropDownList>

1 comment:

  1. Hello Lars,
    when I change the first dropdownlist "categoria" , the second DDL "subCategoria" doesn't change ...It keep the values for the 1st DLL value.

    the autopastBack is set to True ...

    Any idea ?

    Thanks.

    Alain.

    ReplyDelete