PortalUser::addToFavorites is not working

238
5
09-23-2021 11:36 PM
LucDeBatselier
New Contributor

What I done:
1. connected to the signal
connect(m_portal->portalUser(), &Esri::ArcGISRuntime::PortalUser::addToFavoritesCompleted, this, [](bool success)

2. called the method:
m_portal->portalUser()->addToFavorites(m_webmaps->at(itemId.toInt()));

3. inside the connected lambda I always get false, and an item is not added to favorites.


And what I checked:
1. User is definitely logged in, because I can read favorites/groups/username etc

2. m_webmaps->at(itemId.toInt()) is the proper item, because I can call m_webmaps->at(itemId.toInt())->title() and compare it with the title of an item that I clicked on (they are the same items).

0 Kudos
5 Replies
LucDeBatselier
New Contributor

Thanks for your answer, ABladon!

You are right, I got an error: "You do not have permissions to access this resource or perform this operation."
I logged in via m_portal->setCredential() and then m_portal->load(). Is it not enough to just set credentials?

0 Kudos
AndrewBladon
Esri Contributor

Hi LucDeBatselier.

Access to a resource is granted by the item provider. For example, an author on ArcGIS online can assign different sharing levels to an item, including:

  • Everyone (i.e. public).
  • People in an organization.
  • People in a user group.
  • The author of the item only (i.e. private).

See here for more information on sharing items.

To enable a user to add a PortalItem to a favourites list, the user must be granted access to the PortalItem by the item provider.

Hopefully reviewing the sharing levels of the PortalItem’s being accessed will help you resolve the issue.

0 Kudos
AndrewBladon
Esri Contributor

Thanks for getting in touch LucDeBatselier!

We have created a simple app to test the PortalUser::addToFavorites() method and to recreate your issue.

The addToFavorites() method appears to be working. However, we are able to recreate your issue if an error occurs while trying to add a PortalItem to a PortalUser’s favourites (e.g. a user not having the appropriate permissions).

To help identify whether an error could be causing your issue, we suggest creating a connection between the PortalUser::errorOccurred signal and a slot that prints out the error message(s). For example:

connect(m_portal->portalUser(), &PortalUser::errorOccurred, this, [](Error error){

    qDebug() << error.message() << error.additionalMessage();

  });

Hopefully this helps! Let us know if the issue remains after checking for and resolving any errors.

0 Kudos
LucDeBatselier
New Contributor

Thanks for your answer, Andrew Bladon!

I subscribed to PortalUser::errorOccurredand when I tried to add item to favorites I got an error message:
    Error: "PortalItem must be loaded before adding item to favorites."
    Additional error: ""
And then PortalUser::addToFavoritesCompleted returned false, what is logically.

So, the first problem is clear: not only portal, but also portal item must be loaded. 
Then I loaded portal item:

m_webmaps->at(itemId.toInt())->load();

(where m_webmaps is QScopedPointer<Esri::ArcGISRuntime::PortalItemListModel>
and itemId is just number of the item in list.)

Then I checked if portal item is loaded:

m_webmaps->at(itemId.toInt())->loadStatus() == Esri::ArcGISRuntime::LoadStatus::Loaded

 and tried to add item to favorites again:

m_portal->portalUser()->addToFavorites(m_webmaps->at(itemId.toInt()));

, but now neither PortalUser::addToFavoritesCompleted nor PortalUser::errorOccurred are triggered...
I got only message Licensed for Developer Use Only in the console.

May be you have any ideas and solutions for it?

0 Kudos
AndrewBladon
Esri Contributor
Hi LucDeBatselier!
 
From your message we understand that you are trying the following: 

 

m_webmaps->at(itemId.toInt())->load();

if (m_webmaps->at(itemId.toInt())->loadStatus() == LoadStatus::Loaded)
  m_portal->portalUser()->addToFavorites(m_webmaps->at(itemId.toInt()));

 

 
PortalItems are loaded asynchronously. If we have understood your current approach correctly, the LoadStatus check may be occuring before the PortalItem has finished loading. We suggest trying a connection with the PortalItem::doneLoading signal to ensure the LoadStatus check and subsequent addToFavourites operation are executed after the PortalItem has finished loading. For example:

 

// Define connection.
connect(m_webmaps->at(itemId.toInt()), &PortalItem::doneLoading, this, [](Error error){
    if (!error.isEmpty())
        qDebug() << error.message() << error.additionalMessage(); 
    else if (m_webmaps->at(itemId.toInt())->loadStatus() == Esri::ArcGISRuntime::LoadStatus::Loaded)
        m_portal->portalUser()->addToFavorites(m_webmaps->at(itemId.toInt()));
});

// Load PortalItem.
m_webmaps->at(itemId.toInt())->load();

 

 
Please let us know if you have already tried this or if you encounter any further issues.
0 Kudos