Sharing a dashboard by using the REST API

Comments

6 comments

  • Official comment
    Avatar
    Katie Garrison

    Please note, this code flow will not work for removing user access to dashboards. Only for adding user (or group) access.

    Comment actions Permalink
  • Avatar
    Nick Burleigh

    Omer,

    Thanks for this. If you wanted to share a dashboard with a group in this automated fashion (and the dashboard is not already shared with that group), how would you change this script to accommodate that?

    Thanks,

    Nick

    0
    Comment actions Permalink
  • Avatar
    Katie Garrison

    Hi Nick (and anyone who is looking to do this),

    To change from user to group, please try the code below:

    import requests
    import time
    import json

    user_name = 'Test@sisense.com' #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

    Katie

    0
    Comment actions Permalink
  • Avatar
    Andy Liang (Edited )

    Hi,

    I'm trying to share the dashboard via the API. It works to share the dashboard. I can see the user in the dashboard's share list. However, the user hasn't been shared access to the cube for the dashboard. When I perform the sharing dashboard action on Sisense UI, the user is shared access to the cube at the same time so I expect that would be the same for the API call. How can I share the user access to the cube as well? Does it need to be a separated API call or some flag in the same call?

    In the publishing dashboard API, I set the "force=true" because I don't want the user to perform any action in order to get the dashboard.

    Thanks in advance

    0
    Comment actions Permalink
  • Avatar
    Andy Liang

    I figured out that the permission did get propagated to the cube but seems like it takes some time. In our case, we want the user to be able to access the dashboard immediately to we call to the elasticubes permissions endpoint directly. That seems to be in effect immediately.

    0
    Comment actions Permalink
  • Avatar
    Tamir Basin

    Hi Andy, Could you please share your code/script?

     

    0
    Comment actions Permalink

Please sign in to leave a comment.