Friday, 17 May 2013

Get Full Site and Page URL in sharepoint using javascript or client object model

In many cases we want the page to redirect to new page in sharepoint using javascript.

So, the first thing which we need to understand is to get site collection URL like (http://www.contoso.com)

Then after we can get the current page relative URL.

The below code gives you the details how to achieve this.

var SiteCol_URL = window.location.protocol + '//' + window.location.host;

var currentcontext;

This will give you the site collection URL like (http://www.contoso.com)

After then if you want to get current page URL using client object model , here is the code

function GetCurrentPageURL() {


    currentcontext = new SP.ClientContext.get_current();


    currentcontext.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), 

Function.createDelegate(this, this.oncListQueryFailed));}


function OnSuccess() {   var PageURL= SiteCol_URL + currentcontext.get_url(); }


 currentcontext.get_url()  contains that page relative URL like (/sitespages/page1.aspx)


So, this way u can get full page URL and also site collection URL