Kwizcom Forms

How to retrieve the exact site url from the ServerRelativeUrl in SharePoint online?

In this post we will discuss how we can retrieve the exact site url from the ServerRelativeUrl in SharePoint online using .Net managed object model code (C#.Net). Here we are doing in a console application and we are trying to connect to SharePoint online (Download PDF for FREE) site.

Here my requirment is if the site url is:

SharePoint deveopment training course

It should return me onlu “Bhwana”

If the site url is

It should return me onlu “SubSite1”

Here in the GetSiteURL function we are taking the context and spliting the ServerRelativeUrl with “/” and returning the last element from the array.

Below is the full code:

using (ClientContext context = new ClientContext(URL))
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
context.Load(context.Web, w => w.ServerRelativeUrl);
context.Load(context.Web, w => w.Title);
string siteTitle = GetSiteURL(context);

private static string GetSiteURL(ClientContext context)
string siteTitle;
string[] strArray = context.Web.ServerRelativeUrl.Split(‘/’);
int j = strArray.Length;
siteTitle = strArray[j – 1].ToString();
return siteTitle;

Read some sharepoint online tutorials:

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)


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 →