Learn how to use the portal service to set the sharing level for an item in your portal.
Prerequisites
You need an ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account to use your portal.
Steps
Get an access token
To perform the portal service operation in this tutorial, you need an access token from OAuth 2.0 credentials. It is the only supported form of authentication. The use of API keys is not supported.
-
Go to the Create OAuth credentials for user authentication tutorial to get a set of OAuth 2.0 credentials.
-
Get an access token from the OAuth 2.0 credentials.
-
Copy the access token to your clipboard when prompted.
Set up environment
- In a different window, launch Postman and create a blank request.
- If you are using the CDN libraries, to get started.
- To get started, .
Create a new private item
You will create a new web map in your portal. The default sharing level will be private. In a later step, you will use the web map item ID to change the sharing level to public using the portal service.
- In a different tab, sign in to your portal.
- Click on Map to open up Map Viewer.
- Add a basemap, layer, and viewpoint for your map.
- Click Save > Save as.
- Provide a title and tags for your web map. Then, click Save.
- In the URL part of your browser, copy the web map item ID and store it in a safe location. You can find the item ID in
https:
. You will use this item ID in a later step.//arcgis.com/apps/mapviewer/index.html?webmap=< ITEM_ ID>
Perform a share
request
Now, you will be changing the sharing level of your item from private to public using the portal service.
- Set the request type to
POST
. - In the URL field, type in
https:
. Replace//arcgis.com/sharing/rest/content/users/< USER_ NAME>/items/< ITEM_ ID>/share <
with your username andUSER_ NAME> <
with the item ID you copied in the previous step.ITEM_ ID> - Set the
f
parameter topjson
in the parameter list. - Set the
everyone
parameter totrue
to set the sharing level to public. To see other sharing levels, go to Sharing items. - Set the
token
parameter to your access token. - Set the
q
parameter totype:
in the parameter list."Web Map" - Run the request.
POST https://arcgis.com/sharing/rest/content/users/<USER_NAME>/items/<ITEM_ID>/share
Content-Type: application/x-www-form-urlencoded
&f=json
&everyone=true
&token=<YOUR_ACCESS_TOKEN>
-
Add another
<script>
tag and import a CDN library forarcgis-rest-portal
.Use dark colors for code blocks <pre id="result"></pre> <!-- require ArcGIS REST JS libraries from https://unpkg.com --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-portal@4.4.0/dist/bundled/portal.umd.js"></script> <script> </script>
-
Import the following classes from the
arcgis
global.Rest Use dark colors for code blocks <pre id="result"></pre> <!-- require ArcGIS REST JS libraries from https://unpkg.com --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-portal@4.4.0/dist/bundled/portal.umd.js"></script> <script> const { setItemAccess } = globalThis.arcgisRest; const { ArcGISIdentityManager } = globalThis.arcgisRest; </script>
-
Create an
ArcGISIdentity
object and pass in your access token.Manager Use dark colors for code blocks <pre id="result"></pre> <!-- require ArcGIS REST JS libraries from https://unpkg.com --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-portal@4.4.0/dist/bundled/portal.umd.js"></script> <script> const { setItemAccess } = globalThis.arcgisRest; const { ArcGISIdentityManager } = globalThis.arcgisRest; ArcGISIdentityManager.fromToken({ token: "YOUR_ACCESS_TOKEN", // Access token from OAuth2.0 authorizes action on behalf of an ArcGIS user }).then((authentication) => { }); </script>
-
Inside the
ArcGISIdentity
block, call theManager set
method. Pass in your item ID into the appropriate field and setItem Access access
topublic
. This will set the sharing level of your item to public. To see other sharing levels, go to Sharing items.Use dark colors for code blocks <pre id="result"></pre> <!-- require ArcGIS REST JS libraries from https://unpkg.com --> <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-portal@4.4.0/dist/bundled/portal.umd.js"></script> <script> const { setItemAccess } = globalThis.arcgisRest; const { ArcGISIdentityManager } = globalThis.arcgisRest; ArcGISIdentityManager.fromToken({ token: "YOUR_ACCESS_TOKEN", // Access token from OAuth2.0 authorizes action on behalf of an ArcGIS user }).then((authentication) => { return setItemAccess({ id: "ITEM_ID", // Hosted item access: "public", authentication: authentication, }); }); </script>
-
Add the following
require
statement to your code.Use dark colors for code blocks require(["esri/request"], (esriRequest)=> { });
-
Create a variable called
url
and set its value as the portal service URL, e.g.https:
. Replace//arcgis.com/sharing/rest/content/users/< USER_ NAME>/items/< ITEM_ ID>/share <
with your username andUSER_ NAME> <
with the item ID you copied in the previous step.ITEM_ ID> Use dark colors for code blocks require(["esri/request"], (esriRequest)=> { const url = "https://arcgis.com/sharing/rest/content/users/<USER_NAME>/items/<ITEM_ID>/share" });
-
Create an object called
options
and define your search parameters. Pass in your access token into thetoken
field and seteveryone
totrue
. This will set the sharing level of your item to public. To see other sharing levels, go to Sharing items.Use dark colors for code blocks require(["esri/request"], (esriRequest)=> { const url = "https://arcgis.com/sharing/rest/content/users/<USER_NAME>/items/<ITEM_ID>/share" const options = { query: { f: "pjson", everyone: "true", token: "YOUR_ACCESS_TOKEN" } }; });
-
Execute the POST call and print the
JSON
response.Use dark colors for code blocks require(["esri/request"], (esriRequest)=> { const url = "https://arcgis.com/sharing/rest/content/users/<USER_NAME>/items/<ITEM_ID>/share" const options = { query: { f: "pjson", everyone: "true", token: "YOUR_ACCESS_TOKEN" } }; esriRequest(url, { method: "post", ...options }).then((response) => { const data = response.data; console.log(data); }); });
View the results
Go back to your portal and you should see that the sharing level of your item has changed to public. Your code may also print an output like this to verify that the operation was successful:
{
"notSharedWith": [],
"itemId": "<ITEM_ID>"
}
What's next
Copy a hosted layer item
Use the portal service to copy a hosted layer item in your portal.
Create a new group and add members
Use the portal service to create a new group and add members in your portal.