Skip to main content
replaced http://sharepoint.stackexchange.com/ with https://sharepoint.stackexchange.com/
Source Link

The answer of auhcms (http://sharepoint.stackexchange.com/a/39606/41946https://sharepoint.stackexchange.com/a/39606/41946) is correct, but incomplete.

As it seemed strange to me that the root site collection would always be the first element in the SPWebApplication.Sites collection I decided to do a quick test.

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first<br/>
$webapp = $site.WebApplication<br/>
$webapp.Sites[0].Url<br/>

http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

public SPSite GetRootSiteCollection(SPWebApplication webApp)
{
    //no site collections created on this webApp
    if (webApp.Sites.Count == 0)
       return null;

    SPSite site = webApp.Sites[0];
    
    //only return the site collection if it's the root site collection.
    if (site.ServerRelativeUrl.Equals("/"))
        return site;
    
    return null;
}

(you should dispose the SPSite object when your are done with it)

The answer of auhcms (http://sharepoint.stackexchange.com/a/39606/41946) is correct, but incomplete.

As it seemed strange to me that the root site collection would always be the first element in the SPWebApplication.Sites collection I decided to do a quick test.

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first<br/>
$webapp = $site.WebApplication<br/>
$webapp.Sites[0].Url<br/>

http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

public SPSite GetRootSiteCollection(SPWebApplication webApp)
{
    //no site collections created on this webApp
    if (webApp.Sites.Count == 0)
       return null;

    SPSite site = webApp.Sites[0];
    
    //only return the site collection if it's the root site collection.
    if (site.ServerRelativeUrl.Equals("/"))
        return site;
    
    return null;
}

(you should dispose the SPSite object when your are done with it)

The answer of auhcms (https://sharepoint.stackexchange.com/a/39606/41946) is correct, but incomplete.

As it seemed strange to me that the root site collection would always be the first element in the SPWebApplication.Sites collection I decided to do a quick test.

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first<br/>
$webapp = $site.WebApplication<br/>
$webapp.Sites[0].Url<br/>

http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

public SPSite GetRootSiteCollection(SPWebApplication webApp)
{
    //no site collections created on this webApp
    if (webApp.Sites.Count == 0)
       return null;

    SPSite site = webApp.Sites[0];
    
    //only return the site collection if it's the root site collection.
    if (site.ServerRelativeUrl.Equals("/"))
        return site;
    
    return null;
}

(you should dispose the SPSite object when your are done with it)

Formatting
Source Link
wjervis
  • 5.7k
  • 25
  • 45

I preformed the following steps:

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path
  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first<br/>
$webapp = $site.WebApplication<br/>
$webapp.Sites[0].Url<br/>

$site = Get-SPSite http://sp2013:32000/sites/first
$webapp = $site.WebApplication
$webapp.Sites[0].Url
http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first
$webapp = $site.WebApplication
$webapp.Sites[0].Url
http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first<br/>
$webapp = $site.WebApplication<br/>
$webapp.Sites[0].Url<br/>

http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

Source Link

The answer of auhcms (http://sharepoint.stackexchange.com/a/39606/41946) is correct, but incomplete.

As it seemed strange to me that the root site collection would always be the first element in the SPWebApplication.Sites collection I decided to do a quick test.

I preformed the following steps:

  1. Created a new WebApplication
  2. Created a Site collection under the /sites managed path
  3. Created a second site collection under the root managed path

When I then checked the first element of the SPWebApplication.Sites collection, it turned out to be in fact the root site collection.

(when initializing the SPSiteCollection collection, SharePoint call the proc_getSiteNames stored procedures. This procedures retrieves the site collections from the database and orders them by 'path'... so the root, if it exists, will be first)

However, it is not required to have a site collection a the root path of the WebApplication. When there is no site collection at the root path, SPWebApplication.Sites[0] will return another existing site collection.

$site = Get-SPSite http://sp2013:32000/sites/first
$webapp = $site.WebApplication
$webapp.Sites[0].Url
http://sp2013:32000/sites/first

So when an site collection exists at the root path of the webapplication, you can retrieve it using SPWebApplication.Site[0].

You can however not be sure that the site collection at SPWebApplication.Site[0] will always be at the root path of the WebApplication.

To validate if the site collection is as the root, you could check the ServerRelativeUrl property of the SPSite instance:

public SPSite GetRootSiteCollection(SPWebApplication webApp)
{
    //no site collections created on this webApp
    if (webApp.Sites.Count == 0)
       return null;

    SPSite site = webApp.Sites[0];
    
    //only return the site collection if it's the root site collection.
    if (site.ServerRelativeUrl.Equals("/"))
        return site;
    
    return null;
}

(you should dispose the SPSite object when your are done with it)