locked
Set From and To Field in Email Activity RRS feed

  • Question

  • Hi experts i am Creating one Email Activity using SilverLight C# . The Problem is i am unable to set From and To fields. 

    I tried like below but it showing error.

    ActivityParty toParty = new ActivityParty { PartyId = new EntityReference(Contact.EntityLogicalName, _contactId) };

    email.To = new ActivityParty[] { toParty };

     

    Here error is at email.TO and  EntityReference(Contact.EntityLogicalName, _contactId)

    Please Suggest some solution

    Thank You.


    saroj

    Friday, October 12, 2012 12:55 PM

Answers

  • Hi Andrii Thank you for your precious time and reply. I tried as your solution. But still it is not working. I am doing it as below.

    private void CreateActivity()
            {
                Entity emailEntity = new Entity();
                DataService.EntityReference fromEntityRef = new DataService.EntityReference();
                fromEntityRef.LogicalName = "systemuser"; // I tried by replacing it with "activityparty" also
                fromEntityRef.Id = Guid.Parse(_systemUserId);
                Entity fromParty = new Entity();
                fromParty.LogicalName = "activityparty";
                fromParty["partyid"] = fromEntityRef;

                EntityCollection fromcollection = new EntityCollection();
                fromcollection.Entities = new ObservableCollection<Entity>();
                fromcollection.Entities.Add(fromParty);
                emailEntity["from"] = fromcollection;
                emailEntity["partyidname"] = fromParty;
                IOrganizationService service = ServerUtility.GetSoapService();
                service.BeginCreate(emailEntity, OnCreateComplete, service);               
            
            }
            private void OnCreateComplete(IAsyncResult result)
            {           
                Guid emailId = ((IOrganizationService)result.AsyncState).EndCreate(result);
               
            }     


    saroj

    Hello,

    It seems that you are not trying to do anything to make code work. I will provide last working sample of code and I will leave this thread.

                EntityReference entityRef = new EntityReference();
                entityRef.LogicalName = "contact";
                entityRef.Id = new Guid("78CD2C94-9A16-E211-8929-080027BC7342");
                EntityReference fromEntityRef = new EntityReference();
                fromEntityRef.LogicalName = "systemuser";
                fromEntityRef.Id = new Guid("D7C9CBD7-13F8-E111-B1A9-080027BC7342");
                Entity fromParty = new Entity();
                fromParty.LogicalName = "activityparty";
                fromParty["partyid"] = fromEntityRef;
                Entity toParty = new Entity();
                toParty["partyid"] = entityRef;
                toParty.LogicalName = "activityparty";
    
                Entity emailEntity = new Entity();
                emailEntity.LogicalName = "email";
    
                EntityCollection fromcollection = new EntityCollection();
                fromcollection.Entities = new System.Collections.ObjectModel.ObservableCollection<Entity>();
                fromcollection.Entities.Add(fromParty);
    
                emailEntity["from"] = fromcollection;
    
                EntityCollection tocollection = new EntityCollection();
                tocollection.Entities = new System.Collections.ObjectModel.ObservableCollection<Entity>();
                tocollection.Entities.Add(toParty);
    
    
                emailEntity["to"] = tocollection;
                emailEntity["directioncode"] = true;
                emailEntity["subject"] = "subject";
                emailEntity["description"] = "description";
                emailEntity["regardingobjectid"] = entityRef;
    
                IOrganizationService soapService = SilverlightUtility.GetSoapService();
                soapService.BeginCreate(emailEntity, OnCreateComplete, soapService);


    Freelance Developer for Dynamics CRM 4.0/2011

    Tuesday, October 16, 2012 10:50 AM
    Moderator

