Get Site URL using csom in SharePoint 2013 Online

This SharePoint tutorial, we will discuss how to get site URL using csom and jsom in SharePoint 2013/2016 or SharePoint Online.

We will also discuss:

  • How to retrieve site URL based on site title using csom in SharePoint Online?
  • How to retrieve the exact site URL from the ServerRelativeUrl in SharePoint Online using csom?

Get SharePoint site URL using JSOM

Now, we will see how to get SharePoint site URL using JSOM (JavaScript object model) in SharePoint Online or SharePoint 2013/2016.

Below JSOM code you can add inside a script editor web part or content editor web part in a SharePoint web part page.

<script language="javascript" type="text/javascript">
var client context;
var website;
// Make sure the SharePoint script file 'sp.js' is loaded before your
code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getWebSiteURL);
// Create an instance of the current context.
function getWebSiteURL() {
clientContext = SP.ClientContext.get_current();
website = clientContext.get_web();
clientContext.load(website);
clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
function onRequestSucceeded()
{
alert(website.get_url());
}
function onRequestFailed(sender, args)
{
alert('Error: ' + args.get_message());
}
</script>

Retrieve site URL based on site title using csom in SharePoint Online

Now, we will see how to retrieve site URL based on site name using .Net managed object model (csom) code in SharePoint Online Office 365. Here we will work in a console application and we will try to connect to the SharePoint Online site.

Here in this example, we are checking with one title as “Bhawana” and if the site title is “Bhawana” then it will return the URL. In the method, we are loading the Title and URL property of the web.

Below is the full csom code to get site URL based on site title using csom in SharePoint Online.

private static string GetSiteURLFromSiteTitle()
{
string siteURL = string.Empty;
string siteTitle = "Bhawana";
using (ClientContext clientContext = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
Web web = clientContext.Web;
clientContext.Load(web, w => w.Title, w => w.Url);
clientContext.ExecuteQuery();
if (web.Title == siteTitle)
{
siteURL = web.Url;
}
}
return siteURL;
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}

catch
{
throw;
}
}

Once you run the code, you can see it will return the URL like below:

get sharepoint site url csom

Retrieve exact site URL from the ServerRelativeUrl in SharePoint online using CSOM

Now, we will see how to 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 the SharePoint Online site.

Here my requirement is if the site URL is: https://onlysharepoint2013.sharepoint.com/sites/Bhawana

It should return me only “Bhwana”

If the site URL is https://onlysharepoint2013.sharepoint.com/sites/Bhawana/SubSite1

It should return me only “SubSite1”

Here in the GetSiteURL function we are taking the context and splitting 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);
context.ExecuteQuery();
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;
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}

catch
{
throw;
}
}

You may like following SharePoint csom tutorials:

This SharePoint tutorial, we learned how to get Site URL using csom in SharePoint 2013 Online. We also saw how to retrieve the exact site URL from the ServerRelativeUrl in SharePoint Online? And how to retrieve site URL based on site title using (csom) .Net managed object model code in SharePoint Online.

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

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"

>