Thursday, May 24, 2012

Enumerate Site collections and sites

public void GetAllSites()
         {

                SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                //using (SPSite site = new SPSite("http://appsweb02:93"))
                {

                    using (SPWeb web = site.OpenWeb())
                    {

                        SPWebApplication webApplication = site.WebApplication; //SPContext.Current.Site.WebApplication;
                        SPSiteCollection siteCollections = webApplication.Sites;

                        string strRoot = string.Empty;
                        foreach (SPSite siteCollection in siteCollections)
                        {
                            strRoot = "http://" + siteCollection.HostName + ":" + siteCollection.Port;

                            if (!strRoot.Equals(siteCollection.Url))
                            {
                                SPWebCollection collWebsite = siteCollection.AllWebs;
                                foreach (SPWeb subSite in collWebsite)
                                {
                                    //Msg.Text += subSite.Title + "<BR>";     // SPEncode.HtmlEncode(subSite.Title) + "<BR>";
                                    //Console.Write(subSite.Url + "-" + subSite.Title + "\n");

                                    listboxSites.Items.Add(new ListItem(subSite.Title, subSite.Url));
                                    subSite.Close();
                                }
                            }
                            siteCollection.Close();

                        }


                    }
                }
            });
       
         }

No comments:

Post a Comment

Followers