web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Dynamics 365 Dev, How to add privilege to a role by c# code?

(0) ShareShare
ReportReport
Posted on by 91

Hi everyone,

I need to create a workflow to automate the operations for adding and removing privileges on certain roles. usually, user A hasn't write access to customer entity "entityM", when user A demand an edit right for entityM, the workflow will be trigged to add write privilege  to entityM of his security role, is it possible to do it? I have try the message AddPrivilegesRoleRequest but it needs the privilegeId, the privilege doesnt exist before adding to the role, anyone can help me? thanks

Song

I have the same question (0)
  • Suggested answer
    Emre GULCAN Profile Picture
    2,379 on at
    RE: Dynamics 365 Dev, How to add privilege to a role by c# code?

    Hi Song,

    10 seconds maybe not normal, but probably this duration includes "OrganizationService init" and also as you said it depends on network.

    Second point, if you use my method and send privilege name as parameter, my method check all privileges before process your query, so there are 2 steps inside it,

    - check privilege

    - add privilege to security role

    also, MSCRM runs different queries when you add privilege to role.

  • Song Profile Picture
    91 on at
    RE: Dynamics 365 Dev, How to add privilege to a role by c# code?

    Hi Emre,

    I have just retried it and it works well, is it normal it taks several seconds (8 or 10) to complete this? Maybe it is network problem.

    Thanks so much and have a nice day

    Song

  • Suggested answer
    Emre GULCAN Profile Picture
    2,379 on at
    RE: Dynamics 365 Dev, How to add privilege to a role by c# code?

    Hi Song,

    "prvWrite" is default privilege, so it's not normal. Can you copy all exception details here.

    By the way, this privilege name ("prvWritenew_mycustomentity") is just example, you must change redline (new_mycustomentity) with your entity name.

    Please look at https://msdn.microsoft.com/en-us/library/hh547441.aspx for all system pre-defined privlege names

  • Song Profile Picture
    91 on at
    RE: Dynamics 365 Dev, How to add privilege to a role by c# code?

    Hi Emre,

    Thanks for your reply, I have tried this but there is a error message ''privilegeName' privilege not found.

    is it normal? I used well the privilege name "prvWritenew_mycustomentity", in fact, this privilege doesnt exist in the privilegebase before

  • Verified answer
    Emre GULCAN Profile Picture
    2,379 on at
    RE: Dynamics 365 Dev, How to add privilege to a role by c# code?

    Hi,

    Please check code blocks on https://github.com/emregulcan/xrmlibrary.2016/blob/master/XrmLibrary.EntityHelpers/Security/RoleHelper.cs#L151-L251 

    You can use my helper library that called XrmLibrary (2015 or 2016) or directly copy related code block from Github.

    You need to add privilege to role and assign a role to user.

    You can use simple code below;

    RoleHelper roleHelper = new RoleHelper(organizationService);
    roleHelper.AddPrivilege(Guid.Parse("SECURITY_ROLE_GUID"), "PRIVILEGE_NAME", PrivilegeDepth.Global);


    You can find system predefined privileges names on https://msdn.microsoft.com/en-us/library/hh547441.aspx , if you need to add privilege for custom entity, you can use this format "prvCreatenew_mycustomentity"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans