我正在创建一个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");
}
}
}