Pages

Sunday, January 24, 2016

Get Root Site URL from a Sub Site Programmatically

You can access the URL of the root site of your sub site using Uri property.

private string GetRootSiteUrl()
{
   string urlValue = "";
   using (SPSite sPSite = new SPSite(site.ID))
   {
      using (SPWeb sPWeb = sPSite.OpenWeb(web.ID))
      {
         Uri url = new Uri(sPWeb.Url);
         urlValue = url.GetLeftPart(UriPartial.Authority);

         return urlValue;
      }
   }
}

No comments:

Post a Comment