SharePoint Error: Unable to Evaluate Expression Because the Code is Optimized

There was a strange error message in one of our custom SharePoint web parts: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. We thought that this error is caused by some optimization from Visual Studio configuration, but then we eliminated that possibility because our Visual Studio project is configured for debugging.

The following code snippet shows the source that caused the error. This code is intended to get list of permission levels of current log-on user:

public static string FindPermissionLevelForCurrentUser()
{
  List pl = new List();

  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    // direct assignment
    SPWeb web = SPContext.Current.Web;
    string userName = web.CurrentUser.LoginName;
    SPUser user = web.Users[userName];
    SPRoleAssignment currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(user);

    foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
    {
      if (pl.Find(s => s.Equals(role.Name)) == null)
        pl.Add(role.Name);
    }

    // if user in group
    SPGroupCollection groupColl = web.Groups;
    foreach (SPGroup group in groupColl)
    {
      SPUser user = group.Users[userName];
      SPRoleAssignment currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(group);

      foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
      {
        if (pl.Find(s => s.Equals(role.Name)) == null)
          pl.Add(role.Name);
      }
    }
  });

  return pl;
}

Look at the highlighted code at line 9, we use SPContext.Current.Web inside SPSecurity.RunWithElevatedPrivileges delegate. This is the cause of the error above. We can’t use SPContext object inside SPSecurity.RunWithElevatedPrivileges delegate.

Solution

The solution for the error Unable to Evaluate Expression Because the Code is Optimized or a native frame is on top of the call stack above is replacing SPContext.Current.Web with new SPWeb object. Look at the following source:

public static string FindPermissionLevelForCurrentUser()
{
  List pl = new List();
  SPSite site = null;
  SPWeb web = null;
  SPWeb webAdmin = null;
  SPSite siteAdmin = null;

  site = new SPSite(SPContext.Current.Site.Url);
  web = site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl);
  userName = web.CurrentUser.LoginName;

  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    // direct assignment
    siteAdmin = new SPSite(SPContext.Current.Site.Url);
    webAdmin = siteAdmin.OpenWeb(SPContext.Current.Web.ServerRelativeUrl);

    SPUser user = webAdmin.Users[userName];
    SPRoleAssignment currentUserRole = webAdmin.RoleAssignments.GetAssignmentByPrincipal(user);

    foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
    {
      if (pl.Find(s => s.Equals(role.Name)) == null)
      pl.Add(role.Name);
    }
    
	// if user in group
    // .. omitting code
  });
  
  return pl;
}

Line 16 and 17 (highlighted) are the change we have to made to replace SPContext.Current.Web object with a new SPWeb object, derived from SPSite object.

Source: SharePoint Error: Unable to Evaluate Expression Because the Code is Optimized

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s