Search code examples
dynamics-crm-4

How to add required attendees in appoitment in microsoft crm


I added a new contact in microsoft dynamics 4.0 from C#. How to required attendees from code? I created a contact like this. now how to add this contact as required attendees for selected appointment ?

        CRM.CrmService service = GetService();

        var contactResult = new contact
        {
            emailaddress1 = userContact.Email
        };

        var requestContact = new RetrieveDuplicatesRequest
        {
            BusinessEntity = contactResult
            ,
            MatchingEntityName = EntityName.contact.ToString()
            ,
            PagingInfo = new PagingInfo
                             {
                                 PageNumber = 1,
                                 Count = 1
                             }
        };

        bool blnEmailExists = false;

        try
        {


            var response = (RetrieveDuplicatesResponse)
                                    service.Execute(requestContact);

            foreach (contact contactItem in response.DuplicateCollection.BusinessEntities)
            {
                blnEmailExists = true;
                contactResult.contactid = new Key();
                contactResult.contactid.Value = new Guid(contactItem.contactid.Value.ToString());
                contactResult.firstname = userContact.FirstName;
                contactResult.lastname = userContact.LastName;
                contactResult.fullname = userContact.FullName;
                contactResult.mobilephone = userContact.PhoneNumber;
                contactResult.description = userContact.Description;

                service.Update(contactResult);
            }

            if (!blnEmailExists)
            {
                contactResult.firstname = userContact.FirstName;
                contactResult.lastname = userContact.LastName;
                contactResult.fullname = userContact.FullName;
                contactResult.mobilephone = userContact.PhoneNumber;
                contactResult.description = userContact.Description;


                Guid contactId = service.Create(contactResult);

                if (!string.IsNullOrEmpty(userContact.Notes))
                    AppendToNotesSection(userContact.Notes, contactId, service);
            }
        }
        catch (System.Web.Services.Protocols.SoapException ex)
        {
            throw new Exception(ex.Message);
        }

Thanks


Solution

  • Through activityParty it can be updated, atlast i found it and fixed.

    CRM.CrmService service = GetService();
    
            appointment appointment = (appointment)service.Retrieve(EntityName.appointment.ToString(), activityid, new AllColumns());
    
            if(appointment == null)
            {
                throw new ArgumentNullException("Invalid Appointment");
            }
    
            contact contact = RegisterContact(userContact, service);
    
            activityparty[] activityParty = new activityparty[appointment.requiredattendees.Length + 1];
            activityParty[0] = new activityparty();
            activityParty[0].partyid = new Lookup();
            activityParty[0].partyid.Value = contact.contactid.Value;
            activityParty[0].partyid.type = "contact";
            Int16 index = 1;
            foreach (var item in appointment.requiredattendees)
            {
                //for avoiding duplicates
                if (item.partyid.Value != contact.contactid.Value)
                {
                    activityParty[index] = new activityparty();
                    activityParty[index].partyid = item.partyid;
                    activityParty[index].partyid.Value = item.partyid.Value;
                    activityParty[index].partyid.type = item.partyid.type;
                    index++;
                }
            }
    
    
    
            appointment.requiredattendees = activityParty;
    
            try
            {
                service.Update(appointment);
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Console.WriteLine(ex.Message + ". " + ex.Detail.InnerText);
            }