Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
SPContext.Current.Site.Sites object throws an Argument Exception.

Answered SPContext.Current.Site.Sites object throws an Argument Exception.

  • Friday, September 14, 2012 6:31 AM
     
     

    Hi,

    I have a requirement.

    I need to traverse through all the site and subsites URLs under a particular site collection and Put all the URLs in a List in another site collection  prgramatically. Please help. Actually I used the below code snippet to achieve but faced an issue like:  SPWebCollection sites = siteCollection.AllWebs returns Argument exception.

    SPList list = SPContext.Current.Web.Lists["RegisteredIPipeSites"];

                foreach (SPSite siteCollection in siteCollections)
                {
                    SPWebCollection sites = siteCollection.AllWebs;
                    foreach (SPWeb site in sites)
                    {
                        SPListItem listitem = list.AddItem();
                        listitem["SiteCollectionURL"] = siteCollection.Url;
                        listitem["SiteURL"] = site.Url;
                        listitem.Update();                   
                    }               
                } 


    Knowledge is power.

All Replies

  • Friday, September 14, 2012 8:55 AM
     
      Has Code

    Have you tried using

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
    

    SPList list = SPContext.Current.Web.Lists["RegisteredIPipeSites"];

                foreach (SPSite siteCollection in siteCollections)
                {
                    SPWebCollection sites = siteCollection.AllWebs;
                    foreach (SPWeb site in sites)
                    {
                        SPListItem listitem = list.AddItem();
                        listitem["SiteCollectionURL"] = siteCollection.Url;
                        listitem["SiteURL"] = site.Url;
                        listitem.Update();                    
                    }                
                } 

    });


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.



  • Friday, September 14, 2012 9:50 AM
     
     

    Still no luck. I tried an alternate way as below. I am able to see my all site collections in side "this.WebApplication.Sites" object but it is traversing only one site collection. System is not traversing over all the site collection. Any idea?

     SPSiteCollection _sites = this.WebApplication.Sites;

                foreach (SPSite site in this.WebApplication.Sites)
                {
                    try
                    {
                        SPWebCollection subSites = site.AllWebs;

                        for (int i = 0; i < subSites.Count; i++)
                        {
                            SPListCollection lists = subSites[i].Lists;

                            for (int j = 0; j < lists.Count; j++)
                            {
                                //SPListItem listitem = list.AddItem();
                                //listitem["SiteCollectionURL"] = "";
                                //listitem["SiteURL"] = subSites[i].Title.ToString();
                                //listitem.Update(); 
                            }
                        }
                    }
                    catch (Exception e)
                    {
                    }
                    finally
                    {
                        site.Dispose();
                    }
                }


    Knowledge is power.

  • Friday, September 14, 2012 10:34 AM
    Moderator
     
     Answered Has Code

    Hi,

    To return the collection of site collections in a SharePoint Web application, use below code:

    SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
    SPSiteCollection siteCollections = webApplication.Sites;
    
    foreach (SPSite siteCollection in siteCollections)
    {
        /////
    }

    Check this MSDN to get site/subsite/list/: http://msdn.microsoft.com/en-us/library/ms462161.aspx

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

  • Friday, September 14, 2012 10:56 AM
     
     Answered

    Hi Hemendra,

    My issue is different.I tried your mentioned code still same issue I am facing.I don't see my code  and your provided code are  of different logic.

    My issue is: System traverse only one site collection even thoush the collection shows all the site collection.


    Knowledge is power.

  • Friday, September 14, 2012 11:03 AM
     
     
    can you please provide the error screen shot

    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • Friday, September 14, 2012 11:21 AM
    Moderator
     
     Answered

    >System traverse only one site collection even thoush the collection shows all the site collection

    What do you mean by site collection show all site collection? Did you verify the site collection in central admin for web application? Go to CA-->Manage web application -->and check how many site collections are existing for your web app.

    Bit confusion here so kindly correct us


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"