How to retrieve root web site url from site collection url in SharePoint online using .Net managed object model code?

csom retrieve root web site url from site collection url in SharePoint online

Here we will discuss how we can retrieve root website URL and title from site collection URL using.Net managed object model (csom) code in SharePoint online. We will do this inside a console application using.Net managed object model code (CSOM).

Here we need to load context.Site.RootsWeb, then we can retrieve the RootsWeb properties like Url or Title etc.

SharePoint deveopment training course

Also, read:
– How to call web api from visual studio 2015 workflow httpsend activitity in SharePoint online?

– How to update app registration in SharePoint online site?

– How to add lookup column programmatically using csom in SharePoint online Office 365?

Below is the full code:

public static string GetRootweb()
{

string rootweb = string.Empty;

using (ClientContext context = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/subsite1”))

{

context.AuthenticationMode = ClientAuthenticationMode.Default;

context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());

context.Load(context.Site.RootWeb, w => w.Url, w => w.Title);

context.ExecuteQuery();

rootweb =”Root Web URL:: “+ context.Site.RootWeb.Url +” Title:: “+ context.Site.RootWeb.Title;

}

return rootweb;

}

Once you run, you can see the rootweb url and title like below:

csom retrieve root web site url from site collection url in SharePoint online
csom retrieve root web site url from site collection url in SharePoint online

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →