How to edit an assets metadata fields via the front end


(John) #1

Hello
I have a data record asset with a custom metadata schema applied containing a field named ‘Location’.
How would I go about updating the value of this ‘Location’ field via the front end?
Using an asset builder I can create new assets, but how to modify an existing one?
Cheers!


(Tim Trodd) #2

Hi John,

I would recommend using the js api:

https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#setMetadata

Specifically the setMetadata function:

js_api.setMetadata({
“asset_id”:100,
“field_id”:900,
“field_val”:“metadata”,
“dataCallback”:customDataCallbackFunc
})

You set the asset id and metadata field ID in the above (can use declared keywords etc.) then can grab the field value of what the user has entered and use that on a button click.

Quick example:

<a href="#" type="button" class="field" data-asset="113088" data-metadata_field="131985">Submit</a>

 Field input: <input type="text" name="fname" id="fieldinput">

<!-- load JS API -->
<script src="xxxx/jsapi.js"></script>

<!-- jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<!-- Set Metadata Function -->
<script type="text/javascript">
$(document).ready(function() {
var options = new Array();
options['key'] = 'xxxxxxxxxx';
var js_api = new Squiz_Matrix_API(options);
	$('.field').click(function() {
        console.log("clicked!");
		var assetId = $(this).attr('data-asset');
		var fieldId = $(this).attr('data-metadata_field');
		var fieldValue  = document.getElementById("fieldinput").value;

		js_api.setMetadata({
		   "asset_id":assetId,
		   "field_id":fieldId,
		   "field_val":fieldValue,
		   "dataCallback": function() {
                            console.log("callback ran!");
                            location.reload();
                            
                        }
		});

	});
});


</script>

Tim


(Bart Banda) #3

You can also use an asset builder to edit assets. Just append ?edit=1234 after the URL of the asset builder where 1234 is the asset ID of the asset you are wanting to edit.