private void bw_DoWork(object sender, DoWorkEventArgs e) { XrmServiceContext xrm; xrm = new XrmServiceContext("Xrm"); foreach (MyLead objLead in Leads) { //xrm.AddObject(objLead); Guid objLeadGuid = xrm.Create(objLead);//Using this method as opposed to xrm.AddObject results in getting the GUID required to add a note to the lead. //This method also adds the Lead Object to the Db. objLead.Note.ObjectId = new Microsoft.Xrm.Client.CrmEntityReference("lead", objLeadGuid); //Creates the xref between the Annotation object (the note) and the lead xrm.AddObject(objLead.Note); //adds the note to the CRM Database.. } xrm.SaveChanges();//this saves all the notes to the database. The leads are automatically saved by calling the Create() method... }
public void AddSubscribers(string ListId, List <Entities.Subscriber> subscribers) { try { foreach (Entities.Subscriber subscriber in subscribers) { Contact contact = (Contact)context.ContactSet.Where(x => x.EMailAddress1.Equals(subscriber.E_mail)).FirstOrDefault(); Guid listGuid = new Guid(ListId); if (contact == null) { Guid contactGuid = context.Create(new Contact() { EMailAddress1 = subscriber.E_mail, FirstName = subscriber.Name }); var member = new AddMemberListRequest() { EntityId = contactGuid, ListId = listGuid }; context.Execute(member); } else { var member = new AddMemberListRequest() { EntityId = contact.Id, ListId = listGuid }; context.Execute(member); } } context.SaveChanges(); } catch (Exception e) { throw new Exception("Get subscribers CRM (" + e.Message.ToString() + ")"); } }