设为首页 收藏本站
查看: 688|回复: 0

[经验分享] Filter Lookup List in Sharepoint

[复制链接]

尚未签到

发表于 2015-9-28 14:06:53 | 显示全部楼层 |阅读模式
  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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-120081-1-1.html 上篇帖子: 创建Sharepoint 2010 custom WebService 下篇帖子: 一步一步SharePoint 2007之二十:解决实现注册用户后,自动具备访问网站的权限的问题(2)——配置Role
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表