Thursday, June 20, 2013

Implement Cascading drop down in InfoPath


In this post we are going to see how we can have a cascading drop down. Nothing can be simpler than taking an example of classic country, state and cities.

So let's first create these three list and values with lookup values.

First we will have countries list, then a state which will have country lookup and then city which will have a look up to the state list.

For simplicity I am going to take only two three values in country, two three values in state and two three values in the city list.

Here are how I have set up.

Country


State





City


Now we are going to use these into one more list and then customize the list in InfoPath.

I have order list with me and I have three columns each lookup to respective list title field.



Let's customize the list in InfoPath by clicking Customize form on the ribbon button.



So now we have this. Publish the form back to the SharePoint by clicking on quick publish.



Alright now to the point, we have country which has no dependency, then we have state which has dependency on country and finally city which has dependency on state.

So here we do not need to do anything for the country.

Out of the box we will not have this cascading drop downs. We would need to create two more data sources. One for state and one for city.

So let's go ahead and create those two data sources.

Click on manage data connections. you will see something like this.



Click on add. Select Receive data in next screen. Select SharePoint list and library in next screen.

Click on next and select State list. On next screen make sure that you select these three.



and on final screen give a name of the connection and select automatically get the data when the form is opened.

Now we will repeat the same steps for the city. Create a new connection by clicking on Add on manage data connection window.

Repeat the same steps as we did earlier and this time select city list. Give name and add the connection to the form.



So finally I have something like this.



Now it's time to configure the drop down.

Open the InfoPath page and select state drop down and click on property. Change the data source from state to GetStates and then in value tree, instead of Title select ID.





So it will be like this



Same way change it for the city and it should look like this.



 So far so good. Let's just preview out form and see that we are doing good so far.



Alright we still have not configured the cascading look up. All the above steps which we performed where necessary and now is the time to set it.

Right click on State drop down and select property.

Select entry xPath.



Click on filter data.



Click add and then select this condition.



When you click select field or group, a window opens up.



Now change from GetStates (secondary) to main data source.



Now from main data source select country.



Click on ok three times and then apply and ok.

Repeat the same step for city but this time make sure that for filter condition you select State equals main data source state.





Alright, give it a go this time and see what happens.

Let's try to preview the form and select Australia as country.














There is one more setting we need to do here to make it 100 percent complete.

Select country drop down. click on manage rules. add new action.



Select set field's value.



Set State drop down to blank. Same way also set city field value to blank by adding one more action for country drop down.

Click on ok.

Repeat the same steps for the state drop down. Select state drop down. click on manage rule. Add action. Set field. Select City and set field value to blank.

We did these steps because when we change the country we need previously selected state and city to get cleared. When we change state drop down we need previously selected cities to get cleared.

You should now be good to go. Enjoy cascading drop down.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com