SharePoint JavaScript OOB Utilities URL

SharePoint provides Out of Box JavaScript libraries to manipulate URL property.

GetUrlKeyValue(KeyName, bNoDecode, url, bCaseInsensitive)
Gets the key value from url. The second parameter is a Booleans indicating whether the value needs decoding and the last parameter indicates whether key is case sensitive

SetUrlKeyValue(keyName, keyValue, bEncode, url)
Set the value of key in the Url. The third parameter indicates whether the value needs to be encoded

The two functions below are useful before saving a file to detect any illegal characters in url and file name as both have different sets of illegal characters.
http://”, question mark, hash, ssemicolon and ampersand have to be stripped before passing to these functions otherwise they will be interpreted as illegal characters.

//check URL
IndexOfIllegalCharInUrlPath(string)
//check file name
IndexOfIllegalCharInUrlLeafName(string)

An example of the use of the functions are below

var currentUrl = _spPageContextInfo.webAbsoluteUrl.replace("http://", "") + _spPageContextInfo.serverRequestPath;

var illChar = IndexOfIllegalCharInUrlPath(currentUrl);

indexIllegalChar.JPG

ReplaceUrlTokens(urlWithTokens,ctx)
Allows to replace all instances of one of six SP specific tokens in a URL
{ItemId} {ItemUrl} {SiteUrl} {ListId} {Source} {ListUrlDir}

The code below uses the function to obtain listId , SiteUrl and ListUrlDir which is the rootfolder Url of the list.


var tokenizedUrl = "http://www.test?list={ListId}&SiteUrl={SiteUrl}&ListDir={ListUrlDir}"
var url = ReplaceUrlTokens(tokenizedUrl,ctx);
console.log(url)
var listId = GetUrlKeyValue ( "List",false, url, true);
console.log("ListId:" + listId)
var siteUrl = GetUrlKeyValue ( "SiteUrl",false, url, true);
console.log("siteUrl" + siteUrl)
var ListUrlDir = GetUrlKeyValue ( "ListUrlDir",false, url, true);
console.log("ListUrlDir:" + ListUrlDir)

SP.Utilities.UrlBuilder.urlCombine(string1, string2)
Combines the two string parameters without redundant slashes between them. However only works if second string does not begin with a slash.

SP.Utilities.UrlBuilder.removeQueryString(url,key)
Removes key value parameter from url.

var urllink = "http://test.com?param1=200&param2=900"
console.log(SP.Utilities.UrlBuilder.removeQueryString(urlLink,param1));

SP.Utilities.Utility.getLayoutsPageUrl(pageFileName)
Get relative URL for specific layout page for current context
It is useful when writing in a mixed 2010, 2013 mode where some sites have been upgraded

alert(SP.Utilities.Utility.getLayoutsPageUrl("SiteSettings.aspx"))

SP.Utilities.UrlBuilder.navigateTo(url)
It takes the browser to specified url. It is useful when the page url is loaded into a SP modal dialog then the new page that gets loaded will remain inside that dialog as well.

Copy the code into a script editor web part to test.

<a onclick="SP.Utilities.UrlBuilder.navigateTo('http://www.google.com')">Navigate in Dialog</a>
<a href='http://www.google.com')">Navigate without Dialog</a>

AddKeyValueQueryString(key,value)
var testUrl = SP.Utilities.Utility.getLayoutsPageUrl(“test.aspx”)
var urlBuilder = SP.Utilities.UrlBuilder(testUrl);
urlBuilder.AddKeyValueQueryString(“param”,”112″);
console.log( urlBuilder.get_url());

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s