在發佈網站中,我有必須顯示具有受衆羣體定位字段的列表中的新聞項目的Web部件。我正在使用CAML查詢來檢索少量的最新消息。SharePoint 2007發佈網站和受衆在Web部件中定位
是否可以在CAML查詢中指定目標受衆?如果不是,我該怎麼做?檢索所有結果並在循環中應用過濾器?
我實際上正在複製內容查詢Web部件,我需要在我的自定義Web部件中設置受衆定位。
在發佈網站中,我有必須顯示具有受衆羣體定位字段的列表中的新聞項目的Web部件。我正在使用CAML查詢來檢索少量的最新消息。SharePoint 2007發佈網站和受衆在Web部件中定位
是否可以在CAML查詢中指定目標受衆?如果不是,我該怎麼做?檢索所有結果並在循環中應用過濾器?
我實際上正在複製內容查詢Web部件,我需要在我的自定義Web部件中設置受衆定位。
不,不可能在CAML查詢中指定受衆羣體定位。我認爲這與CAML查詢是WSS的事情有關,而觀衆是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存在觀衆和母校只要使用默認accesor AudienceManager.Audiences的得到它的參考[GUID]。