Forum Discussion

BartHuls's avatar
BartHuls
Helper I
1 year ago
Solved

How do set the Row-level Security roles within the PowerBiClient SDK

All,
WithIn the PowerBiClient SDK I want to update / set  a Row-level Security roles.
The Group will then be visible in underlying screen.

Thanks in advance

 

 

  • This cannot be done by the PowerBIClient SDK.

    For now I have the following solution. It's not nice, but it works. based on the Microsoft.AnalysisServices.Tabular lib.

    public async Task SetRowLevelSecurity(string groupName, string datasetId, Guid domainGroupId, string domainName, string tenantId)
        {
            _authenticationResult ??= await GetAccessToken();
    
            var dataSource = $"powerbi://api.powerbi.com/v1.0/{domainName}/{groupName}";
    
            using var server = new Server();
            server.Connect($"DataSource={dataSource};Password={_authenticationResult.AccessToken}");
    
            var database = server.Databases.Find(datasetId) ?? throw new NullReferenceException(_cannotFindDataSet);
    
            // Cast is needed otherwise the loading does not work
            var model = (Model)database.Model;
            var role = model.Roles.Find(<RoleName>);
            if (role is not null)
            {
                var memberName = $"obj:{domainGroupId}@{tenantId}";
                var member = role.Members.FirstOrDefault(m => m.Name.Contains(memberName, StringComparison.OrdinalIgnoreCase));
                if (member is not null)
                {
                    return;
                }
    
                role.Members.Add(new ExternalModelRoleMember
                {
                    MemberName = memberName,
                    MemberType = RoleMemberType.Group,
                    IdentityProvider = "AzureAD"
                });
                database.Model.SaveChanges();
    
                logger.LogInformation("Row Level Security set for {GroupId} for dataset {DataSetId}", domainGroupId, datasetId);
            }
        }



5 Replies

  • v-csrikanth's avatar
    v-csrikanth
    Community Support

    Hi BartHuls 
    Please do raise your new thoughts in the ideas forum and upvote it so that it will be adressed by the Microsoft internal team.

    Best Regards,
    Community Support Team _ C Srikanth.

  • This cannot be done by the PowerBIClient SDK.

    For now I have the following solution. It's not nice, but it works. based on the Microsoft.AnalysisServices.Tabular lib.

    public async Task SetRowLevelSecurity(string groupName, string datasetId, Guid domainGroupId, string domainName, string tenantId)
        {
            _authenticationResult ??= await GetAccessToken();
    
            var dataSource = $"powerbi://api.powerbi.com/v1.0/{domainName}/{groupName}";
    
            using var server = new Server();
            server.Connect($"DataSource={dataSource};Password={_authenticationResult.AccessToken}");
    
            var database = server.Databases.Find(datasetId) ?? throw new NullReferenceException(_cannotFindDataSet);
    
            // Cast is needed otherwise the loading does not work
            var model = (Model)database.Model;
            var role = model.Roles.Find(<RoleName>);
            if (role is not null)
            {
                var memberName = $"obj:{domainGroupId}@{tenantId}";
                var member = role.Members.FirstOrDefault(m => m.Name.Contains(memberName, StringComparison.OrdinalIgnoreCase));
                if (member is not null)
                {
                    return;
                }
    
                role.Members.Add(new ExternalModelRoleMember
                {
                    MemberName = memberName,
                    MemberType = RoleMemberType.Group,
                    IdentityProvider = "AzureAD"
                });
                database.Model.SaveChanges();
    
                logger.LogInformation("Row Level Security set for {GroupId} for dataset {DataSetId}", domainGroupId, datasetId);
            }
        }