Assign Privileges to a Security Role by code C#
If for any reason you need to assign a specific Privileges to a Security Role, you can use this code :
// The ID of the role without the privileges
Guid RoleId = new Guid("7ad78e66-a4df-ee11-904c-000d3a8312b6");
// The privileges to add to the role
string[] privileges = { "prvAllowTDSAccess" };
// Retrieve the privileges
var query = new QueryExpression
{
EntityName = "privilege",
ColumnSet = new ColumnSet("privilegeid", "name")
};
query.Criteria.AddCondition("name", ConditionOperator.In, privileges);
DataCollection privilegeRecords =
service.RetrieveMultiple(query).Entities;
// Define a list to hold the RolePrivileges we'll need to add
List rolePrivileges = new List();
//Populate the RolePrivileges parameter
foreach (Entity privilege in privilegeRecords)
{
RolePrivilege rolePrivilege = new RolePrivilege(
(int)PrivilegeDepth.Global,
privilege.GetAttributeValue("privilegeid"));
rolePrivileges.Add(rolePrivilege);
}
// Prepart the request
AddPrivilegesRoleRequest request = new AddPrivilegesRoleRequest()
{
Privileges = rolePrivileges.ToArray(),
RoleId = RoleId
};
// Send the request
service.Execute(request);
Published on:
Learn moreWe can help you with Assign Privileges to a Security Role by code C#
If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.
Related posts
Unmanaged developer environments are ending: what to do before December 15
Unmanaged developer environments can no longer be created, are suspended on December 15, 2026 and deleted in January. Who is affected and how ...
Backfill Dataverse prompt columns on existing records
Prompt columns only ran on new or updated rows. A bulk option now fills existing records as a system job: the two settings, the steps and the ...
Reveal a masked Dataverse column with Dataverse.Unmask
A new Power Fx function lets authorised users of a canvas app reveal one masked Dataverse value on demand, while column security stays in char...
Generative pages in model-driven apps: a practical guide
What generative pages are, where they stand in November 2026, the two ways to build them, the code they produce, and the limits to plan for.
Big Update — CRMJetty Dynamics 365 Portal Now Comes with Powerful New Integrations
Enterprise portals are evolving rapidly. They are no longer mere portals through which CRM records can be viewed. Portals are now expected to ...
Power Apps Code Apps in Practice: Recreating Model-Driven App Patterns
Power Apps Code Apps went from preview to general availability in record time. With that, Microsoft has sent a clear signal that it is taking ...
Use XRM type definition in your Javascript file
We implement Javascript webresource for form scripting (form onload, field on change,...). Our code need to follow the official document Xrm o...
Dataverse Functions Deep Dive
Microsoft released in 2024 Dataverse low-code plugins. The promise was great : writing plugins using low-code tools. No need to have developme...