SharePoint 2010: Filter DropDown within Displayform.aspx with JavaScript

Case

Try to filter a lookup column within a standard SharePoint Edit Form just using JQuery and JavaScript.

Prerequisites

  • You need a SharePoint Library to store your JavaScript and two JQuery Library’s
  • You need a LookUp Column
  • You need a Query List with a filter criteria

How it works

We have two lists, one list for your projects and one list for our tasks. Within the task list there is a lookup column which references to the project list. Our project list also stored the status of a project so we are able to see if a project is active, inactive or planed. As a result you should only get the active Projects e.g. P12-002-20 and P12-002-10 as options if you create a new task.

Sample Picture for Project List

Sample Picture for Selecting only the Active Projects

Step by Step

  1. Create a library for all JAVA Script stuff
    1. Upload jquery.SPServices-0.7.1a.min
    2. Upload jquery-1.7.1.min
    3. Upload your JAVA Script File
  2. Create a project list
    1. Add a choice field for project status
    2. Add a text field to store project number
  3. Create a Task list
    1. Add a lookup column to the project list
    2. Reference to the project number field
  4. Edit the default edit form for the Task list
    1. Add a CEWP Content Editor Webpart
    2. Reference the content of the CEWP to the JAVA Script File
  5. Now test your solution

Sample Picture for CEWP Settings

Sample Picture for JAVA Script Library

Sample Code

[sourcecode language=’JavaScript’]
$(document).ready(function() {
alert($().SPServices.SPGetCurrentSite());
});
$().SPServices.SPFilterDropdown({
relationshipList: “My Projects”,
relationshipListColumn: “ProjectNumber”,
columnName: “Project Number”,
CAMLQuery: “active“,
completefunc: null,
debug: true });
[/sourcecode]

More information about $().SPServices.SPFilterDropdown: http://spservices.codeplex.com/wikipage?title=$().SPServices.SPFilterDropdown

Download sample text file sample_getformfields

..:: I like SharePoint ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Dennis de Vries 34 Articles
Loving SharePoint, Social Media and like to work together with creative people all over the world.

Be the first to comment

Leave a Reply