1

In Azure Synapse Analytics, I'm trying to create a code that returns a list with all notebook names.

In databricks I verified that it is possible by these two solutions:

  1. https://docs.databricks.com/dev-tools/api/latest/workspace.html#example
  2. Get list of all notebooks in my databricks workspace

, but I can't get a solution for synapse notebooks.

Can anyone please help me in achieving this?

Thank you!

CHEEKATLAPRADEEP
  • 12,191
  • 1
  • 19
  • 42
coding
  • 135
  • 2
  • 9

1 Answers1

1
  • You can use the Azure synapse REST API to get the list of notebooks available in the synapse workspace. The following is an image showing the available notebooks in my synapse workspace along with the code that you can use to achieve your requirement.

enter image description here

#install msal using !pip install msal for getting bearer token
import msal

client_id = "<client_id>"
authority = "https://login.microsoftonline.com/<tenant_id>"
client_secret = "<client_secret>"

# Create a ConfidentialClientApplication instance
app = msal.ConfidentialClientApplication(client_id=client_id, authority=authority, client_credential=client_secret)

# Get the token
scopes = ["https://dev.azuresynapse.net/.default"]
result = app.acquire_token_for_client(scopes=scopes)
print(result)

enter image description here

  • Call the synapse Rest API using python's requests library (GET method).
import requests

response = requests.get("https://synapse3003.dev.azuresynapse.net/notebooks?api-version=2020-12-01", headers = {"Authorization":f"Bearer {result['access_token']}"}).json()

print(len(response['value']))

for i in response['value']:
    print(i)

enter image description here

NOTE: You need to create a service principle by navigating to azure AD-> App registration. After that go to your synapse studio->manage tab->Access control and then add your service principle with appropriate role to create token as in above procedure.

Saideep Arikontham
  • 5,558
  • 2
  • 3
  • 11
  • Hi Saideep, thanks a lot for helping me! I'm trying to execute your code and the code generated the token well, but I got an error on this step: print(len(response['value'])) Error: KeyError: 'value'. I added the app as a Synapse Artifact User, is enough ? once again thanks a lot – coding Mar 31 '23 at 09:10
  • Hey @coding, print just the response and check whether it has returned the list of notebooks or not. Provide the error message if you are getting one. My app has Synapse Administrator role. – Saideep Arikontham Mar 31 '23 at 09:30
  • Hey Saideep, I printed the response and I got this error: {'code': 'InvalidTokenIssuer', 'message': "Token Authentication failed with SecurityTokenInvalidIssuerException - IDX10205: Issuer validation failed. Issuer: '[PII of type 'System.String' is hidden. probably I need to give a higher permission level, I'll check that – coding Mar 31 '23 at 09:41
  • Hey @coding, I was able to access the API and get correct response even with Synapse Artifact User role. – Saideep Arikontham Mar 31 '23 at 09:59
  • Really ? even reader access ? :O – coding Mar 31 '23 at 10:03
  • @coding, yes. Not having appropriate role is throwing the error `The principal does not have the required Synapse RBAC permission to perform this action. Required permission: Action: Microsoft.Synapse/workspaces/artifacts/read, Scope: workspaces/synapse3003` – Saideep Arikontham Mar 31 '23 at 10:07
  • thanks a lot for provided me that information, so I need to figure out why I'm getting that error :\ – coding Mar 31 '23 at 10:15
  • This might have something to do with the token or the way it is generated. Try other token generation methods. As commented, if you think the solution might be helpful, accept it as solution so it might help other community members. – Saideep Arikontham Mar 31 '23 at 10:20
  • Sure, thanks ! just a last question, regarding app, did you give some API permission ? or you just created the app, picked up the client id and client secret and put the app as a Synapse Administrator role ? – coding Mar 31 '23 at 10:23
  • Just those steps. Created an app registration, used the tenant id, client id and client secret. Added this app registration in my azure synapse studio (as mention in the NOTE) with azure synapse administrator role (synapse artifact user is working as well). – Saideep Arikontham Mar 31 '23 at 10:25
  • Ok, thanks once again – coding Mar 31 '23 at 10:41