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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
arberceni
Frequent Visitor

Add user to group through REST API does not work C#

Hello everyone,

 

I am trying to share a workspace with a user outside my organization by using PowerBI .NET SDK (C#).

 

I am using the following code:

 

 

 

Invitation invitation = new Invitation{
                            InvitedUserEmailAddress = "[email protected]",
                            InviteRedirectUrl = "<url>",
                            SendInvitationMessage = true,                            
                        };

var res = await graphClient.Invitations.Request().AddAsync(invitation);

GroupUser gu = new GroupUser("Viewer", "[email protected]", null, "[email protected]", PrincipalType.User);

await client.Groups.AddGroupUserAsync(Guid.Parse(<workspace_id>), gu);

 

 

 

No errors are thrown from this code and the guest user DOES appear in the admin portal as well as in the "access" pane of the workspace.

arberceni_0-1603998979345.png

 

However, the problem is that the user this workspace is shared with ([email protected]) is not able to see the workspace or the reports inside the workspace. If the same process is done through the UI it works perfectly.

arberceni_1-1603999092684.png

 

Has anyone had the same problem?

 

Regards,

Arber

1 ACCEPTED SOLUTION
arberceni
Frequent Visitor

Hello all,

 

In case someone else is looking for a solution to this problem:

When inviting a guest user with e-mail address: [email protected], their username in the new new tenant will be:

username_tenant.onmicrosoft.com#EXT#@[email protected] and this username has to be used when creating a GroupUser object. (newtenant is the tenant from where the workspace was shared). So the code becomes:

 

 

Invitation invitation = new Invitation{
                            InvitedUserEmailAddress = "[email protected]",
                            InviteRedirectUrl = "<url>",
                            SendInvitationMessage = true,                            
                        };

var res = await graphClient.Invitations.Request().AddAsync(invitation);

GroupUser gu = new GroupUser("Viewer", "username_tenant.onmicrosoft.com#EXT#@newtenant.onmicrosoft.com", null, "username_tenant.onmicrosoft.com#EXT#@newtenant.onmicrosoft.com", PrincipalType.User);

await client.Groups.AddGroupUserAsync(Guid.Parse(<workspace_id>), gu);

 

 

Hope this helps anyone else!

 

Regards,

Arber

View solution in original post

3 REPLIES 3
arberceni
Frequent Visitor

Hello all,

 

In case someone else is looking for a solution to this problem:

When inviting a guest user with e-mail address: [email protected], their username in the new new tenant will be:

username_tenant.onmicrosoft.com#EXT#@[email protected] and this username has to be used when creating a GroupUser object. (newtenant is the tenant from where the workspace was shared). So the code becomes:

 

 

Invitation invitation = new Invitation{
                            InvitedUserEmailAddress = "[email protected]",
                            InviteRedirectUrl = "<url>",
                            SendInvitationMessage = true,                            
                        };

var res = await graphClient.Invitations.Request().AddAsync(invitation);

GroupUser gu = new GroupUser("Viewer", "username_tenant.onmicrosoft.com#EXT#@newtenant.onmicrosoft.com", null, "username_tenant.onmicrosoft.com#EXT#@newtenant.onmicrosoft.com", PrincipalType.User);

await client.Groups.AddGroupUserAsync(Guid.Parse(<workspace_id>), gu);

 

 

Hope this helps anyone else!

 

Regards,

Arber

ScottStauffer
Advocate I
Advocate I

I wonder if this "How to" in Microsoft docs would help?

https://docs.microsoft.com/en-us/power-bi/admin/service-admin-azure-ad-b2b

 

Hello Scott,

 

Thank you for your reply!

The "How to" you shared describes how the sharing is done through the user interface and I can confirm that it works as expected. The problem is that when I try to perform the same steps through the REST API it doesn't work.

 

Regards,

Arber

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.