I'm using the code below to modify Custom List Permission in SharePoint 2010. What I'm trying to do is to modify All Group's Permission Level to Read except for the Owners Group. My code runs fine when I manually deactivate/activate the feature in Site Settings. But an error is displayed in the ULS Log when I deploy it using Visual Studio 2010.

The error I got is

Object reference not set to an instance of an object. StackTrace: StackTrace: at SPProject.Features.ListInstance_Feature.ListInstance_FeatureEventReceiver.b_0() at Microsoft.SharePoint.SPSecurity.<>c_DisplayClass4.b__2() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at SPProject.Features.ListInstance_Feature.ListInstance_FeatureEventReceiver.UpdateList...

Here is the code I am using in FeatureActivated method

SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite site = new SPSite(SPContext.Current.Site.ID))     {
    using (SPWeb web = site.OpenWeb())      {

        SPGroupCollection groupCol = web.Groups;
        SPList list = SPContext.Current.Web.GetList(SPHelper.GetListByURL(GDCLists.TypeDeDemande));
        SPRoleDefinition contributeDef = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
        SPRoleDefinition readDef = web.RoleDefinitions.GetByType(SPRoleType.Reader);
        foreach (SPGroup group in groupCol)         {
            SPRoleAssignment contributeRole = new SPRoleAssignment(group);
            if (!list.HasUniqueRoleAssignments) // required to make role change
            SPRoleAssignment assignmentForGroup = list.RoleAssignments.GetAssignmentByPrincipal(group);

            if (!assignmentForGroup.RoleDefinitionBindings.Contains(readDef))



I've researched through many sites and applied the suggested workaround but still it doesn't resolved the error. Please advise. Thank you.

2 Answers 2


You should not use SPContext for opening the site! In PowerShell or from Visual Studio the SPContext.Current will always be null. This is because it relies on the HttpContext, that will not be initiated when using PowerShell.

If your feature is Site scoped you can use

 var parentSite = properties.Feature.Parent as SPSite;
 using (SPSite site = new SPSite(parentSite.ID));

If your features is web scoped you will get the SPWeb instead

 var parentWeb = properties.Feature.Parent as SPWeb;
 using (SPSite site = new SPSite(parentWeb.Site.ID))     {
 using (SPWeb web = site.OpenWeb())

Make sure to get the list from the web object created instead of SPContext.Current as well

SPList list = web.GetList(SPHelper.GetListByURL(GDCLists.TypeDeDemande));
  • Hi Robert, thank you for your quick response and for sharing this :) The feature is in web scope, I updated it to the above code.
    – Mhe
    Commented Feb 10, 2014 at 9:02
  • Then use my second code example. And make sure to get the list from the web object created instead of SPContext.Current as well Commented Feb 10, 2014 at 9:07

You are getting the reference of the list in the RunWithElevatedPrivileges delegate using SPContext object. You should get the list reference using the elevated web object. Try replacing this code:

 SPList list = SPContext.Current.Web.GetList(SPHelper.GetListByURL(GDCLists.TypeDeDemande));


SPList list = web.GetList(SPHelper.GetListByURL(GDCLists.TypeDeDemande));
  • Hi Nadeem, thank you for your quick response and apologies for replying just now because of work loads. Robert and your recommendation solves the error. :) I've learned something new today. Big thanks. :)
    – Mhe
    Commented Feb 10, 2014 at 14:09
  • Nice to hear that! Commented Feb 10, 2014 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.