How to get sharepoint site URL programmatically (CSOM and JSOM)

In this SharePoint tutorial, we will discuss how to get SharePoint site URL programmatically using javascript object model (jsom) and client side object model (CSOM). The code we can use to get sharepoint site URL in SharePoint Online, SharePoint 2013, SharePoint 2016 and SharePoint 2019.

We will also discuss:

  • get sites URL SharePoint from SharePoint site title using CSOM
  • sharepoint site URL using JSOM
  • get SharePoint site URL using JavaScript
  • How to get server relative URL in SharePoint 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>

By using the above code, we can get sharepoint site url programmatically using JavaScript object model.

SharePoint get site url using CSOM

SharePoint get site url using CSOM?

Now, we will see how to get SharePoint 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 SharePoint site title is “Bhawana” then it will return the URL. In the method, we are loading the Title and URL property of the web. Before that, we using the SharePoint clientcontext.

Below is the full csom code to get sites URL SharePoint 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 SharePoint site URL like below:

get sharepoint site url csom
get sharepoint site url

Retrieve exact SharePoint site URL from SharePoint ServerRelativeUrl

Let us see an serverrelativeurl sharepoint example in c#.

Now, we will see how to get the exact SharePoint 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 (First we are using the SharePoint clientcontext)

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;
}
}

This is how we can get SharePoint site URL from SharePoint serverrelativeurl.

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 get the exact site URL from the ServerRelativeUrl in SharePoint Online? And how to get sharepoint site URL based on the site title using (csom) .Net managed object model code in SharePoint Online.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>