Skip to content
Franco Mussopower pages
Room with purple lighting and white wall
Photograph · Juliana Tanchak / Unsplash
Guide No.003
TutorialPortals Web API

How to upload images using Power Pages’ Portal Web API – Part 1: Upload a profile photo

While this technique still works and is still valid for custom templates, Power Pages forms and lists now support image columns

Level
03CustomiseStage 3 of 4
Read
9 min
Includes
6 snippets
Written
Jan 2021

Introduction

Here are the main points I learned along my twisty-turny R&D journey trying to upload images using the Portal Web API:

In this first example, we’ll add a simple form to a Web Template that allows users to upload a profile photo. Using the Portal Web API, we’ll save this to the entityimage field on the portal user’s contact record.

For the impatient readers (full disclosure, that’s usually me), you can jump straight to my sample code for uploading profile photos

Here’s how I arrived at that code

There are 4 pieces to this puzzle, most of which require very little code:

  1. A form allowing the user to select a single image
  2. jQuery to get the contents of the selected image in base64 format
    (I say with confidence but have no clue what that actually means… it just works so no arguing please)
  3. Authenticate with the Portal Web API
    (I followed Nick Doelman’s sound advice and stored the code for this in a separate web template for easy reuse. See his excellent tutorial here: Test Driving Power Pages’ Portal Web API – ReadyXRM)
  4. Use the Portal Web API to update the logged in portal user’s contact record, saving the selected image to the entityimage field

Step 1 – Add a form allowing the user to select a single image

The form contains a single input that accepts one image file, and an anchor tag (link) which we’ll use to trigger the upload functionality

FetchXML
	<form id="profileUploadForm">
		<input id="profilePhoto" accept="image/jpg,image/jpeg,image/gif,image/tif,image/tiff,image/png" type="file">
		<a class="btn btn-default" id="submitPhoto" href="#">Upload</a>
	</form>

Step 2 – Add jQuery to get the contents of the selected file (in base64 format)

This is where I struggled the first time around. Here’s what I hadn’t understood…. at this stage, although there is a file selected, the contents of the image aren’t yet available in the browser and nothing has been uploaded. We need to tell jQuery to read the file and get its content in base64 format before we can send anything with the API

I’ve commented this throughout to try and help make sense of what each piece does…

JavaScript
// When the submitPhoto links is clicked
$( '#submitPhoto' ).click(function(e) {
	// Run the getImageFileContents function
	getImageFileContents();
	// And don't perform the default behaviour for the link e.g. navigation
	e.preventDefault();				
});
 
function getImageFileContents() {
	// Get the first (and only) file selected in the profilePhoto field
	var file = document.getElementById('profilePhoto').files[0];
	// If the user has selected a file
    if (file) {
        // Read the file
        var reader = new FileReader();
		reader.readAsDataURL(file);
        reader.onload = function(e) {
            // The browser has now completed reading the file, do something with it...
			var profilePhotoContents = e.target.result;
			// Split the string at the first comma (removing the base64 prefix)
			profilePhotoContents = profilePhotoContents.substring(profilePhotoContents.indexOf(',')+1);
			// Run the function to upload to the Portal Web API, passing the file's contents as an input
			uploadPhotoAPI(profilePhotoContents);
        };
    }
}

Step 3 – Authenticate with the Portal Web API

Thanks to following Nick’s advice, this is a matter of a single line of liquid, including the contents of the Portal Web API Wrapper web template:

Liquid
{% include 'Portal Web API Wrapper' %}

If you haven’t already saved the wrapper function for easy reuse each time you need to authenticate the logged in portal user with the Portal Web API, here are the steps:

Step 4 – Use the Portal Web API to update the logged in portal user’s contact record, saving the selected image to the entityimage field

JavaScript
function uploadPhotoAPI(profilePhotoContents) {
 
webapi.safeAjax({
    type: "PATCH",
    url: "/_api/contacts({{ user.id }})",
    contentType:"application/json",
    data: JSON.stringify({"entityimage": profilePhotoContents}),
    success: function(res, status, xhr) {
        $('#submitArtwork').html('&amp;lt;span class="glyphicon glyphicon-ok"&amp;gt;&amp;lt;/span&amp;gt;');
        var entityID = xhr.getResponseHeader("entityid");
        console.log("entityID: " + xhr.getResponseHeader("entityid"));
    }
});
 
}

Sample web template for uploading profile photos using the Portal Web API

This code contains a few extras here compared to the snippets above:

FetchXML
 
<div id="showOnSuccess">
 
</div>
 
<div id="hideOnSuccess">
	{% fetchxml entity_image_query %}
		<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
		<entity name="contact">
		<attribute name="fullname" />
		<attribute name="telephone1" />
		<attribute name="contactid" />
		<attribute name="entityimage" />
		<order attribute="fullname" descending="false" />
		<filter type="and">
			<condition attribute="contactid" operator="eq" uitype="contact" value="{{ user.id }}" />
		</filter>
		</entity>
	</fetch>
	{% endfetchxml %}
	{% if entity_image_query.results.entities.size > 0 %}
		{% for result in entity_image_query.results.entities %}
			<img class="img-responsive" data-entityimage="{{ result.entityimage | join: ',' }}" alt="Profile photo of {{ user.fullname }}" />
		{% endfor %}
	{% endif %}
 
<h3>Profile Photo</h3>
	<form id="profileUploadForm">
		<input id="profilePhoto" accept="image/jpg,image/jpeg,image/gif,image/tif,image/tiff,image/png" type="file">
		<a class="btn btn-default" id="submitPhoto" href="#">Upload</a>
	</form>
</div>
{% include 'Portal Web API Wrapper' %}
 
<script>
 
// When the submitPhoto links is clicked
$( '#submitPhoto' ).click(function(e) {
	// Run the getImageFileContents function
	getImageFileContents();
	// And don't perform the default behaviour for the link e.g. navigation
	e.preventDefault();				
});
 
function getImageFileContents() {
	// Get the first (and only) file selected in the profilePhoto field
	var file = document.getElementById('profilePhoto').files[0];
	// If the user has selected a file
    if (file) {
        // Read the file
        var reader = new FileReader();
		reader.readAsDataURL(file);
        reader.onload = function(e) {
            // The browser has now completed reading the file, do something with it...
			var profilePhotoContents = e.target.result;
			// Split the string at the first comma (removing the base64 prefix)
			profilePhotoContents = profilePhotoContents.substring(profilePhotoContents.indexOf(',')+1);
			// Run the function to upload to the Portal Web API, passing the file's contents as an input
			uploadPhotoAPI(profilePhotoContents);
        };
    }
}
function uploadPhotoAPI(profilePhotoContents) {
$('#submitPhoto').html('Uploading...');
 
webapi.safeAjax({
	type: "PATCH",
	url: "/_api/contacts({{ user.id }})",
	contentType:"application/json",
	data: JSON.stringify({"entityimage": profilePhotoContents}),
	success: function(res, status, xhr) {
		$('div#showOnSuccess').append('<img class="img-responsive" src="data:image/jpeg;base64,' + profilePhotoContents + '" />');
		$('div#hideOnSuccess').hide();
		var entityID = xhr.getResponseHeader("entityid");
		console.log("entityID: " + xhr.getResponseHeader("entityid"));
	}
});
 
}
 
// Convert the entity image to a format that can be displayed in the portal
function toBase64(str) {
	if (!str) return null;
		var uarr = new Uint8Array(str.split(',').map(function (x) { return parseInt(x); }));
		return btoa(String.fromCharCode.apply(null, uarr));
	}
 
	// Find any entity images and convert the byte string to base64
	window.addEventListener('load', function () {
	document.querySelectorAll('img[data-entityimage]').forEach(function (img) {
		var data = img.dataset && img.dataset.entityimage;
		var base64data = data ? toBase64(data) : null;
		if (base64data) {
			img.src = 'data:image/jpeg;base64,' + base64data;
		}
	});
});
 
 
//Credit for the code to display the entity image: https://saddamk.blogspot.com/2018/10/display-image-of-product-in-dynamics.html
 
</script>

In the next post, we’ll see how to take images uploaded through the web API and attach them to existing records using the Note entity

Credit where it’s due… I’d never have worked out how to turn the entity image into something compatible with the portal. Here’s where I got that method: https://saddamk.blogspot.com/2018/10/display-image-of-product-in-dynamics.html

Go deeper · Power Pages Mastery

This guide is one technique. The course is the method.

The course covers this in Level 3 · Advanced customisation: “Portal Web API — deep inserts, editable grids”, alongside “AJAX and asynchronous JavaScript”. A structured programme distilled from dozens of real portal launches — from messy stakeholder requirements to successful go-live.

  • 6 weeks
  • 3 levels
  • Live training + video lessons
Where this fits
  1. Level 1Foundations
  2. Level 2Extending the platform
  3. Level 3Advanced customisation
    • Portal Web API — deep inserts, editable grids
    • AJAX and asynchronous JavaScript
Francesco isn’t just a Power Pages expert — he’s also an exceptional educator. I wholeheartedly endorse this course as the ideal resource for anyone eager to accelerate their career in Power Pages.
Safwan Karim — Senior Consultant — Accelerate Tech
Filed underPortals Web API

4 comments

  1. Ryan

    Could you please add screenshots of *where* the code is pasted into?

    1. Franco MussoAuthor

      Hi Ryan. The code needs to go into a Web Template. In my case, I’ve created a custom web template for the Profile page, replacing the out-of-the-box web template using the steps shown in this post: https://francomusso.com/customising-the-profile-page-on-power-apps-portals
      I’d strongly suggest that anyone new to Power Apps Portals master the basics before attempting anything with the Portal Web API. A good understanding liquid and some understanding of jQuery will give you a solid foundation.

    1. Franco MussoAuthor

      The easiest way now that Dataverse File columns are supported in forms (basic and multistep, in edit mode only) is simply to add a File column to your table and include that on the form.

Leave a comment

Your email address won’t be published. Comments are moderated.

Consulting

Need a Power Pages build that editors love and brands survive?

I help teams design and build Power Pages sites with bespoke UX. Tell me about yours.

Search

Book a discovery call

30 minutes. Bring your trickiest Power Pages problem — I’ll tell you honestly whether I’m the right fit.