locked
How to programmatically get URL of top level site collection ? RRS feed

  • Question

  • Hello,

    I am having site hierarchy as below

    Web Application -> Site Collection1(http://servername:portnumber/) , Site Collection 2 (http://servername:portnumber/sites/Sitename1)..

    my custom control deployed at web application level and i need to fetch programmatically top level site collection url for other site collection.

    can anyone please let me know how to accomplish this , so i can get top level site collection url (http://servername:portnumber/) to other site collection ?

    I am egarly waiting for your reply !!

    Thanks,


    dipti chhatrapati
    Friday, December 16, 2011 11:29 AM

Answers

  • Hi,

    Try the following snippet to get the first (Top-Level) Site Collection from the WebApplication.

            SPWebApplication webapp = SPContext.Current.Site.WebApplication;
            SPSite site = webapp.Sites.First<SPSite>();
            Response.Write(site.Url);

     


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me
    Friday, December 16, 2011 12:08 PM

All replies

  • Hi Dipti,

    Try this

                   
          SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
          SPSiteCollection siteCollections = webApplication.Sites;
          foreach (SPSite siteCollection in siteCollections)
          {
              //use this "siteCollection.Url" to get top level site collection UL
          }

     


    Satyam MCITP, MCPD
    Friday, December 16, 2011 11:53 AM
  • Hi dipti,

    You can use the solution provided by satyam in case you want to know the url of current site on which user is logged in programitically kindly use

    SPContext.Current.Site.Url


    Best Regards, Ashutosh | SharePoint World
    Friday, December 16, 2011 12:08 PM
  • Hi,

    Try the following snippet to get the first (Top-Level) Site Collection from the WebApplication.

            SPWebApplication webapp = SPContext.Current.Site.WebApplication;
            SPSite site = webapp.Sites.First<SPSite>();
            Response.Write(site.Url);

     


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me
    Friday, December 16, 2011 12:08 PM
  • Thank you so much to all of you for your time !!! :) i got expert answers !!! :) thanks again !!
    dipti chhatrapati
    Monday, December 19, 2011 6:05 AM
  • Hi, it works fine for me!

    Thanks.

    Ricardo

    Thursday, March 22, 2012 1:12 PM