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.
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 listif(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
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.
You can refer to this link as well
No comments:
Post a Comment