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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
g3kuser
Helper I
Helper I

Find notebook executing user

I am trying to find notebook executing user using mssparkutils.env.getUserName() method.
This works fine when executing user is named user but fails when the user is SPN as the notebook owner is SPN.
 
Does this method not return user details if it is SPN?
 
Thanks,
 
Gayatri
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @g3kuser,

Thank you for reaching out in Microsoft Community Forum.

Please follow below steps to Handle mssparkutils.env.getUserName() with SPNs;

1.mssparkutils.env.getUserName() works for named users, but does not reliably return SPN names.

2.SPNs are service accounts, and not treated as interactive users, so this method may return null or empty.

3.Pass the user or SPN name as a parameter to the notebook when triggered via pipeline or API.

4.Use mssparkutils.env.getExecutionId() to trace the notebook run or log trigger identity externally.

Please continue using Microsoft community forum.

If you found this post helpful, please consider marking it as "Accept as Solution" and give it a 'Kudos'. if it was helpful. help other members find it more easily.

Regards,
Pavan.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @g3kuser,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please "Accept  as  Solution" and give a 'Kudos' so other members can easily find it.

Thank you,
Pavan.

nilendraFabric
Super User
Super User

Hi @g3kuser 

mssparkutils.env.getUserId()  might works more reliably and can return the ID even when the notebook is executed by a service principal

Thanks. My reason to look at userName is to identify if the user is named user or SPN and based on that initialize credential class to call Fabric APIs from notebook. In case of named user it returns the entire email addres and I can easily put a regex to verify it. Should I assume it will always return None/empty/exception if the user is SPN/MI?

 

Thanks,

 

Gayatri

Anonymous
Not applicable

Hi @g3kuser,

Thank you for reaching out in Microsoft Community Forum.

Thank you @nilendraFabric  for the helpful response.

mssparkutils.env.getUserName() will return a valid email address for named interactive users

Use a simple check like this to differentiate:

user_name = mssparkutils.env.getUserName()

if user_name and "@" in user_name:
# Named user
else:
# SPN or MI

Please continue using Microsoft community forum.

If you found this post helpful, please consider marking it as "Accept as Solution" and give it a 'Kudos'. if it was helpful. help other members find it more easily.

Regards,
Pavan.

Anonymous
Not applicable

Hi @g3kuser,

Thank you for reaching out in Microsoft Community Forum.

Please follow below steps to Handle mssparkutils.env.getUserName() with SPNs;

1.mssparkutils.env.getUserName() works for named users, but does not reliably return SPN names.

2.SPNs are service accounts, and not treated as interactive users, so this method may return null or empty.

3.Pass the user or SPN name as a parameter to the notebook when triggered via pipeline or API.

4.Use mssparkutils.env.getExecutionId() to trace the notebook run or log trigger identity externally.

Please continue using Microsoft community forum.

If you found this post helpful, please consider marking it as "Accept as Solution" and give it a 'Kudos'. if it was helpful. help other members find it more easily.

Regards,
Pavan.

Thank you. Yes I implemented the same with the additional check on userName against email regex. This is added only to make it future proof. If MS changes the definition of the method and ends up returning a actual SPN name instead of exception getting raised as of today email regex will help in differentiating.

 

Thanks,

 

Gayatri 

Helpful resources

Announcements
Fabric July 2025 Monthly Update Carousel

Fabric Monthly Update - July 2025

Check out the July 2025 Fabric update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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