我正在創建一個SharePoint應用程序,並希望儘可能經常以正確的方式執行操作。此數據訪問模式在SharePoint中可以接受嗎?
我在網站定義中部署了一系列列表,我想將數據訪問鎖定到一系列Get()方法來維護約定。
我的一些列表在列表定義中設置了SecurityBits =「22」,因爲我希望僅在UI中修改List Item條目。
我想避免濫用SPSecurity.RunWithElevatedPrivileges
。我也想避開SPSecurity.RunWithElevatedPrivileges
的限制,你不能在代表中有return
函數。
這似乎是一個強制執行此操作的好方法。如果您打電話給列表以獲取具有正常安全性的列表項目,則可以致電var PostList = CoreLists.Posts
。如果您需要使用提升的權限致電該列表,請撥打var PostList = CoreLists.SystemAccount.Posts
這是一個很好的方法嗎?
public static class CoreLists
{
public static SPList Posts()
{
return SPContext.Current.Web.GetList(SPContext.Current.Web.ServerRelativeUrl + "/lists/CommunityPost");
}
public static class SystemAccount
{
public static SPList Posts()
{
using (var elevatedSite = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.SystemAccount.UserToken))
using (var web = elevatedSite.OpenWeb())
return web.GetList(web.ServerRelativeUrl + "/lists/CommunityPost");
}
}
}