TransWikia.com

Updating tags on user's portal item via API

Geographic Information Systems Asked by david wendelken on January 15, 2021

I’m using the extract data widget to create extract files. It writes these files to the logged in portal user’s content.

I want to tag the items that were just created but I can’t find anywhere in the api that lets me do that.

Example of what I want to do:

var myUrl = 'somevalue';  // I know this value at runtime, so not a problem.
var myItemId = 'someOtherValue'; // Ditto.

var thisPortal = new arcgisPortal.Portal(myurl);
thisPortal.signIn().then(function (loggedInUser) {
    loggedInUser.getItem(myItemId).then(function(theItem) {
         // I want to add a tag to the item so I know it's ok to automatically toss it a day later.
         theItem.tags[theItem.tags.length] = 'Delete in One Day';
        // I want to write the item back to the portal.
        // And I cannot find any documentation on this step!
    })
});

I don’t want to have to trust the users to clean up after themselves. I have a concern that they will clog up the disk with extract files that they will never look at again and never throw away again.

I can ask the users to mark whether the extract should be held temporarily or permanently before the extract files are created (and default to temporarily!) If I then tag the extract files accordingly, I can have an automated program run every evening and toss out the expired ones.

One Answer

This works:

var myUrl = 'somevalue';  // I know this value at runtime, so not a problem.
var myItemId = 'someOtherValue'; // Ditto.



var thisPortal = new arcgisPortal.Portal(myurl);
thisPortal.signIn().then(function (loggedInUser) {

    var theToken = loggedInUser.credential.token;
    loggedInUser.getItem(myItemId).then(function(theItem) {
         // I want to add a tag to the item so I know it's ok to automatically toss it a day later.
         // then I want to write the item back to the portal.
         // I could not find any way to do this in the 3.20 javascript api, so I'm using the restful api.
         var tagsCsv = '';
         for (var i = 0; i < theItem.tags.length; i++) {

             tagsCsv = tagsCsv + theItem.tags[i] + ',';
         }
         tagsCsv = tagsCsv + 'Delete in One Day';

         // I could not find any documentation for the restful api to do this either, but I could observe via the

         // browser debugger what the portal did when I manually added a tag to the portal item.

         var updateUrl = '{portalUrl}sharing/rest/content/users/{userId}/items/{itemId}/update';
         updateUrl = updateUrl.replace('{portalUrl}',myUrl);
         updateUrl = updateUrl.replace('{userId}',loggedInUser.username);
         updateUrl = updateUrl.replace('{itemId}', theItem.id);
         xhr.post(updateUrl, {

            data: {

                tags: tagsCsv,
                clearEmptyFields: 'true',
                id: theItem.id,
                f: 'json',

                token: theToken

            }

         }).then(function(data) {  // officially a success

                  // inspect and take whatever action you deem fit.

             },

             function(error) {   // officially a failure

                  // inspect and take whatever action you deem fit.

             },

             function(evt) {  // status notification, works depending on the browser version.

                  // inspect and take whatever action you deem fit.

             });

    });
});

I had to transpose this by hand, hopefully it's syntactically correct. If not, post the mistakes and I'll edit it to match.

Answered by david wendelken on January 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP