How to get all site collections in a web application

30.2k views Asked by At

I want to list all site collections under my web application; the purpose is to list all mysites created by users.

Like that:

/members/sites/sqladmin
/members/sites/sqluser
/members/sites/sqluser1
/members/sites/sqluser2
/members/sites/user1
4

There are 4 answers

0
Telan Niranga On

Use below code to programmatically get the all site collections of the web application. url parametter = web application url.

        public DataTable GetAllSiteCollections(string url)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("URL");
            dt.Columns.Add("Name");
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(url));
                foreach (SPSite site in webApplication.Sites)
                {
                    dt.Rows.Add(new object[] { site.Url, site.Url });

                }

            });

            return dt;
        }
0
Ondrej Tucny On

For the purpose of accessing all site collections within a given web application there's, quite naturally, the SPWebApplication.Sites property. To get access to a particular web application you can use a code like this:

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://my-web-app-url:80/"));

(see MSDN here as well).

0
NLV On

Use SPWebApplication.Sites property for that.

0
Merin Nakarmi On

On the Central Administration home page, click Application Management.

On the Application Management page, in the Site Collections section, click View all site collections.

The Site Collection List page lists all the site collections in the Web application.