Monday, May 9, 2016

Operation is not valid due to current state of the object -- Elevated Privileges


Exception

ExceptionType=System.InvalidOperationExceptionJSON
Message=Operation is not valid due to the current state of the object.

Cause: 

I tried to call SPUtility.SendEmail within the RunWithElevatedPrivileges delegate.

Comments

If i try to run this method out of Elevated delegate, it works fine. All the other things that are working fine in Elevated Privileges (except send email) are,

  1. SPUtility.GetPrincipalsInGroup
  2. spWeb.EnsureUser

Solution:

I found two solution/Work arounds ,


  1. Remove elevation (Do not know why this option works).
  2. Set HttpContext.Current to null. Following is the detail discussion over why this option works.

https://johnatjornata.wordpress.com/2012/01/23/runwithelevatedprivileges-operation-is-not-valid-due-to-the-current-state-of-the-object-error-explained/



No comments:

Post a Comment