在发布站点中,我有一个 Web 部件,它必须显示列表中具有受众目标字段的新闻项目。我正在使用 CAML 查询来检索少量最后的新闻项目。
是否可以在 CAML 查询中指定目标受众?如果没有,我该怎么做?检索所有结果,然后在循环中应用过滤器?
我实际上是在复制内容查询 Web 部件,并且我需要在我的自定义 Web 部件中定位受众。
在发布站点中,我有一个 Web 部件,它必须显示列表中具有受众目标字段的新闻项目。我正在使用 CAML 查询来检索少量最后的新闻项目。
是否可以在 CAML 查询中指定目标受众?如果没有,我该怎么做?检索所有结果,然后在循环中应用过滤器?
我实际上是在复制内容查询 Web 部件,并且我需要在我的自定义 Web 部件中定位受众。
不可以,不能在 CAML 查询中指定受众定位。我认为这与 CAML 查询是 WSS 事物和 Audiences 是 MOSS 共享服务有关。您需要做的是在 CAML 查询中包含受众字段,即在 SPQuery.ViewFields 属性中添加 <FieldRef Name='Target_x0020_Audiences'/>。然后在每个列表项上按受众筛选结果代码。使用 AudienceManager 类来测试当前用户是否是受众的成员。
好吧,我找到了一个解决方法,当我尝试检查当前用户是否是特定发布页面的受众成员以及该受众的名称时,我遇到了一个问题。这是我想出的解决方法。
// Run through the pages building the list items
foreach (SPListItem li in pages)
{
// Get a reference to the publishing page
PublishingPage p = PublishingPage.GetPublishingPage(li);
// Make sure the page has been approved
if (li.ModerationInformation.Status == SPModerationStatusType.Approved)
{
// Check if this page has an audience
if (string.IsNullOrEmpty(p.Audience))
// Add to everyone list
else
{
// Split the audiences
string[] Audiences = p.Audience.Split(';');
// Check each audience to see if this user can see it
foreach (string audPart in Audiences)
{
AudienceManager audienceManager = new AudienceManager();
// Get a reference to the audience
// IsGuid is an extenstion method i wrtoe
if (audPart.IsGuid())
{
if (audienceManager.Audiences.AudienceExist(new Guid(audPart)))
aud = audienceManager.Audiences[new Guid(audPart)];
}
else
{
if (audienceManager.Audiences.AudienceExist(audPart))
aud = audienceManager.Audiences[audPart];
}
// Ensure we have a reference to the audience
if (aud != null)
{
// store the item in the temp variables
switch (aud.AudienceName)
{
case "All site users":
// Add to everyone list
break;
case "Some List":
if (audienceManager.IsMemberOfAudience(web.CurrentUser.LoginName, aud.AudienceID))
{
// Add to other list
}
break;
case "Other List":
if (audienceManager.IsMemberOfAudience(web.CurrentUser.LoginName, aud.AudienceID))
{
// Add to other list
}
break;
}
}
}
}
}
}
正如您所看到的,它实际上只是通过使用 AudienceManager.Audiences.AudienceExist 检查受众是否存在,并通过使用默认访问器 AudienceManager.Audiences[GUID] 来获取对它的引用;