我有一个包含列表的SharePoint 2010页面。该列表包含多个项目和一个名为“Department”的字段,必须根据从用户配置文件中检索到的用户部门值过滤项目。
为此,我创建了一个功能,在激活时将UserContextFilterWebPart添加到页面并在UserContextFilterWebPart和XsltListViewWebPart之间建立连接。激活该功能后,我可以在页面设计模式中看到建立了连接但列表变空。然后我打开Web部件的菜单,选择“Connections”,然后选择“Send Filter Values To”并单击“List1”。当对话框出现时我什么也没做,只是单击“完成”按钮,它开始工作正常。任何人都可以解释一下为什么连接只有在我做那个手动的额外动作时才开始工作?必须采取哪些措施来解决?
当List.Views [0] .Query属性设置为适当的CAML查询时,我尝试了不同的方式,它也可以正常工作。但我被告知,由于性能和并行任务问题,这不是一个好方法。这是非常糟糕的行动方案吗? 以下是2种不同方法的代码。 提前谢谢!
UserStextFilterWebPart:
的1-s变体SPSite Site = new SPSite(URL);
SPWeb Web = Site.OpenWeb();
SPLimitedWebPartManager WPM = Web.GetLimitedWebPartManager(URL, PersonalizationScope.Shared);
XsltListViewWebPart List = WPM.WebParts[0] as XsltListViewWebPart;
UserContextFilterWebPart UCFWP = new UserContextFilterWebPart();
UCFWP.Title = "Current User Filter";
UCFWP.AllowEdit = true;
UCFWP.FilterName = "Current User";
UCFWP.SendEmptyWhenNoValues = true;
UCFWP.AllowClose = true;
UCFWP.ExportMode = WebPartExportMode.All;
UCFWP.AllowConnect = true;
UCFWP.AllowHide = true;
UCFWP.ProfilePropertyName = "Department";
UCFWP.ValueKind = UserContextFilterValueKind.ProfileValue;
UCFWP.ZoneID = "Main";
WPM.AddWebPart(UCFWP, UCFWP.ZoneID, 1);
WPM.SaveChanges(UCFWP);
ConsumerConnectionPointCollection consumerConnections = WPM.GetConsumerConnectionPoints(List);
ConsumerConnectionPoint addConsumerConnPoint = consumerConnections["DFWP Filter Consumer ID"];
ProviderConnectionPointCollection providerConnections = WPM.GetProviderConnectionPoints(UCFWP);
ProviderConnectionPoint addProviderConnPoint = providerConnections["ITransformableFilterValues"];
TransformableFilterValuesToParametersTransformer trans = new TransformableFilterValuesToParametersTransformer();
trans.ConsumerFieldNames = new string[] { "Department" };
trans.ProviderFieldNames = new string[] { "Department" };
SPWebPartConnection newConnection = WPM.SPConnectWebParts(UCFWP, addProviderConnPoint, List, addConsumerConnPoint, trans);
WPM.SPWebPartConnections.Add(newConnection);
带有CAML查询的第二个变体(不是用于特征而是用于Web部件):
SPSite Site = new SPSite(URL);
SPWeb Web = Site.OpenWeb();
SPLimitedWebPartManager WPM = Web.GetLimitedWebPartManager(URL, PersonalizationScope.Shared);
XsltListViewWebPart List = WPM.WebParts[0] as XsltListViewWebPart;
SPUser CurrentUser = Web.CurrentUser;
SPServiceContext context = SPServiceContext.GetContext(Site);
UserProfileManager upm = new UserProfileManager(context, false);
UserProfile up = upm.GetUserProfile(CurrentUser.RawSid);
String UserDepartment = up["Department"].Value.ToString();
SPView ListView = Web.Lists["List1"].Views[0];
ListView.Query = "<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>" + UserDepartment + "</Value></Eq></Where>";
ListView.Update();
答案 0 :(得分:2)
我遇到了连接两个Web部件的类似问题。我在这里找到了答案:http://kvdlinden.blogspot.dk/2011/02/programmatically-connect-two.html
请注意,该帖子描述了如何使用两个XsltListViewWebParts。为了在你的情况下使用它,我建议你:
下次启动功能时,您的连接应该与手工制作的连接相同。