Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
igorkk
Frequent Visitor

Is there a function to check the workspace name / ID in Fabric notebook?

Hi fabricators, is there a function by which we can see the name of the workspace or the workspace ID in fabric notebook?

1 ACCEPTED SOLUTION
v-nikhilan-msft
Community Support
Community Support

Hi @igorkk 
Thanks for using Fabric Community.
You can use the below codes:

vnikhilanmsft_0-1711942737928.png

Hope this helps. Please let me know if you have any further questions.

View solution in original post

16 REPLIES 16
David_Bridge
Frequent Visitor

Although marked as solved, it really isn't, so here's a solution...


How to get the workspace name when running a notebook. Use sempy (semantic link)

Here’s the function page help

https://learn.microsoft.com/en-us/fabric/data-science/semantic-link-semantic-functions

 

Install like this

%pip install -U semantic-link

 

Then run this...

 

 

 

import sempy.fabric as fabric

# Get the id of this notebooks workspace
workspace_id = fabric.get_notebook_workspace_id()  
# You can also get this with fabric.get_workspace_id()

# From all workspaces, filter by Id and then just get the Name
workspaces = fabric.list_workspaces()
workspace_row = workspaces[workspaces.Id == workspace_id]
workspace_name = workspace_row['Name']

 

 

 

Edit: In the above code I put a comment to say that you could also use fabric.get_workspace_id(). Due to a possible bug in Fabric, you cannot use this, and should only use fabric.get_notebook_workspace_id(), like I did in the above code.
Thanks to Element115 for mentioning this. See replies for details.

There's a simpler, one line, way to get the workspace name. Behold!!!  😁

 

 

import sempy.fabric as fabric

workspace_name = fabric.resolve_workspace_name()

 

Nice find. Sempy is evolving nicely and so too is notebookutils (mssparkutils) with another one-liner.

notebookutils.mssparkutils.env.getWorkspaceName()


or 

notebookutils.runtime.context.get("currentWorkspaceName")



However, something I just noticed and don't know if it's a bug or intended to be such.  Here's the context.  

 

2 workspaces: SPARK and IVDS.  1 notebook:  nb_test.   Current workspace == SPARK.  

I created nb_test in workspace IVDS.  Later I saved it as a copy to workspace SPARK.  

 

I am now working in SPARK-->nb_test.

 

This 

workspace_id = fabric.get_notebook_workspace_id() 

returns 'SPARK'.

 

But this

workspace_id = fabric.get_workspace_id()

returns 'IVDS' or the workspace where the copy's original was first created.

 

Go figure.

I agree that this looks like a bug.

 

As your code references id's, the return values will be workspace GUIDs, not names, but I assumed you meant the ids are referencing different workspaces and was intrigued so I tried it out, and I found exactly the same thing. fabric.get_workspace_id() gets the pre-copy workspace id. and if you used it to subsequently get the workspace name from a lookup using it, the result would be incorrect. get_notebook_workspace_id() returns the correct value so probably a better choice. 

Rather more worrying, I also note that if you deploy with deployment pipelines rather than using Save a copy, you still get this behaviour.
(tested on 2024-09-24)

 

To get the workspace name, using notebookutils.mssparkutils is probably easier, but this library fdoesn't have a method to get the workspace_id

 

mssparkutils.env.getWorkspaceName()

It's not my code but your code btw 😁, I lifted from your older post.  

I didn't need to do any pip install personally, also I want to return it as a string for my purposes. Hope this helps speed someone else up!

 

 

import sempy.fabric as fabric

def get_current_workspace_name():
    # Get the id of this notebook's workspace
    workspace_id = fabric.get_notebook_workspace_id()
    # From all workspaces, filter by Id and then just get the Name
    workspaces = fabric.list_workspaces()
    workspace_row = workspaces[workspaces.Id == workspace_id]
    workspace_name = workspace_row['Name'].values[0]  # Extract the first value as a string
    return workspace_name

# Usage example
workspace_name = get_current_workspace_name()
print(f'name: {workspace_name}')

 

 

 

v-nikhilan-msft
Community Support
Community Support

Hi @igorkk 
Thanks for using Fabric Community.
You can use the below codes:

vnikhilanmsft_0-1711942737928.png

Hope this helps. Please let me know if you have any further questions.

Due to a change to notebookutils, these snippets work now:

 

 

fabric_workspace_id = notebookutils.runtime.context.get("currentWorkspaceId") # current workspace ID
fabric_workspace_name = notebookutils.runtime.context.get("currentWorkspaceName") # current workspace name

 

 

Could you please share the link where this is documented? 

This works as well: `mssparkutils.env.getWorkspaceName() # current workspace name`

Hi,

 

I tried this and it works as expected for the Id, but when I call: 

spark.conf.get("trident.workspace.name")
I get the error: 
Py4JJavaError: An error occurred while calling o308.get. : java.util.NoSuchElementException: trident.workspace.name at org.apache.spark.sql.errors.QueryExecutionErrors$.noSuchElementExceptionError(QueryExecutionErrors.scala:2138)
 
@v-nikhilan-msft Can you help? Thanks 🙂

Hi @igorkk 
We haven’t heard from you on the last response and was just checking back to see if your query got resolved. Otherwise, will respond back with the more details and we will try to help.
Thanks

Hi @igorkk 
We haven’t heard from you on the last response and was just checking back to see if your query got resolved. Otherwise, will respond back with the more details and we will try to help.
Thanks

Hi @igorkk 
Glad that your query got resolved. Please continue using Fabric Community for any help regarding your queries.

Helpful resources

Announcements
Oct Fabric Update Carousel

Fabric Monthly Update - October 2024

Check out the October 2024 Fabric update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.