SharePoint Developer Center > SharePoint Products and Technologies Forums > SharePoint - Development and Programming (pre-SharePoint 2010) > SPContext.Current.Site - Operation is not valid due to the current state of the object

Locked SPContext.Current.Site - Operation is not valid due to the current state of the object

Locked

  • Monday, February 26, 2007 9:19 PM
     
      Has Code

    Hi,

    WSS 3.0, VS.NET 2005 Sp1, and MSDN walkthru: How to: Create a Web Application in a SharePoint Web Site located at: http://msdn2.microsoft.com/en-us/library/ms368312.aspx

    The error generated says: "Operation is not valid due to the current state of the object". 

    I am able to see the ASPX page at: http://server:43302/_layouts/SPUsersGroups/default.aspx 

    Here's the entire code block.  Any assistance would be appreciated.  I just don't know what the error means or how to resolve it.

    protected void GetSitesAndGroups()

    {

    string bUrl;

    string userList = SPEncode.HtmlEncode(TextBox1.Text) + " is a user in the following webs:<BR>";

    SPSite mySite = SPContext.Current.Site;

    SPWebCollection allWebs = mySite.AllWebs;

    foreach (SPWeb subWeb in allWebs)

    {

    string listGroups = "";

    /*Use AllUsers not Users to ensure you find the user*/

    SPUserCollection allUsers = subWeb.AllUsers;

    foreach (SPUser user in allUsers)

    {

    if (user.LoginName.ToUpper() == TextBox1.Text.ToUpper())

    {

    SPGroupCollection allGroups = user.Groups;

    foreach (SPGroup group in allGroups)

    {

    listGroups += SPEncode.HtmlEncode(group.Name) + " ";

    }

    userList += subWeb.ServerRelativeUrl.ToString() +

    " -- " + listGroups + "<BR>";

    }

    }

    }

    Label1.Text = userList;

    }

    Thanks in advance.

    Paul

Answers

  • Monday, February 26, 2007 11:18 PM
    Moderator
     
     Answered

    There are a number of reasons why I never use the SPControl or SPContext. (by the way - try changing the code to get the current site using SPControl.GetContextSite(context) )

    Anyway - I prefer loading the SPSite object using the current URL. This produces code that can be migrated away to console applications, can be used to load other sites and you as a developer have more control over.

     

    using (SPSite mySite= new SPSite(this.Request.Url.ToString())
    {

    SPWebCollection allWebs = mySite.AllWebs;

    }

All Replies

  • Monday, February 26, 2007 11:18 PM
    Moderator
     
     Answered

    There are a number of reasons why I never use the SPControl or SPContext. (by the way - try changing the code to get the current site using SPControl.GetContextSite(context) )

    Anyway - I prefer loading the SPSite object using the current URL. This produces code that can be migrated away to console applications, can be used to load other sites and you as a developer have more control over.

     

    using (SPSite mySite= new SPSite(this.Request.Url.ToString())
    {

    SPWebCollection allWebs = mySite.AllWebs;

    }

  • Friday, March 09, 2007 8:35 PM
     
     

    Where exactly is the error thrown?  Is it in the actual SPContext.Current.Site call, or later on?  Looking at the code, it appears that you are updating the groups, but it's not in the context of a post, so it could get an exception because you are editing during an HttpGet.  If you know that you want to update during a get, you need to mark the web (through code) via SPWeb.AllowUnsafeUpdates.

    As to the later comment, if at all possible you should use the SPContext.Current.Site call rather than fetching a new site.  By all means abstract it out so that your code is portable to console apps (your example using the HttpContext object, which won't be present in a console app), but constantly refetching the SPWeb / SPSite, etc. will have performance impacts in terms of memory consumption, web front end CPU and back end SQL.

    The SPContext object caches the objects for the life of the Http request, so each time you request it, you'll always get the same object.  Also very useful if you are updating the objects from multiple parts of code.

    Pat.