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 :
Customer experience | Sales, Customer Insights,...
Suggested answer

check if user exists , if not exists create and assign a specific role c# plugin

(0) ShareShare
ReportReport
Posted on by 150

Dear All,

                  i want to check a user with DomainName, if user exists then assign a role, if user not exists create it first then assign a specific Role

any help will be appreciated,

thank you all. 

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: check if user exists , if not exists create and assign a specific role c# plugin

    Hi,

    You can use below code to check if user exist based on Domain Name.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">

     <entity name="systemuser">

       <attribute name="fullname" />

       <attribute name="businessunitid" />

       <attribute name="title" />

       <attribute name="address1_telephone1" />

       <attribute name="positionid" />

       <attribute name="systemuserid" />

       <order attribute="fullname" descending="false" />

       <filter type="and">

         <condition attribute="domainname" operator="eq" value="USER DOMAIN NAME" />

       </filter>

     </entity>

    </fetch>

    Use below code to associate role

    // Find the role.

                   QueryExpression query = new QueryExpression

                   {

                       EntityName = Role.EntityLogicalName,

                       ColumnSet = new ColumnSet("roleid"),

                       Criteria = new FilterExpression

                       {

                           Conditions =

                               {

                                   new ConditionExpression

                                   {

                                       AttributeName = "name",

                                       Operator = ConditionOperator.Equal,

                                       Values = {_givenRole}

                                   }

                               }

                       }

                   };

                   // Get the role.

                   EntityCollection roles = _serviceProxy.RetrieveMultiple(query);

                   if (roles.Entities.Count > 0)

                   {

                       Role salesRole = _serviceProxy.RetrieveMultiple(query).Entities[0].ToEntity<Role>();

                       Console.WriteLine("Role {0} is retrieved for the role assignment.", _givenRole);

                       _roleId = salesRole.Id;

                       // Associate the user with the role.

                       if (_roleId != Guid.Empty && _userId != Guid.Empty)

                       {

                           _serviceProxy.Associate(

                                       "systemuser",

                                       _userId,

                                       new Relationship("systemuserroles_association"),

                                       new EntityReferenceCollection() { new EntityReference(Role.EntityLogicalName, _roleId) });

                           Console.WriteLine("Role is associated with the user.");

                       }

    Use below code to create user in CRM Onpremise

    Entity user=new Entity(""systemuser);

    user["domainname"]=DOMAIN NAME;

    //map mandatory fields as per your data

    service.Create(user);

    If found helpful, Please mark my answer verified.

  • erhan.keskin Profile Picture
    2,253 on at
    RE: check if user exists , if not exists create and assign a specific role c# plugin

    Hi,

    This would help?

    community.dynamics.com/.../add-users-to-crm-online-programmatically

    and/or

    www.inogic.com/.../

    Regards,

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 164

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 121 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans