Toggle menu

Create a Media Item using the iCM API Worker

This form is very similar to the "create article" example. It uses the iCM API worker and an API key that provides iCM user credentials.

Here's the part of the function that creates our media item from the fields on the form:

function(helper, processor, props, context) {
    let msg = "";
    let media = {
        "MediaID": 0,
        "Title": helper.queryFieldValue('TITLE'),
        "Description": helper.queryFieldValue('DESC'),
        "Type": "image",
        "Display": "on",
        "GroupID": 168,
        "DisplayStartDate": "",
        "DisplayEndDate": "",
        "Keywords": "",
        "ReviewDate": "",
        "ReviewInterval": "",
        "ArticleID": 0,
        "LinkID": 0,
        "FileArray": [{
            "FilestoreRef": helper.queryFieldValue('IMAGE'),
            "FileType": "Image"
        }, {
            "FilestoreRef": helper.queryFieldValue('IMAGE'),
            "FileType": "Thumbnail"
        }]
    };
    try {
        // Media creation
        let result = helper.utilServerAPIServerCall("icmapi", "iCM_obj_mediaUpdate", {
            "attributes": {
                "MediaStruct": media
            }
        }, {
            "options": {
                "apiKey": "B9772652-XXXX-XXXX-XXXX-F9621EEB295E"
            },
            "rawMode": false
        });
        if (result.CONTENT && result.CONTENT.MediaID) {
            msg = "Media created with ID: " + result.CONTENT.MediaID;
        } else {
            msg = JSON.stringify(result);
        }
    } catch (ex) {
        msg = "Exception: " + ex.message;
    }
    // Create and return a new action object
    let actionObject = helper.utilCreateAction(props["FIELD"]["NAME"], "CONFMESSAGE");
    helper.utilAddActionProperty(actionObject, "CLASS", "form, com.gossinteractive.community.form.actions.ConfirmationAction");
    helper.utilAddActionProperty(actionObject, "MESSAGE", msg);
    return actionObject;
}

Our API Key supplies the credentials of an iCM user with sufficient privileges to create media items in group 168.

Apart from the title and description, most fields are empty as the API inserts sensible defaults.

We can get the filestore reference by querying the value of the upload field. The upload field automatically generates a string in the format 'file/95B617B9-00D6-4908-8785-C450E37D98B8/formdatafileupload.png'

Note how, in the case of an image, we have to set both the "image" and "thumbnail" components of the media item. Required components will vary between iCM installations, depending upon the media definitions that have been created. The upload field itself includes standard validation so that only image media types are accepted.

Last modified on August 02, 2023

Share this page

Facebook icon Twitter icon email icon

Print

print icon