Simple REST Personalization

This is neither the time nor the place to get into the functionality of the new (ish) REST API. That will follow. For now, this is just some simple JavaScript that accesses the API, returns the full name for the selected user, and outputs a pretty "Hi Bob" to the page.

Add a simple placeholder to the page, something like this does the trick

<h2>&nbsp;</h2>

And then add this to the bottom of the content page (Content_HTML iPart)

<script>
	try {
		var j = JSON.parse(jQuery("#__ClientContext").val());
		if (j.selectedPartyId != "") {
			jQuery.ajax("/iMIS0/api/party/" + j.selectedPartyId, {
				type : "get", 
				contentType: "application/json", 
				async: true,
				headers: {
					"RequestVerificationToken": jQuery("#__RequestVerificationToken").val()
				}, 
				success: function(data) {
					var n = data.Name;
					if (n != "GUEST") {
						jQuery("h2").html("Welcome Back, " + n + "!");
					}
				}
			})
		}
	} catch(e) {}
</script>

If all goes well, the page should look something like the below. Of course, for this to work the member has to be logged in but that's sort of a given.


back
Categories
2459 Bridge Road
Oakville, ON
L6L 2G9
Canada
jake@k2andyou.com
support@k2andyou.zendesk.com
Office: (905) 901-3625
Mobile: (289) 795-3538