giovedì 25 ottobre 2012

C# Google API - Insert Google Contact

If we want to synchronize our own index book with google calendar we could be able also to insert. 
The following code shows a c# method to insert google contact.
The comments, I think, make the code clearer.


       private Contact CreateContact(ContactsRequest cr)
        {
            Contact newEntry = new Contact();

            /*
                With this code you can set the Atom ID (id of the group) for the new contact.
                The Group AtomId can be (for example) taked from a combo box containing the all groups you have
look the following link http://dotnetoday.blogspot.it/2012/10/c-google-api-load-contacts-groups.html to fill a combo box with the contact's group
             *

                newEntry.AtomEntry.Id = (AtomId)((ComboboxGruppi)cbx_group.SelectedItem).Value;
            */

            //NAME
            newEntry.Name = new Name()
                {
                    FullName = "GivenName FamilyName",
                    GivenName = "GivenName",
                    FamilyName = "FamilyName",
                };
            newEntry.Content = "Insert Here Some Notes About The Contact";

            //EMAIL(S)
            newEntry.Emails.Add(new EMail()
                {
                    Primary = true,
                    Rel = ContactsRelationships.IsOther,
                    Address = "primari email address"
                });
            newEntry.Emails.Add(new EMail()
                {
                    Rel = ContactsRelationships.IsWork,
                    Address = "other email address"
                });
            //as the previous lines, you can insert all the mails you want

            //PHONE NUMBER(S)
            newEntry.Phonenumbers.Add(new PhoneNumber()
                {
                    Primary = true, //primary phone number
                    Rel = ContactsRelationships.IsWork,
                    Value = "12345",
                });
            newEntry.Phonenumbers.Add(new PhoneNumber()
                {
                    Rel = ContactsRelationships.IsHome,
                    Value = "5464534",
                });
            //as the previous lines, you can insert all the phone numbers you want

            //POSTAL ADDRESS
            newEntry.PostalAddresses.Add(new StructuredPostalAddress()
                {
                    Rel = ContactsRelationships.IsWork,
                    Primary = true,
                    Street = "the street",
                    City = "the city",
                    Region = "Region",
                    Postcode = "Postal Code",
                    Country = "Country"
                });

            Uri feedUri = new Uri("https://www.google.com/m8/feeds/contacts/yourmail@gmail.com/full");
            Contact createdEntry = cr.Insert(feedUri, newEntry);

            return createdEntry;
        }



And here the code shows an example of how to use it.

GDataCredentials Credentials = new GDataCredentials("yourMail@gmail.com", "yourPassword");
        RequestSettings settings = new RequestSettings("appName", Credentials);
        ContactsRequest cr = new ContactsRequest(settings);
                                
                CreateContact(cr);





I want to remeber to go here http://support.google.com/mail/bin/answer.py?hl=it&answer=1173270 to get  Application-specific password

My Two Cents...

3 commenti:

  1. Is the same for domain accounts ?
    Thanks in advance.

    --Orlando

    RispondiElimina
  2. in what sense? can you explain better your case?
    thank you

    RispondiElimina
  3. Hi Mr. Sorbello.
    I sent you an explanation to your G+ page :)
    Thanks in advance.

    --Orlando

    RispondiElimina