Filter Lookup List in Sharepoint

Author : Patrick Tisseghem

SharePoint
allows you to create custom lists with your own custom columns. One type you can use for these columns is a lookup against one of your other lists in the SharePoint site. By default this lookup gives the user all the items that are available in the list. Many times, you probably want to have a filtered list instead of the complete one.

Lately, I had to do exactly this for a customer. The approach explained here is probably not the only one you can take but it is one you can start with and it is not difficult to incorporate that within your NewForm/EditForm.aspx (the pages where you actually see the lookup). The approach I have taken is a small JavaScript function that repopulates the dropdown based on the items in the lookuplist that are visible via a view you create. Take for example a list that allows users to submit questions to SharePoint. Besides columns for capturing data regarding the question itself, the user also has to select the person who has to handle the question. In our case, the person submitting the question has one or more roles he or she can select from. A custom list storing this information is create to support the lookup that needs to be done.

When you create the Questions list, you can create a column Role that points to that second lookup list. Problem is that everything out of that list is displayed to the user and we want to have the user only see those entries that are relevant for him or her. So, I first thing I did was to create a new view on the second list that only displays all the roles for the logged on user. This view has the filter set to [Me] (a common practice when creating views).

Next thing you do is to open up the EditForm.aspx of the first list (the questions one) in FrontPage. At the bottom of the HTML (just before the closing BODY tag) you can add the following script blocks.

<script language="javascript" src="/sites/development/repopulate.js"></script>
<script language="javascript">
//
drop here all the lists you want to have repopulated based on a view parameters are:
// controlName - the exact name as it appears in the HTML sent to the browser
// siteName - the root / and possibly sub site you are working on
// lookupListName - the guid for the list you want to use as lookup
// lookupViewName - the guid for the view filtering the content of the list
// textField - the field you want to show to the user
// valueField - this is most of the time the internal ID field
RepopulateList("urn:schemas-microsoft-com:office:office#Role", "/sites/development", "{4E933C67-848B-4DAF-A383-B99BC9AEAFA7}", "{C97B2EFD-B552-490D-9B05-61B2B88836D2}", "ows_LinkTitle", "ows_ID");
</script>

The first block points to a Javascript file with a generic function to repopulate lists (note that it needs more exception handling to become really generic!). Here is the content of that file:

function RepopulateList(controlName,siteName,lookupListName,lookupViewName,textField,valueField)
{
// -- retrieve the list for the elements var listEl = document.getElementsByName(controlName)
// -- emptying the list
if(listEl.length>0)
{
 
listEl(0).innerHTML = "";
 // -- getting the filtered lookup
 var reqstring = siteName + "/_vti_bin/owssvr.dll?CS=109&XMLDATA=1&RowLimit=0&List=" + lookupListName + "&View=" + lookupViewName;
var req = new ActiveXObject("MSXML2.XMLHTTP");
req.open("GET",reqstring,false);
req.send();
// -- loading response in XML Document
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.loadXML(req.responseText);
var data = doc.documentElement.childNodes(1);
for (i=0;i<data.childNodes.length;i++)
{
   var optionText = data.childNodes(i).attributes.getNamedItem(textField).value;
  var optionValue = data.childNodes(i).attributes.getNamedItem(valueField).value;
  var opt = document.createElement("OPTION");
  listEl(0).options.add(opt);
  opt.innerText = optionText;
  opt.value = optionValue;
}
}
}

The second script block calls this function providing it the necessary values to go back to the server and retrieve the XML with only the data provided by the created view. I use that XML to repopulate the list then. Notice the owssvr.dll I am calling in the script. When you look at a view in a SharePoint, you can always view the source of that page and at the bottom of the page you will find the following information: You can copy this link and run it in the browser. The result is the following: This is the response I need to repopulate the dropdown then with only the roles for the current visitor. The performance is not that great if you apply this technique for big lookup lists so be careful. Test it first for your own specific scenario. To filter big lookup lists, you better have a look at your own custom ASP.NET control. If I have the time, I will give you a starter. This week however I give my first Whidbey 5-day workshop and I will probably be 'very' busy preparing all of this. Please feel free to comment on this technique. If you have done this already using another technique (maybe CAML?), let us know.

posted @ 2007-09-05 10:38  AutodeskWebDev  阅读(1077)  评论(0编辑  收藏  举报