There are lot of utilities and functions available on the client side SharePoint 2013 API and in this post we will see one of those API called GetUrlKeyValue and SetUrlKeyValue
There are scenarios where we need to get or set the value from or to the querystring of the URL and SharePoint provides an easy way to do this
GetUrlKeyValue:
This small utility function will try to get the value from the querystring of the url. There is no OOTB function to get the querystring value using javascript and this GetUrlKeyValue will help us in getting the value from the querystring
This function has been declared in in init.js. Ensure that init.js is referenced and loaded before you use this function.
Syntax: The syntax for GetUrlKeyValue is
GetUrlKeyValue(keyName, decodeQueryStringValue, url, caseComparison)
keyName: The querystring name for which we need the value
decodeQueryStringValue: Whether you want to decode the value - default value false
url: The url from which we need to retrieve the keyName value
caseComparison: Case sensitivity for keyName. - default value true
Example:
In the above example, we wanted to retrieve "EditPage" querystring value from the current url - window.location.href
window.location.href can be replaced with your own url
The return value from this method is, string representation of querystring value and in this case it is "EditPage"
If the EditPage key doesn't exists in the url, it will return an empty string
SetUrlKeyValue
This is opposite to GetUrlKeyValue. In GetUrlKeyValue, we will getting the value from the querystring and in SetUrlKeyValue, we will be setting the querystring value
The syntax for SetUrlKeyValue is,
SetUrlKeyValue(keyName, keyValue, bEncode, url)
keyName: The querystring name to which we need to set the value
keyValue: The value that needs to be set to the keyName
bEncode: Whether the value needs to be encoded in the url before setting the value
url: Url to which the value needs to be set
Example
There are scenarios where we need to get or set the value from or to the querystring of the URL and SharePoint provides an easy way to do this
GetUrlKeyValue:
This small utility function will try to get the value from the querystring of the url. There is no OOTB function to get the querystring value using javascript and this GetUrlKeyValue will help us in getting the value from the querystring
This function has been declared in in init.js. Ensure that init.js is referenced and loaded before you use this function.
Syntax: The syntax for GetUrlKeyValue is
GetUrlKeyValue(keyName, decodeQueryStringValue, url, caseComparison)
keyName: The querystring name for which we need the value
decodeQueryStringValue: Whether you want to decode the value - default value false
url: The url from which we need to retrieve the keyName value
caseComparison: Case sensitivity for keyName. - default value true
Example:
In the above example, we wanted to retrieve "EditPage" querystring value from the current url - window.location.href
window.location.href can be replaced with your own url
The return value from this method is, string representation of querystring value and in this case it is "EditPage"
If the EditPage key doesn't exists in the url, it will return an empty string
SetUrlKeyValue
This is opposite to GetUrlKeyValue. In GetUrlKeyValue, we will getting the value from the querystring and in SetUrlKeyValue, we will be setting the querystring value
The syntax for SetUrlKeyValue is,
SetUrlKeyValue(keyName, keyValue, bEncode, url)
keyName: The querystring name to which we need to set the value
keyValue: The value that needs to be set to the keyName
bEncode: Whether the value needs to be encoded in the url before setting the value
url: Url to which the value needs to be set
Example