我們有一個要求,用戶無權訪問的Sharepoint列表的項目必須仍然在前端對用戶可見,並且可以選擇「請求訪問「,如果有必要。爲此,我們有一個Web服務,首先獲取列表中的所有項目,然後下一個僅獲取用戶有權訪問的項目。Sharepoint - 獲取兩個SPListItemCollection之間的差異
// Get all the items in list
SPUser superUser = SPContext.Current.Web.AllUsers[@"SHAREPOINT\SYSTEM"];
SPUserToken superToken = superUser.UserToken;
SPList superList;
using (SPSite site = new SPSite(SPContext.Current.Web.Url, superToken))
{
using (SPWeb elevatedWeb = site.OpenWeb())
{
superList = elevatedWeb.Lists.TryGetList(listname);
}
}
SPListItemCollection superListItems = superList.GetItems(camlQuery);
// Get items in list accessible to current user
SPUser regularUser = SPContext.Current.Web.CurrentUser;
SPUserToken regularToken = regularUser.UserToken;
SPList regularList;
using (SPSite site = new SPSite(SPContext.Current.Web.Url, regularToken))
{
using (SPWeb regularWeb = site.OpenWeb())
{
regularList = regularWeb.Lists.TryGetList(listname);
}
}
SPListItemCollection regularListItems = regularList.GetItems(camlQuery);
接下來,需要計算兩個list-item-collections之間的差異。這應該已經是很簡單的,如果這是可能的:
// Now make a list of the items that the current user DOES NOT HAVE ACCESS TO
SPListItemCollection noAccessListItems;
foreach (SPListItem superListItem in superListItems)
{
if(!regularListItems.Contains(superListItem)) // Fails here
{
noAccessListItems.Add(superListItem);
}
}
唯一的問題是,SPListItemCollection
不支持.Contains()
方法。
獲得兩個list-item-collection對象的區別的最好方法是什麼?
謝謝!這似乎是一個更好的解決方案。您可以詳細說明LINQ解決方案與try-catch的性能嗎?此外,如果您展示瞭如何爲SPListItem編寫比較器,那將是一件好事。 – SNag
@SNag,這不是關於性能(我不認爲它真的很不一樣)。這是關於代碼可讀性的。更新了我的答案以包含比較器代碼段。 –
再次感謝!如果它是關於可讀性的話,那麼IMO的LINQ比try-catch更難閱讀。比較器解決方案雖然是最好的。我給了你一個投票並接受你的答案。 :) – SNag