none
UserProfile: "Operation is not valid due to the current state of the object" RRS feed

  • Question

  • I want to create a User Profile as part of a registration process.  Simple enough, right?  Whenever I call UserExists(), GetUserProfile() or CreateUserProfile() however I get the following error:
    
      "Operation is not valid due to the current state of the object".
    
    I have verified that I have a valid connection; I am able to call the Count property, for instance, without problem.  
    
    Here's the code - it's pretty straight forward.
    
        SPSecurity.RunWithElevatedPrivileges (
          delegate() {
            using (SPSite site = new SPSite("
    http://sharepoint/ssp/admin/")) {
              site.AllowUnsafeUpdates = true;
              ServerContext context = ServerContext.GetContext(site);
              UserProfileManager profileManager = new UserProfileManager(context);
              Response.Write(profileManager.Count);
              Response.Write(profileManager.UserExists("sharepoint\\username"));
              }
            }
          );
    And the (condensed) stack trace:
    SPWebEnsureSPControl(HttpContext context) +138
    GetContextWeb(HttpContext context) +25
    UserProfiles.UserProfileGlobal.GetCurrentUserName() +228
    UserProfileManager.get_strCurrentAccountName() +64
    UserProfileManager.UserExists(String strAccountName) +217
    Note: If I change the impersonation method outlined in the following article (which was referenced as a solution in this forum):
    http://dotnetjunkies.com/WebLog/victorv/archive/category/2032.aspx
    I get the following behavior:
    Count: "403 UNAUTHORIZED"
    Get/Create UserProfile: Redirect to login page (likely due to a 403)
    This is peculiar because a) I've tried impersonating using an administrator account that is also in the WSS_ADMIN_WPG group, and b) this is the same account that should be used (the App Pool account) using RunWithElevatedPrivileges (which can execute Count properly).
    Thoughts?
    [Note: Cross-posting unanswered questions from microsoft.sharepoint.development_and_programming].

     

    Sunday, November 11, 2007 8:17 PM

All replies

  • I'm experiencing the same problem...
     Have you solved it?

    Gustavo Gil
    Monday, October 13, 2008 7:51 PM
  • It's likely fixed with: http://support.microsoft.com/kb/952294.


    Posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, October 13, 2008 8:00 PM
  • I had this error because I was running code in an ASPX page outside of SharePoint  using the ASP.NET Development Server with it's default application pool and so operations inside the "SPSecurity.RunWithElevatedPrivileges" fail because the App Pool identity is invalid/not trusted.

    If you get the error, one thing to check is taht you're running under a SharePoint App Pool/valid identity.
    Wednesday, March 11, 2009 4:53 AM
  • I got this while trying to delete a "SEALED" field from a sharepoint field collection using the sharepoint API, unsealing it first allowed me to continue.
    Thursday, October 1, 2009 10:21 AM
  • Hello,
              you have used using statement for SPSite object so when ever code goes out of scope SPSite object gets dispose.. So next time when you try to call it again SPSite object is null.

    Check whether SPSite object is not null at all level. 


    Regards
    Milan
    Thursday, October 1, 2009 12:26 PM
  • How do you execute this code? Like, in event handler or in HttpModule etc..
    Thursday, October 1, 2009 3:52 PM
  • try this right before the Response.Write line


    if  (HttpContext.Current !=  null )  
    {  
        if  (HttpContext.Current.Items[ "HttpHandlerSPWeb" ] ==  null )  
            HttpContext.Current.Items["HttpHandlerSPWeb" ] = site.RootWeb;  
        if  (HttpContext.Current.Items[ "Microsoft.Office.ServerContext" ] ==  null )  
            HttpContext.Current.Items["Microsoft.Office.ServerContext" ] = serverContext;  


    It seems that some of the properties of HttpContext used by MOSS in static classes (i.e. ServerContext) are not initialised properly. The code makes it possible for MOSS to retrieve “current web” (SPContext.Current.Web) and “current servercontext”.


    Taken from http://blog.ozo.dk/?p=60
    Monday, December 14, 2009 8:30 PM
  • I also got the error

    "Operation is not valid due to the current state of the object."

    when calling the function UserProfileManager.UserExists().

    The above workaround of setting the HttpContext properties "HttpHandlerSPWeb" and "Microsoft.Office.ServerContext" worked for me very fine.

    Thanks.

    Tuesday, June 14, 2011 9:45 AM
  • Took me two weeks to find this - this actually fixed it! Thanks! In addition to the above, I simply had to add AllowUnsafeUpdates and it all worked.

    David Sterling/CEO - SICG

    Tuesday, September 24, 2019 11:57 AM