locked
on Creation of User, i need to write a plugin to create a contact with that user full name RRS feed

  • Question

  • on Creation of User, i need to write a plugin to create a contact with that user full name

    may i know how to do this in MS CRM 2013?

    thanks in advance....

    Please find below code:

              

     public void Execute(IServiceProvider sericeProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)sericeProvider.GetService(typeof(IPluginExecutionContext));

                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)sericeProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                if (context.InputParameters["Target"] is Entity && context.InputParameters.Contains("Target"))
                {
                    Entity User = context.InputParameters["Target"] as Entity;

                    string Username = User["fullname"].ToString();
                        Entity contact = new Entity("contact");
                        contact["lastname"] = Username;

                    service.Create(contact);

                }

                }

    i Resisted this plugin  in MS CRM by using plugin registration tool( message : CREATE ;Primary entity:Systemuser;on Post operation ) but it is not working for me ...


    • Edited by pavad Thursday, May 15, 2014 11:56 AM
    Thursday, May 15, 2014 11:55 AM

Answers

All replies


  • public void Execute(IServiceProvider sericeProvider)
            {
                IPluginExecutionContext context = 
                    (IPluginExecutionContext)sericeProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory factory = 
                    (IOrganizationServiceFactory)sericeProvider.GetService(typeof(IOrganizationServiceFactory));
    
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters["Target"] is Entity && context.InputParameters.Contains("Target"))
                {
                    Entity user = context.InputParameters["Target"] as Entity;
    
                    string username = user["fullname"].ToString();
    
                    Entity contact = new Entity("contact");
                    contact.Attributes.Add("lastname",username);
    
                    service.Create(contact);
    
                }
    
            }

    This code should work in CRM 2013.

    I have checked the CRM 2013 SDK for supported messages on SystemUser.

    Message Name Primary Entity Secondary Entity Message Availability Entity Supported Deployment
    Create SystemUser NULL Both Server
    SetState SystemUser NULL Both Server
    SetStateDynamicEntity SystemUser NULL Both Server
    Update SystemUser NULL Both Server


    Friday, May 16, 2014 11:26 AM
  • Because you are using CRM 2013, you can achieve this with a synchronous workflow, it's easier to implement and maintain.

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Friday, May 16, 2014 11:41 AM