locked
Possibility of setting values for PartyList & regarding (C#)? RRS feed

  • Question

  • Is there is possible to set the values for partylist & regarding Lookups though C#(plugin)? If yes,please explain how ?
    Tuesday, March 29, 2016 5:52 AM

All replies

  • Dear Santhosh,

    Just curious what have you tried before you asked question? I know how to populate ActivityParty fields and Regarding lookup but just as an experiment I tried to look for information online. I opened search engine and literally typed following - microsoft crm set activity party C#. The first one article described everything that was required. Provided approach was relevant to EarlyBinding so I assumed that you wanted to use LateBinding instead. I returned back to search engine and at the same results page I found following urls:

    http://stackoverflow.com/questions/23389031/create-activityparty-in-crm-without-early-bound-entities

    http://mileyja.blogspot.com/2012/02/create-email-activity-in-microsoft.html

    Those urls described how to work with Activity Party fields using late binding approach.

    So I'm just curious what kind of issues you have faced, what have you tried to do?


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Tuesday, March 29, 2016 6:51 AM
    Moderator
  • Hi Andrii Butenko,

    Thanks for your response.

    First of all I'm a Beginner,I have some difficulties to work on. 

    My requirement is simple. I am trying  to create a new email record by using the GUID of existing email record.Just I need to populate the fields such as To,From,CC,Subject and attachments from the existing email record to new one.

    As per my understanding I have done some steps,

    1)I have created an email record.

    2)I got the GUID of the email record which I have created.I took this email record as the source email.

    (3) Now I need to retrieve the fields from this source email and need to set it for new email record.(yet to do)

    Note:

    I am not sure about these steps.If it is correct Please tell me how to do the 3rd step else please provide me some alternatives.

    Tuesday, March 29, 2016 7:11 AM
  • Ok. What code have you tried to use to retrieve fields from source email? Here is sample that demonstrates how to retrieve record:

    https://msdn.microsoft.com/ru-ru/library/gg334754.aspx

    https://msdn.microsoft.com/en-us/library/gg328416.aspx

    This thread have a sample that shows how to work exactly with ActivityParty field.

    I want to warn you - I will not give you any single line of code until I will see that you are trying to do something but asking questions. You will have to write everything with your own. But I can give you articles that demonstrate examples.


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Tuesday, March 29, 2016 7:19 AM
    Moderator
  • Hi,

    I'm not asking you to give me the code. I have just said my concerns.

    Code:

                                                           

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Client;
    namespace EmailPreCreatePlugin
    {
        public class EmailClass : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                //Extract the tracing service for use in debugging sandboxed plug-ins.
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                //</snippetFollowupPlugin1>

                //<snippetFollowupPlugin2>
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
                    //</snippetFollowupPlugin2>

                    // Verify that the target entity represents an account.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName != "email")
                        return;

                    try
                    {

                            if (entity.Id.Equals(new Guid("61872D6E-ADF4-E511-80ED-000D3A120DB1"))) // specific test account
                            {
                                // Create a email activity to follow up with the account customer in 7 days. 
                                Entity createemail = new Entity("email");

                                createemail["subject"] = "Send e-mail to the new customer.";
                                createemail["description"] = "new test mail ";


                                createemail["regardingobjectid"] = entity.ToEntityReference();

                                //<snippetFollowupPlugin4>
                                // Obtain the organization service reference.
                                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                                //</snippetFollowupPlugin4>

                                // Create the task in Microsoft Dynamics CRM.
                                tracingService.Trace("EmailPlugin: Creating the email activity.");
                                service.Create(createemail);
                            }
                    }
                    //<snippetFollowupPlugin3>
                    catch (System.ServiceModel.FaultException<OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the EmailPlugin plug-in.", ex);
                    }
                    //</snippetFollowupPlugin3>

                    catch (Exception ex)
                    {
                        tracingService.Trace("EmailPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }
        }
    }

    Note: I got this code from some site and am trying this.

    Tuesday, March 29, 2016 7:30 AM
  • Based on this code I assume that you try to handle create/update of an email record.

    After that you try to guess Id of an email that will be created (that's not good because it is random) so I believe it will be almost impossible to pass if (entity.Id.Equals(new Guid("61872D6E-ADF4-E511-80ED-000D3A120DB1"))) check. I would suggest to remove this check.

    Furthermore, you try to create an email and you try to populate regarding field with reference to an email that will fail because of platform validation so I suggest to remove this line.

    Also I don't see any line where you try to get ActivityParty fields from source email.

    Also please mention what message you try to handle - is it Create, Update or Send?


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Tuesday, March 29, 2016 7:49 AM
    Moderator
  • Thank you.

    I would do the modifications which you have said.

    BTW am doing for On "Create" of Email.

    Tuesday, March 29, 2016 8:29 AM
  • I have tried this code for retrieving the ActivityParty fields.But its not working.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Client;

    namespace EmailPreCreatePlugin
    {
        public class EmailClass : IPlugin
        {
            private Guid emailId;

            public void Execute(IServiceProvider serviceProvider)
            {
                try
                {
                    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        if (entity.LogicalName == "email")
                        {
                            if (entity.Attributes.Contains("new_parentrequestexists") == false)
                                return;
                            else
                            {
                                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                                //Entity createemail = new Entity("email");
                                Entity email = service.Retrieve("email", emailId, new ColumnSet(true));
                                EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
                                EntityCollection cc = email.GetAttributeValue<EntityCollection>("cc");
                                if (to != null) { Console.WriteLine("Records inside To: " + to.Entities.Count.ToString()); }
                                if (cc != null) { Console.WriteLine("Records inside Cc: " + cc.Entities.Count.ToString()); }
                                //service.Create(createemail);
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message.ToString());
                }
            }
          }
    }

    Tuesday, March 29, 2016 11:22 AM
  • 1. I don't see any line where you assign some value to emailId. You declare it, don't assign anything and after you try to use it. That's obvious error.

    2. Why do you need to retrieve an email? If you handle Create message then all the populated fields would be present in entity.

    3. "Code not working" can be interpret in many ways - VS doesn't compile it, VS compile it but I can't register a plugin, VS compile it and it is possible to register a step but during execution code throws an error. Unfortunately I'm not magician and I don't have extrasensory power so I don't know what you meant. Next time please be more specific.


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Tuesday, March 29, 2016 11:45 AM
    Moderator
  • Thank you.
    Tuesday, March 29, 2016 11:52 AM
  • From this code I can populate the From,To & CC fields as "System User Info"

    How can I set the account or contact info into these fields ?

    Code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Client;
    namespace EmailPreCreatePlugin
    {
        public class EmailClass : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
                if (context.InputParameters.Contains("Target"))
                {
                    if (context.InputParameters["Target"] is Entity)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        if (entity.LogicalName == "email")
                        {
                            try
                            {
                                Entity createemail = new Entity("email");
                                Entity fromParty = new Entity("activityparty");                          
                                Entity toParty = new Entity("activityparty");
                                Entity cc = new Entity("activityparty");
                                fromParty["partyid"] = new EntityReference("systemuser", context.UserId);
                                toParty["partyid"] = new EntityReference("systemuser", context.UserId);
                                cc["partyid"] = new EntityReference("systemuser", context.UserId);
                                createemail["from"] = new Entity[] { fromParty };
                                createemail["to"] = new Entity[] { toParty };
                                createemail["cc"] = new Entity[] { cc };
                                createemail["subject"] = "Create Email - " + DateTime.Now.ToString();
                                createemail["description"] = "Email is created ";
                                /*EntityCollection fromCollection = (EntityCollection)entity["from"];
                                if (fromCollection != null && fromCollection.Entities.Count > 0)
                                {
                                    Entity fromParty1 = fromCollection[0]; //activityparty entity
                                    EntityReference fromRef = (EntityReference)fromParty1["partyid"]; //actual entity ref
                                    entity["regardingobjectid"] = fromRef;
                                }*/
                                if (context.OutputParameters.Contains("id"))
                                {
                                    createemail["parentcustomerid"] =
                                        new EntityReference { Id = new Guid(context.OutputParameters["id"].ToString()), LogicalName = entity.LogicalName };
                                }
                                IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                                IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);
                                client.Create(createemail);
                            }

                            catch (Exception e)
                            {
                            }
                        }
                        else
                            return;
                    }
                }
            }

        }
    }


    Tuesday, March 29, 2016 2:03 PM
  • this piece of code:

    fromParty["partyid"] = new EntityReference("systemuser", context.UserId);
    toParty["partyid"] = new EntityReference("systemuser", context.UserId);
    cc["partyid"] = new EntityReference("systemuser", context.UserId);

    you should put the account or the contact id and set the logical name to account or contact instead of systemuser, for example:

    fromParty["partyid"] = new EntityReference("account", accountId);
    toParty["partyid"] = new EntityReference("contact", contact1Id);
    cc["partyid"] = new EntityReference("contact", contact2Id);


    My blog: www.crmanswers.net - CRM Theme Generator

    Tuesday, March 29, 2016 4:21 PM
  • @Santosh Deepak,

    I asked not to create threads with nesting content. What's not clear with my request?

    @Guido

    Good luck with this thread. I give up answering :)


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses


    Tuesday, March 29, 2016 6:09 PM
    Moderator
  • Howdy,

    From my coding I'm trying to retrieve some fields from the email record. I'm writing a plugin on Create of Email. 

    I need to create the new email record with those retrieved values. Pls help me.

    If I have create a new email record am not sure the whether my fields are getting retrieved.

    code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Client;
    using Microsoft.Xrm.Sdk.Client;
    namespace EmailPreCreatePlugin
    {
        public class EmailClass : IPlugin
        {
            
            public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
                if (context.InputParameters.Contains("Target"))
                {
                    if (context.InputParameters["Target"] is Entity)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        if (entity.LogicalName == "email")
                        {
                            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                            try
                            {
                                Guid emailguid = new Guid("FFB02934-38F6-E511-80EF-000D3A115C5B");
                                ColumnSet cols = new ColumnSet(new String[] { "from", "to", "cc", "subject" });
                                var email = service.Retrieve("email", emailguid, cols);
                                
                                if(email != null)
                                {
                                    if(email.Attributes.Contains("new_parentrequestexists")== true)
                                    {
                                        service.Create(email);
                                    }
                                }
                               
                                                          
                                
                            }
                            catch (System.ServiceModel.FaultException<OrganizationServiceFault> ex)
                            {
                                throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                            }

                            catch (Exception ex)
                            {
                                tracingService.Trace("MyPlugin: {0}", ex.ToString());
                                throw;
                            }
                        }
                        else
                            return;
                    }
                }
            }       
        }
    }


    Thursday, March 31, 2016 8:59 AM