Sharing A Dashboard By Using The REST API
Sharing a dashboard by using the REST API is very useful in case you need to automate the sharing process.
However, in order to do so more than one API call is required, and not using them all in the right order will result in an unsuccessful share.
Sharing Dashboard through API steps:
- Get Auth token(POST api/v1/authentication/login) - this is required in order to run any of our API calls.
- Get the id of the user you want to share the dashboard with(GET api/v1/users) - in order to share a dashboard with a user you need to provide the system with the user id, and the user email/username is not enough. This id can be retrieved by using this call.
- Get the id of the dashboard you want to share - you can get the dashboard id from the API(GET api/v1/dashboards) or from the dashboard url (the id will appear at the end of the dashboard url: http://localhost:8081/app/main#/dashboards/5829b46bd009e91420000047)
- Share the dashboard with the user - In order to do so you will need to add the user to the shares object of the dashboard. You can use the PATCH api/v1/dashboards/{id} call for this. Important - updating the shares object by using this call will override it, hence if you want to keep the current users which the dashboard is shared with, you need to first get the list of current shared users, append to it the user you want to add, and only then run the PATCH command. You can get this object from the GET api/v1/dashboards call. Important#2 - you need to make sure that each user appears in the list only once, or else the share will fail.
- Publish the dashboard to the user - After you have added the user to the shares object of the dashboard, in order for the user to see it you need to republish the dashboard to him. In order to do so, use the following API call: POST api/v1/dashboards/{id}/publish
Here is an example in Python for doing this:
import requests
import time
import json
user_name = '[email protected]' #Change to your admin user name.
password = 'Sisense' #Change to your password.
Sisense_url = "http://localhost:8081" #Change to your Sisense address.
new_user = "[email protected]" #Change to the email of the user you want to share the dashboard with.
user_rule = "edit" # change to "view" if the user is a viewer. If it is a designer ir admin, leave it as it is.
dashboard_id = '5829b46bd009e91420000047'
#------------------------------------------------------------------------------
# Get auth_token
url = Sisense_url+"/api/v1/authentication/login"
payload = "username="+user_name.replace('@','%40')+"&password="+password
headers = {
'cache-control': "no-cache",
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
auth_token = response.json()["access_token"]
auth_token = "Bearer "+auth_token
# Get user
url = Sisense_url + "/api/v1/users"
headers = {
'accept': "application/json",
'authorization': auth_token
}
response = requests.request("GET", url, headers=headers)
response = response.json()
user_id = ''
for user in response:
try:
if user["userName"] == new_user:
user_id = user["_id"]
except KeyError:
pass
new_user_share = {
"shareId": user_id,
"type": "user",
"rule": user_rule,
"subscribe": False
}
#Get current shared users
url = Sisense_url + "/api/v1/dashboards/"+dashboard_id
headers = {
'accept': "application/json",
'authorization': auth_token
}
response = requests.request("GET", url, headers=headers)
response = response.json()
dashboard_shares = response["shares"]
dashboard_shares.append(new_user_share)
arr_users = []
for user in dashboard_shares:
try:
arr_users.index(user["shareId"])
dashboard_shares.remove(user)
except:
arr_users.append(user["shareId"])
dashboard_shares = {"shares": dashboard_shares}
#Add user to dashboard shares
payload = json.dumps(dashboard_shares)
url = Sisense_url + "/api/v1/dashboards/"+dashboard_id
headers = {
'accept': "application/json",
'content-type': "application/json",
'accept': "application/json",
'authorization': auth_token
}
response = requests.request("PATCH", url, data=payload, headers=headers)
#Publish the dashboard to the user
url = Sisense_url + "/api/v1/dashboards/"+dashboard_id+"/publish?force=false"
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': auth_token
}
response = requests.request("POST", url, headers=headers)
print "Done!"
Please note, this code flow will not work for removing user access to dashboards. Only for adding user (or group) access.
To change from user to group, please try the code below:
import requests
import time
import json
user_name = '[email protected]' #Change to your admin user name.
password = 'Sisense' #Change to your password.
Sisense_url = "http://localhost:8081" #Change to your Sisense address.
new_group = "market group" #name the group you want to share with
group_rule = "edit" # change to "view" if the group is a viewer. If it is a designer or admin, leave it as it is.
dashboard_id = '5dc5c237832aeb53441186bb'
#------------------------------------------------------------------------------
# Get auth_token
url = Sisense_url+"/api/v1/authentication/login"
payload = "username="+user_name.replace('@','%40')+"&password="+password
headers = {
'cache-control': "no-cache",
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
auth_token = response.json()["access_token"]
auth_token = "Bearer "+auth_token
# Get groups
url = Sisense_url + "/api/v1/groups"
headers = {
'accept': "application/json",
'authorization': auth_token
}
response = requests.request("GET", url, headers=headers)
response = response.json()
group_id = ''
for group in response:
try:
if group["name"] == new_group:
group_id = group["_id"]
except KeyError:
pass
new_group_share = {
"shareId": group_id,
"type": "group",
"rule": group_rule,
"subscribe": False
}
#Get current shared groups
url = Sisense_url + "/api/v1/dashboards/"+dashboard_id
headers = {
'accept': "application/json",
'authorization': auth_token
}
response = requests.request("GET", url, headers=headers)
response = response.json()
dashboard_shares = response["shares"]
dashboard_shares.append(new_group_share)
#arr_shares = []
#for group in dashboard_shares:
# try:
# arr_groups.index(group["shareId"])
# dashboard_shares.remove(group)
#
# except:
# arr_groups.append(group["shareId"])
dashboard_shares = {"shares": dashboard_shares}
#Add group to dashboard shares
payload = json.dumps(dashboard_shares)
url = Sisense_url + "/api/v1/dashboards/"+dashboard_id
headers = {
'accept': "application/json",
'content-type': "application/json",
'accept': "application/json",
'authorization': auth_token
}
response = requests.request("PATCH", url, data=payload, headers=headers)
#Publish the dashboard to the group
url = Sisense_url + "/api/v1/dashboards/"+dashboard_id+"/publish?force=false"
headers = {
'accept': "application/json",
'content-type': "application/json",
'authorization': auth_token
}
response = requests.request("POST", url, headers=headers)
print ("Done!")
Please test this out before adding to your automation
Updated 03-02-2023
intapiuser
Admin
Joined December 15, 2022