I got this code to "work" (read not throwing an exception). But the contact is not added to my gmail contacts as it should (nor on my android phone which sync contacts).
Note that I can read the contacts correctly so the credentials are right.
I read that I should check for a Status on the request, but the only Status I see is a property of ContactEntry and it's always null.
This is a console app for tests.
public static void AddContact(ContactDetail contact)
{
GContactService = new ContactsService("Contact Infomation");
GContactService.setUserCredentials("myemail@gmail.com", "mypassword");
ContactEntry newEntry = new ContactEntry();
newEntry.Title.Text = contact.Name;
newEntry.Name = new Name() { FullName = "Tristan Savage", GivenName = "Tristan", FamilyName = "Savage"};
EMail primaryEmail = new EMail(contact.EmailAddress1);
primaryEmail.Primary = true;
primaryEmail.Rel = ContactsRelationships.IsWork;
newEntry.Emails.Add(primaryEmail);
PhoneNumber phoneNumber = new PhoneNumber(contact.Phone);
phoneNumber.Primary = true;
phoneNumber.Rel = ContactsRelationships.IsMobile;
newEntry.Phonenumbers.Add(phoneNumber);
PostalAddress postalAddress = new PostalAddress();
postalAddress.Value = contact.Address;
postalAddress.Primary = true;
postalAddress.Rel = ContactsRelationships.IsCompanyMain;
newEntry.PostalAddresses.Add(new StructuredPostalAddress() { City = "montreal", Label = "Bureau"});
newEntry.Content.Content = contact.Details;
Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default")); //default
ContactEntry createdEntry = (ContactEntry)GContactService.Insert(feedUri, newEntry);
}
I finally figured it out. The GroupMembership is required in order for the contact to get to the mobile device!
Here is the missing part:
var groupMembership = new GroupMembership
{
HRef = "http://www.google.com/m8/feeds/groups/" + utilisateur.CourrielGmailContacts + "/base/6"
};
newEntry.GroupMembership.Add(groupMembership);