Community Tip - You can change your system assigned username to something more personal in your community settings. X
Hi,
I have a use case in which i wanted to create media entity dynamically.
please suggest.
Thanks in advance!
You can do this, but as far as i know this is not supported and is not part of the standard Thingworx API.
You need to create a service that makes a PutJSON request to your thingworx media entities uri, i.e.:
http://[YOUR THINGWORX SERVER]/Thingworx/MediaEntities?reason=header%20%3A%20name%20%3A%20updated%2C%20content%20%3A%20updated%0A
with the following JSON content:
{
"content": "[BASE64 ENCODED IMAGE]
"tags": [],
"documentationContent": "",
"runTimePermissions": {
"permissions": []
},
"description": "",
"name": "[MEDIA ENTITY NAME]",
"aspects": {},
"designTimePermissions": {
"Update": [],
"Create": [],
"Read": [],
"Metadata": [],
"Delete": []
},
"avatar": "",
"homeMashup": ""
}
Edit: If the image is available via a URL, you can use the LoadMediaEntity service:
var params = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
description: undefined /* STRING */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: undefined /* STRING */,
timeout: undefined /* NUMBER */,
tags: undefined /* TAGS */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
name: undefined /* STRING */,
username: undefined /* STRING */
};
// result: IMAGELINK
var result = Resources["ContentLoaderFunctions"].LoadMediaEntity(params);
In this way, you could also dynamically upload the image to a file repository, get the link, and then load it as a media entity from the file repo link.
The following service will create a new media, load an image from URL, and then update the newly created media with the image loaded. It can be alternated to the desired goal whether it's an immediate media assignment or updating the existing one.
var params = {
name: "NewMediaEntity" /* STRING */,
content: undefined /* IMAGE */
};
// no return
Resources["EntityServices"].CreateMediaEntity(params);
var params = {
url: 'https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg' /* STRING */,
};
// result: IMAGE
var image = Resources["ContentLoaderFunctions"].LoadImage(params);
var params = {
name: 'NewMediaEntity'/* STRING */,
content: image
};
// no return
Resources["EntityServices"].UpdateMediaEntity(params);