Skip to content

Instantly share code, notes, and snippets.

@kitmenke
Last active September 24, 2015 02:48
Show Gist options
  • Save kitmenke/9948277 to your computer and use it in GitHub Desktop.
Save kitmenke/9948277 to your computer and use it in GitHub Desktop.
Auto-populate a SharePoint field using a URL Parameter
<script src="/kitsite/Files/jquery-1.11.0.min.js"></script>
<script src="/kitsite/Files/sputility.min.js"></script>
<script>
// url parsing from http://stackoverflow.com/a/2880929/98933
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search;
// fix for sharepoint 2013 URLs which use the hash
if (query === '') {
query = window.location.hash.substring(window.location.hash.indexOf('?'));
}
query = query.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
// wait for the window to load
$(window).load(function () {
try {
var urlValue = urlParams['projectID'];
SPUtility.GetSPField('Project ID').SetValue(urlValue).MakeReadOnly();
} catch (ex) {
alert(ex.toString());
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment