How to update the tags on a user's portal item via the api?

1033
2
Jump to solution
05-25-2017 06:56 AM
DavidWendelken
Occasional Contributor

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!
    })
});

Tags (1)
1 Solution

Accepted Solutions
DavidWendelken
Occasional Contributor

This is special, but it does work:

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 + ',';
         }
         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.

View solution in original post

2 Replies
KellyHutchins
Esri Frequent Contributor

Looks like you are using version 3.x of the JSAPI. If so then you'll have to use esriRequest with the ArcGIS Rest API to call the update operation. 

In version 4.x of the JSAPI there is an update method on the PortalItem class that will allow you to write updates to the item in Online. 

DavidWendelken
Occasional Contributor

This is special, but it does work:

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 + ',';
         }
         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.