All replies

  • Try to replace lines

    PartyId = new EntityReference(Contact.EntityLogicalName, _contactId)

    with lines

    PartyId = new EntityReference()
    {
    	LogicalName = Contact.EntityLogicalName,
    	Id = _contactId
    }
    


    Freelance Developer for Dynamics CRM 4.0/2011

    Friday, October 12, 2012 1:14 PM
    Moderator
  • Hi Andrii, Thank you for your response. Actually  i am creating a new Email Activity. I am able to Add the body and Subject but unable to add From and To Field. How should i do it. I have tried many way but couldn't achieve this. The code you have suggested also getting error at Contact.EntityLogicalName.

    Please Guide me and give some solution.

    Thank you.


    saroj


    Friday, October 12, 2012 1:28 PM
  • Hi Saroj,

    Did you check : http://mahenderpal.wordpress.com/2012/03/02/send-email-through-code-using-late-bound/


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, October 12, 2012 2:00 PM
    Moderator
  • Try to use following code:

    PartyId = new EntityReference()
    {
    	LogicalName = "contact",
    	Id = _contactId
    }
    Have you ever tried to use search engine instead of asking question here?



    Freelance Developer for Dynamics CRM 4.0/2011



    Friday, October 12, 2012 2:06 PM
    Moderator
  • Hi Andrii, Yes I have Googled many. but the solution were didn't work. So i posted this question in Forum. And For your kind information, i never post question directly here before googling or searched in different source.

    Thank you.

     

    saroj

    Friday, October 12, 2012 3:31 PM
  • Hi Mahender, thank you for your response. I have already tried the Late bound approach but when i passed the entity name as parameter in Entity().

    Then it is showing error that no constructor take one Argument. What might be the problem.

    Please suggest.

    Thank You.


    saroj

    Friday, October 12, 2012 3:36 PM
  • Did you try to set logicalname instead of passing entity name in constructor

    Entity Fromparty = new Entity();

    Fromparty.LogicalName="activityparty";


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, October 12, 2012 3:47 PM
    Moderator
  • Hi Mahender thank you very much for your reply. Here i am trying as below but this time the exception is throwing . I am Pasting my code and the error. 

            

                    DataContextService.EntityReference entityRef = new DataContextService.EntityReference();
                    entityRef.LogicalName = "contact";               
                    entityRef.Id = Guid.Parse(_entity._uid);                              
                    DataContextService.EntityReference fromEntityRef = new DataContextService.EntityReference();
                    fromEntityRef.LogicalName = "systemuser";
                    fromEntityRef.Id = Guid.Parse(_systemUserId);          
                    Entity fromParty = new Entity();                
                    fromParty["partyid"] = fromEntityRef;
                    Entity toParty = new Entity();              
                    toParty["partyid"] = entityRef;

                    Entity emailEntity = new Entity();
                    emailEntity.LogicalName = "email";
                    emailEntity["from"] = new Entity[] { fromParty };
                    emailEntity["to"] = new Entity[] { toParty };
                    emailEntity["directioncode"] = true;
                    emailEntity["subject"] = subject;
                    emailEntity["description"] = descript;
                    emailEntity["regardingobjectid"] = entityRef;

                    IOrganizationService service = ServerUtility.GetSoapService();
                    service.BeginCreate(emailEntity, OnCreateComplete, service);

             private void OnCreateComplete(IAsyncResult result)
            {                       
                Guid emailId = ((IOrganizationService)result.AsyncState).EndCreate(result);            
                MessageBox.Show("Email Id :" + emailId);
            }

    ************The Exception is like below************

    There was an Error while trying to Serialize parameter. The InnerException Message was Type System.Collections.Generic.List[...]With data contract Name ArrayOfEntity:http://Schemas.microsoft.com/xrm/2011/Contracts is not expected. Add Any types  not known statically to the list of Knowntypes.

    Please suggest where i am getting Wrong.

    Thank You.


    saroj


    Monday, October 15, 2012 5:08 AM
  • hey Saroj,

    This is how we do it.

    Instead of using a array of Entities, use the EntityCollection.

    Entity theEmail = new Entity("email");
    
    EntityCollection _from = new EntityCollection();
    _from.EntityName = "activityparty";
    if (fromRef != null) {
       Entity f = new Entity("activityparty");
       f.Attributes.Add("partyid", fromRef);
       _from.Entities.Add(f);
    }
    
    theEmail.Attributes.Add("from", _from);

    Steven De Waele Software Engineer http://www.stevendewaele.be

    Monday, October 15, 2012 6:37 AM
  • It seems you are missing two below lines

    Try

                     fromParty.LogicalName = "activityparty";
                   

                   toParty.LogicalName = "activityparty";     
                


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, October 15, 2012 6:39 AM
    Moderator
  • Hi Mahender, i added above two line. Still the same Serialialization Exception is coming. Please suggest some other solution.

    Thank you for your precious time.


    saroj

    Monday, October 15, 2012 6:59 AM
  • HI Steven, when i tried your code it's getting error in " f.Attributes.Add()"  Method that the Add method can not take two arguement.

    Thank You


    saroj

    Monday, October 15, 2012 7:36 AM
  • Hello,

    Here is solution for your issue:

                EntityReference fromEntityRef = new EntityReference();
                fromEntityRef.LogicalName = "systemuser";
                fromEntityRef.Id = new Guid("D7C9CBD7-13F8-E111-B1A9-080027BC7342");
                Entity fromParty = new Entity();
                fromParty.LogicalName = "activityparty";
                fromParty["partyid"] = fromEntityRef;
    
                EntityCollection fromcollection = new EntityCollection();
                fromcollection.Entities = new System.Collections.ObjectModel.ObservableCollection<Entity>();
                fromcollection.Entities.Add(fromParty);
    
                emailEntity["from"] = fromcollection;
    


    Freelance Developer for Dynamics CRM 4.0/2011

    Monday, October 15, 2012 10:30 AM
    Moderator
  • Hi Saroj,

    You might wanna try it this way.

    var activityParty1 = new ActivityParty { PartyId = new EntityReference(Contact.EntityLogicalName, contacts[0].ContactId.Value), }; Email email = new Email

    { From = new ActivityParty[] { activityParty1 },

    To = new ActivityParty[] { activityparty2 },

    }


    Check out the sdk : Sample: Work with Activity Party Records 

    For more contacts, Create more activity parties and add them up in the array.

    Thanks

    Hope this helps.


    Haris Adil e-Bizsoft


    • Edited by Harispk Tuesday, October 16, 2012 7:42 AM
    Tuesday, October 16, 2012 7:37 AM
  • Hi Andrii Thank you for your precious time and reply. I tried as your solution. But still it is not working. I am doing it as below.

    private void CreateActivity()
            {
                Entity emailEntity = new Entity();
                DataService.EntityReference fromEntityRef = new DataService.EntityReference();
                fromEntityRef.LogicalName = "systemuser"; // I tried by replacing it with "activityparty" also
                fromEntityRef.Id = Guid.Parse(_systemUserId);
                Entity fromParty = new Entity();
                fromParty.LogicalName = "activityparty";
                fromParty["partyid"] = fromEntityRef;

                EntityCollection fromcollection = new EntityCollection();
                fromcollection.Entities = new ObservableCollection<Entity>();
                fromcollection.Entities.Add(fromParty);
                emailEntity["from"] = fromcollection;
                emailEntity["partyidname"] = fromParty;
                IOrganizationService service = ServerUtility.GetSoapService();
                service.BeginCreate(emailEntity, OnCreateComplete, service);               
            
            }
            private void OnCreateComplete(IAsyncResult result)
            {           
                Guid emailId = ((IOrganizationService)result.AsyncState).EndCreate(result);
               
            }     


    saroj

    Tuesday, October 16, 2012 9:21 AM
  • Hi Andrii Thank you for your precious time and reply. I tried as your solution. But still it is not working. I am doing it as below.

    private void CreateActivity()
            {
                Entity emailEntity = new Entity();
                DataService.EntityReference fromEntityRef = new DataService.EntityReference();
                fromEntityRef.LogicalName = "systemuser"; // I tried by replacing it with "activityparty" also
                fromEntityRef.Id = Guid.Parse(_systemUserId);
                Entity fromParty = new Entity();
                fromParty.LogicalName = "activityparty";
                fromParty["partyid"] = fromEntityRef;

                EntityCollection fromcollection = new EntityCollection();
                fromcollection.Entities = new ObservableCollection<Entity>();
                fromcollection.Entities.Add(fromParty);
                emailEntity["from"] = fromcollection;
                emailEntity["partyidname"] = fromParty;
                IOrganizationService service = ServerUtility.GetSoapService();
                service.BeginCreate(emailEntity, OnCreateComplete, service);               
            
            }
            private void OnCreateComplete(IAsyncResult result)
            {           
                Guid emailId = ((IOrganizationService)result.AsyncState).EndCreate(result);
               
            }     


    saroj

    Hello,

    It seems that you are not trying to do anything to make code work. I will provide last working sample of code and I will leave this thread.

                EntityReference entityRef = new EntityReference();
                entityRef.LogicalName = "contact";
                entityRef.Id = new Guid("78CD2C94-9A16-E211-8929-080027BC7342");
                EntityReference fromEntityRef = new EntityReference();
                fromEntityRef.LogicalName = "systemuser";
                fromEntityRef.Id = new Guid("D7C9CBD7-13F8-E111-B1A9-080027BC7342");
                Entity fromParty = new Entity();
                fromParty.LogicalName = "activityparty";
                fromParty["partyid"] = fromEntityRef;
                Entity toParty = new Entity();
                toParty["partyid"] = entityRef;
                toParty.LogicalName = "activityparty";
    
                Entity emailEntity = new Entity();
                emailEntity.LogicalName = "email";
    
                EntityCollection fromcollection = new EntityCollection();
                fromcollection.Entities = new System.Collections.ObjectModel.ObservableCollection<Entity>();
                fromcollection.Entities.Add(fromParty);
    
                emailEntity["from"] = fromcollection;
    
                EntityCollection tocollection = new EntityCollection();
                tocollection.Entities = new System.Collections.ObjectModel.ObservableCollection<Entity>();
                tocollection.Entities.Add(toParty);
    
    
                emailEntity["to"] = tocollection;
                emailEntity["directioncode"] = true;
                emailEntity["subject"] = "subject";
                emailEntity["description"] = "description";
                emailEntity["regardingobjectid"] = entityRef;
    
                IOrganizationService soapService = SilverlightUtility.GetSoapService();
                soapService.BeginCreate(emailEntity, OnCreateComplete, soapService);


    Freelance Developer for Dynamics CRM 4.0/2011

    Tuesday, October 16, 2012 10:50 AM
    Moderator
  • Hi Andrii, Thank you very much for your valuable suggestion and time. Sorry to say it is not true that i am not trying. In this case i just used the "activityparty" instead of "email". So it was not working. Still i ll hope for your Suggestion and guidance in future. 

    Thank You very Much.


    saroj

    Tuesday, October 16, 2012 11:27 AM
  • Hi Andrii I have marked it as answer. And hope Andrii, the CRM master will Help me in future. If Yes, Please leave a reply.

    Thank you.


    saroj

    Tuesday, October 16, 2012 12:24 PM