/// <summary> /// Get a contact by email address /// </summary> /// <param name="emailAddress"></param> /// <returns></returns> private static Contact GetContactByEmailAddress(string emailAddress) { //var contactService = _constantContactFactory.CreateContactService(); IUserServiceContext userServiceContext = new UserServiceContext("3f09fe65-10ae-44a9-9db6-d9f2d6de1dec", "cvwkqk7ajrm67rcagvfwn9gx"); ConstantContactFactory serviceFactory = new ConstantContactFactory(userServiceContext); IContactService contactService = serviceFactory.CreateContactService(); ResultSet <Contact> contacts = contactService.GetContacts(emailAddress, 1, null, null); if (contacts != null) { if (contacts.Results != null && contacts.Results.Count > 0) { return(contacts.Results[0]); } } return(null); }
static void Mainss(string[] args) { Uri OrganizationUri = new Uri(String.Format("https://netunousa.api.crm.dynamics.com/XRMServices/2011/Organization.svc")); Uri HomeRealmUri = new Uri(String.Format("https://netunousa.api.crm.dynamics.com/XRMServices/2011/Discovery.svc")); Uri serviceUri = new Uri("https://netunousa.api.crm.dynamics.com/XRMServices/2011/Organization.svc"); System.ServiceModel.Description.ClientCredentials clientCredentials = new System.ServiceModel.Description.ClientCredentials(); clientCredentials.UserName.UserName = "******"; clientCredentials.UserName.Password = "******"; OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(OrganizationUri, null, clientCredentials, null); IOrganizationService orgservice = (IOrganizationService)serviceproxy; //ConditionExpression condition = new ConditionExpression(); //condition.AttributeName = "new_integratectct"; //condition.Operator = ConditionOperator.Equal; //condition.Values.Add("0"); string[] cols = { "firstname", "lastname", "middlename", "emailaddress1", "address1_line1", "address1_line2", "address1_city", "address1_county", "address1_postalcode", "mobilephone", "donotbulkemail", "company", "jobtitle", "birthdate", "gendercode", "salutation" }; ColumnSet columns = new ColumnSet(cols); QueryExpression expression = new QueryExpression(); expression.EntityName = "contact"; //expression.ColumnSet = new AllColumns(); expression.ColumnSet.AllColumns = true; //expression.ColumnSet.AllColumns = true; expression.ColumnSet = columns; //expression.Criteria.AddCondition(condition); EntityCollection contactEntityColl = orgservice.RetrieveMultiple(expression); if (contactEntityColl.Entities.Count > 0) { IUserServiceContext userServiceContext = new UserServiceContext("3f09fe65-10ae-44a9-9db6-d9f2d6de1dec", "cvwkqk7ajrm67rcagvfwn9gx"); ConstantContactFactory serviceFactory = new ConstantContactFactory(userServiceContext); IContactService contactService = serviceFactory.CreateContactService(); for (int i = 0; i < contactEntityColl.Entities.Count; i++) { try { //get contact if exists (by email address) Contact contact = null; if (contactEntityColl.Entities[i].Attributes.Contains("emailaddress1")) { try { contact = GetContactByEmailAddress(contactEntityColl.Entities[i].Attributes["emailaddress1"].ToString()); } catch (CtctException ctcEx) { //contact not found } bool alreadyExists = contact != null ? true : false; contact = UpdateContactFields(contact, contactEntityColl.Entities[i].Attributes); Contact result = null; // var contactService = _constantContactFactory.CreateContactService(); if (alreadyExists) { result = contactService.UpdateContact(contact, false); contactEntityColl.Entities[i].Attributes["new_integratectct"] = result.Id; orgservice.Update((Entity)contactEntityColl.Entities[i]); } else { result = contactService.AddContact(contact, false); contactEntityColl.Entities[i].Attributes["new_integratectct"] = result.Id; orgservice.Update((Entity)contactEntityColl.Entities[i]); } if (result != null) { if (alreadyExists) { //messageResult = "Changes successfully saved!"; } else { // messageResult = "Contact successfully added!"; } } else { if (alreadyExists) { // messageResult = "Failed to save changes!"; } else { // messageResult = "Failed to add contact!"; } } //MessageBox.Show(messageResult, "Result"); } } catch (IllegalArgumentException illegalEx) { //MessageBox.Show(GetExceptionsDetails(illegalEx, "IllegalArgumentException"), "Exception"); } catch (CtctException ctcEx) { //MessageBox.Show(GetExceptionsDetails(ctcEx, "CtctException"), "Exception"); } catch (OAuth2Exception oauthEx) { //MessageBox.Show(GetExceptionsDetails(oauthEx, "OAuth2Exception"), "Exception"); } catch (Exception ex) { // MessageBox.Show(GetExceptionsDetails(ex, "Exception"), "Exception"); } } //btnSave.Enabled = true; } }
public void LiveGetContactByEmail() { var cc = new ConstantContactFactory(userServiceContext); var contactService = cc.CreateContactService(); var contact = new Contact(); contact.EmailAddresses.Add(new EmailAddress { EmailAddr = String.Format("{0}@email.com", Guid.NewGuid()), ConfirmStatus = ConfirmStatus.NoConfirmationRequired, Status = Status.Active }); contact.Lists.Add(new ContactList { Id = "1", Status = Status.Active }); Contact nc = contactService.AddContact(contact, false); Assert.IsNotNull(nc); Assert.IsNotNull(nc.Id); var result = contactService.GetContacts(nc.EmailAddresses[0].EmailAddr, 1, DateTime.Now.AddMonths(-1), null); Assert.IsNotNull(result); Assert.IsNotNull(result.Results); Assert.AreEqual(1, result.Results.Count); }
public void LiveAddContactTest() { var cc = new ConstantContactFactory(userServiceContext); var contactService = cc.CreateContactService(); var contact = new Contact(); contact.EmailAddresses.Add(new EmailAddress { EmailAddr = String.Format("{0}@email.com", Guid.NewGuid()), ConfirmStatus = ConfirmStatus.NoConfirmationRequired, Status = Status.Active }); contact.Lists.Add(new ContactList { Id = "1", Status = Status.Active }); var nc = contactService.AddContact(contact, false); Assert.IsNotNull(nc); Assert.IsNotNull(nc.Id); }
public void LiveGetAllContacts() { var cc = new ConstantContactFactory(userServiceContext); var contactService = cc.CreateContactService(); var result = contactService.GetContacts(DateTime.Now.AddMonths(-1)); Assert.IsNotNull(result); Assert.IsNotNull(result.Results); Assert.AreNotEqual(0, result.Results.Count); }
public void LiveContactTrackingUnsubscribesTest() { var cc = new ConstantContactFactory(userServiceContext); var contactService = cc.CreateContactService(); var contactTrackingService = cc.CreateContactTrackingService(); ResultSet<Contact> contacts = contactService.GetContacts(DateTime.Now.AddMonths(-1)); Assert.IsNotNull(contacts); Assert.IsNotNull(contacts.Results); Assert.IsTrue(contacts.Results.Count > 0); ResultSet<OptOutActivity> a = contactTrackingService.GetContactTrackingOptOuts(contacts.Results[0].Id, DateTime.Now.AddMonths(-1)); Assert.IsNotNull(a); }
public void LiveContactTrackingSummaryTest() { var cc = new ConstantContactFactory(userServiceContext); var contactService = cc.CreateContactService(); var contactTrackingService = cc.CreateContactTrackingService(); var emailCampaignService = cc.CreateEmailCampaignService(); ResultSet<Contact> contacts = contactService.GetContacts(DateTime.Now.AddMonths(-1)); Assert.IsNotNull(contacts); Assert.IsNotNull(contacts.Results); Assert.IsTrue(contacts.Results.Count > 0); TrackingSummary summary = contactTrackingService.GetSummary(contacts.Results[0].Id); Assert.IsNotNull(summary); //Assert.AreNotEqual(0, summary.Forwards); //Assert.AreNotEqual(0, summary.Opens); //Assert.AreNotEqual(0, summary.Sends); //ResultSet<EmailCampaign> camps = emailCampaignService.GetCampaigns(DateTime.Now.AddMonths(-1)); //summary = contactTrackingService.GetSummary(camps.Results[1].Id); //Assert.AreNotEqual(0, summary.Forwards); //Assert.AreNotEqual(0, summary.Opens); //Assert.AreNotEqual(0, summary.Sends); //Assert.AreNotEqual(0, summary.Bounces); }
public void LiveContactTrackingEmailCampaignActivitiesTest() { var cc = new ConstantContactFactory(userServiceContext); var contactService = cc.CreateContactService(); var contactTrackingService = cc.CreateContactTrackingService(); ResultSet<Contact> contacts = contactService.GetContacts(DateTime.Now.AddMonths(-1)); Assert.IsNotNull(contacts); Assert.IsNotNull(contacts.Results); Assert.IsTrue(contacts.Results.Count > 0); ResultSet<TrackingSummary> result = contactTrackingService.GetEmailCampaignActivities(contacts.Results[0].Id); Assert.IsNotNull(result); }
private static void GetAllMembersInaList(string listid, string constantcontactlistid, bool flag, ref IOrganizationService orgservice, ref IListService listService) { ArrayList memberGuids = new ArrayList(); PagingInfo pageInfo = new PagingInfo(); pageInfo.Count = 5000; pageInfo.PageNumber = 1; QueryByAttribute query = new QueryByAttribute("listmember"); // pass the guid of the Static marketing list query.AddAttributeValue("listid", listid); query.ColumnSet = new ColumnSet(true); EntityCollection entityCollection = orgservice.RetrieveMultiple(query); Entity entity1 = null; Contact result = null; IUserServiceContext userServiceContext = new UserServiceContext("3f09fe65-10ae-44a9-9db6-d9f2d6de1dec", "cvwkqk7ajrm67rcagvfwn9gx"); ConstantContactFactory serviceFactory = new ConstantContactFactory(userServiceContext); IContactService contactService = serviceFactory.CreateContactService(); ContactList list = null; try { list = GetListByID(constantcontactlistid, ref listService); } catch (Exception ex) { CTCTLogger.LogFile(ex.InnerException.ToString(), ex.InnerException.ToString(), ex.Data.ToString(), (int)ex.LineNumber(), ex.Source.ToString()); } foreach (Entity entity in entityCollection.Entities) { try { Contact contact = null; entity1 = RetrieveEntityById(orgservice, "contact", Guid.Parse(((EntityReference)entity.Attributes["entityid"]).Id.ToString())); if (entity1.Attributes.Contains("emailaddress1")) { contact = GetContactByEmailAddress(entity1.Attributes["emailaddress1"].ToString(), ref contactService); if (flag == false) { if (contact != null) { contact.Lists.Clear(); } } bool alreadyExists = contact != null ? true : false; try { contact = UpdateContactFields(contact, entity1.Attributes, list); } catch (Exception ex) { CTCTLogger.LogFile(ex.InnerException.ToString(), ex.InnerException.ToString(), ex.Data.ToString(), (int)ex.LineNumber(), ex.Source.ToString()); } // var contactService = _constantContactFactory.CreateContactService(); if (alreadyExists) { try { result = contactService.UpdateContact(contact, false); entity1.Attributes["new_integratectct"] = result.Id; orgservice.Update((Entity)entity1); } catch (Exception ex) { CTCTLogger.LogFile(ex.InnerException.ToString(), ex.InnerException.ToString(), ex.Data.ToString(), (int)ex.LineNumber(), ex.Source.ToString()); } } else { try { result = contactService.AddContact(contact, false); entity1.Attributes["new_integratectct"] = result.Id; orgservice.Update((Entity)entity1); } catch (Exception ex) { CTCTLogger.LogFile(ex.InnerException.ToString(), ex.InnerException.ToString(), ex.Data.ToString(), (int)ex.LineNumber(), ex.Source.ToString()); } } } } catch (Exception ex) { CTCTLogger.LogFile(ex.InnerException.ToString(), ex.InnerException.ToString(), ex.Data.ToString(), (int)ex.LineNumber(), ex.Source.ToString()); } } // if list contains more than 5000 records while (entityCollection.MoreRecords) { query.PageInfo.PageNumber += 1; query.PageInfo.PagingCookie = entityCollection.PagingCookie; entityCollection = orgservice.RetrieveMultiple(query); foreach (Entity entity in entityCollection.Entities) { try { memberGuids.Add(((EntityReference)entity.Attributes["entityid"]).Id); entity1 = RetrieveEntityById(orgservice, "contact", Guid.Parse(((EntityReference)entity.Attributes["entityid"]).Id.ToString())); } catch (Exception ex) { CTCTLogger.LogFile(ex.InnerException.ToString(), ex.InnerException.ToString(), ex.Data.ToString(), (int)ex.LineNumber(), ex.Source.ToString()); } } } }