Add items to portal

View inQMLC++
View on GitHub
Sample viewer app

This sample demonstrates how to add and delete items in a user's portal.

screenshot

Use case

Portals allow you to share and publish data with others. For example, you may create or collect some data on your device and want to upload that to a cloud storage location to share with others or simply save for later use. In this case, a CSV file is created and uploaded to ArcGIS Online.

How to use the sample

  1. Press the Authenticate Portal button and sign into your ArcGIS Online account.
  2. Press the Add Item button, and this will add the CSV to your Portal.
  3. Press the Delete Item button, and this will delete that same item from your Portal.

How it works

  1. A Portal is created with a Credential using OAuthModeUser. The Portal instance is loaded and the OAuth sign in page is displayed. The user must now provide credentials.
  2. Once the sign-in process is complete, a PortalItem of type CSV is created using this Portal instance.
  3. When the portal is successfully loaded, the PortalUser object is obtained from the Portal.
  4. When the "Add Item" button is clicked, the item is added via the PortalUser's addPortalItemWithUrl method. The PortalItem and the local URL of the CSV file are supplied as parameters.
  5. When the item has been successfully added, it is loaded in order to retrieve online properties such as the itemId.'
  6. When the "Delete Item" button is clicked, the item is passed as a parameter for the portalUser's deleteItem method.

Relevant API

  • Portal
  • PortalItem
  • PortalUser::addPortalItemWithUrl
  • PortalUser::deleteItem

Tags

add item, cloud, portal

Sample Code

AddItemsToPortal.cppAddItemsToPortal.cppAddItemsToPortal.hAddItemsToPortal.qml
Use dark colors for code blocksCopy
                                                                                                                                                                                                         
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// [WriteFile Name=AddItemsToPortal, Category=CloudAndPortal]
// [Legal]
// Copyright 2016 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the Sample code usage restrictions document for further information.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "AuthenticationManager.h"
#include "Portal.h"
#include "PortalItem.h"
#include "AddItemsToPortal.h"

using namespace Esri::ArcGISRuntime;

AddItemsToPortal::AddItemsToPortal(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent),
  m_portal(new Portal(new Credential(OAuthClientInfo("iLkGIj0nX8A4EJda", OAuthMode::User), this), this)),
  m_item(new PortalItem(m_portal, this))
{
  AuthenticationManager::instance()->setCredentialCacheEnabled(false);
  m_item->setTitle("Add Items Sample");
  m_item->setType(PortalItemType::CSV);
}

AddItemsToPortal::~AddItemsToPortal() = default;

void AddItemsToPortal::init()
{
  // Register the types for QML
  qmlRegisterType<AddItemsToPortal>("Esri.Samples", 1, 0, "AddItemsToPortalSample");
}

void AddItemsToPortal::componentComplete()
{
  QQuickItem::componentComplete();

  if (m_portal)
  {
    connect(m_portal, &Portal::loadStatusChanged, this, [this]()
    {
      emit portalLoadedChanged();

      if (m_portal->loadStatus() != LoadStatus::Loaded)
        return;

      m_user = m_portal->portalUser();
      m_busy = false;
      connectUserSignals();
    });
  }

  if (m_item)
  {
    connect(m_item, &PortalItem::loadStatusChanged, this, [this]()
    {
      if (m_item->loadStatus() != LoadStatus::Loaded)
        return;

      m_busy = false;
      emit portalItemIdChanged();
      emit portalItemTitleChanged();
      emit portalItemLoadedChanged();
      setStatusText("Succesfully loaded item from portal." + m_item->itemId());
    });
  }
}

bool AddItemsToPortal::portalLoaded() const
{
  return m_portal && (m_portal->loadStatus() == LoadStatus::Loaded);
}

bool AddItemsToPortal::portalItemLoaded() const
{
  return m_item && (m_item->loadStatus() == LoadStatus::Loaded);
}

QString AddItemsToPortal::portalItemId() const
{
  if (m_itemDeleted || !m_item)
    return QString();

  return m_item->itemId();
}

QString AddItemsToPortal::portalItemTitle() const
{
  if (m_itemDeleted || !m_item)
    return QString();

  return m_item->title();
}

QString AddItemsToPortal::portalItemTypeName() const
{
  if (m_itemDeleted || !m_item)
    return QString();

  return m_item->typeName();
}

bool AddItemsToPortal::itemDeleted() const
{
  return m_itemDeleted;
}

QString AddItemsToPortal::statusText() const
{
  return m_statusText;
}

bool AddItemsToPortal::busy() const
{
  return m_busy;
}

void AddItemsToPortal::authenticatePortal()
{
  if (m_portal)
    m_portal->load();
}

void AddItemsToPortal::addItem()
{
  if (!m_user || !m_item)
    return;

  m_busy = true;

  //! [PortalUser addItemWithUrl]
  QUrl localCSV("qrc:/Samples/CloudAndPortal/AddItemsToPortal/add_item_sample.csv");
  m_user->addPortalItemWithUrl(m_item, localCSV, "add_item_sample.csv" );
  //! [PortalUser addItemWithUrl]
}

void AddItemsToPortal::deleteItem()
{
  if (!m_user || !m_item)
    return;

  m_busy = true;
  m_user->deletePortalItem(m_item);
}

void AddItemsToPortal::connectUserSignals()
{
  if (!m_user)
    return;

  connect(m_user, &PortalUser::errorOccurred, this, [this](Esri::ArcGISRuntime::Error error)
  {
    m_busy = false;
    setStatusText( QString(error.message() + ": " + error.additionalMessage()));
  });

  //! [PortalUser addPortalItemCompleted]
  connect(m_user, &PortalUser::addPortalItemCompleted, this, [this](bool success)
  {
    m_busy = false;

    if (!success)
      return;

    setStatusText("Successfully added item.");
    m_item->load();
  });
  //! [PortalUser addPortalItemCompleted]

  connect(m_user, &PortalUser::deletePortalItemCompleted, this, [this](bool success)
  {
    m_busy = false;

    if (!success)
      return;

    m_itemDeleted = true;
    emit itemDeletedChanged();
    emit portalItemIdChanged();
    emit portalItemTitleChanged();
    emit portalItemTypeNameChanged();
    setStatusText("Successfully deleted item " + m_item->itemId());
  });
}


void AddItemsToPortal::setStatusText(const QString &statusText)
{
  m_statusText = statusText;
  emit statusTextChanged();
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.