Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm creating a SharePoint application and want to do things the right way as often as possible.

I'm deploying a series of Lists in a Site Definition, and I want to lock down Data Access to a series of Get() methods to maintain conventions.

Some of my lists have SecurityBits="22" set in the list definition, because I want List Item entries to be modified only in the UI.

I want to avoid abusing SPSecurity.RunWithElevatedPrivileges. I also want to sidestep the limitation of SPSecurity.RunWithElevatedPrivileges where you cannout have return functions in the delegates.

This seems like a good way to enforce this. If you're calling a list to get list items with normal security, you can call var PostList = CoreLists.Posts. If you need to call that same list with elevated permissions, you can call var PostList = CoreLists.SystemAccount.Posts

Is this a good way to do this?

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");
        }
    }
}
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.