Tuesday, February 19, 2013

SharePoint 2013 StandardTokens with IntelliSense

Update: 21 february 2013. Fixed a problem with the json not being valid in Internet Explorer 8. 

Problem:  The standard function getQueryStringParameter used to get the querystring parameters in SharePoint Apps is a bit poor. It does not offer any help on IntelliSense and will spilt the url every time you request a value, making it a bit slow function to use.

I love IntelliSense, especially when working with JavaScript. So I came up with this:

Solution:

window.SF = window.SF || {};

 

window.SF.CreateQueryString = function () {

    // Added defined properties for IntelliSense.

    var q = {

        SPAppWebUrl: '',

        SPClientTag: '',

        SPHostLogoUrl: '',

        SPHostTitle: '',

        SPHostUrl: '',

        SPItemId: '',

        SPItemUrl: '',

        SPLanguage: '',

        SPListId: '',

        SPProductNumber: '',

        SPRecurrenceId: '',

        SPRemoteAppUrl: '',

        SPSite: '',

        SPSiteCollection: '',

        SPSiteUrl: '',

        SPSource: ''

    };

    var hash;

    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for (var i = 0; i < hashes.length; i++) {

        hash = hashes[i].split('=');

        var val = decodeURIComponent(hash[1].replace(/\+/g, ' '));

        q[hash[0]] = val;

        q[hash[0].toLowerCase()] = val;

    }

 

    return q;

};

 

window.SF.Response = {

    QueryString: SF.CreateQueryString()

};

Usage:
Start with adding the javascript file with the code to the _references.js file, in order to get the IntelliSense going.

clip_image001

Now accessing the Standardtokens is like an easy game.

If you want to access other parameters in the url, there are sadly no IntelliSense, but the parameters are still available in a case mode and lower case mode. This is very handy when creating custom properties for an App Part and accessing those properties from the App Part iframed page.

<Content Type="html" Src="~appWebUrl/Pages/SiteTreeViewPage.aspx?{StandardTokens}&amp;showrootweb=_showrootweb_" />

Makes the “showrootweb” parameter available in the QueryString.

var showRootWeb = SF.Response.QueryString.showrootweb;

It does not matter what case the parameter have in the url, because it will always be available in lowercase and its normal form.

1 comment:

Unknown said...


WoW, that sure is pretty cool,I hope others find it helpful and educational as i did Microsoft Sharepoint 2013