giovedì 25 ottobre 2012

C# Google API - Load Contact's Groups ComboBox

When we insert a new contact, we need to indicate the group.
The following code shows how loading a combo box with the Contact's Groups

public void LoadGroupsComboBox()
        {
            ContactsService _service = new ContactsService("appName");
            _service.setUserCredentials("yourMail@gmail.com", "yourPassword");
            _service.QueryClientLoginToken();

            GroupsQuery query = new GroupsQuery(ContactsQuery.CreateGroupsUri("default"));
            GroupsFeed feed = _service.Query(query);

            while (feed != null && feed.Entries.Count > 0)
            {
                foreach (GroupEntry entry in feed.Entries)
                {
                    string gruppoNome = entry.Title.Text;
                    AtomId gruppoID = entry.Id;

                    ComboboxGroup item = new ComboboxGroup();
                    item.Text = gruppoNome;
                    item.Value = gruppoID;

                                   //add the item to your combo box
                    cbx_gruppi.Items.Add(item);
                }

                if (feed.NextChunk != null)
                {
                    query.Uri = new Uri(feed.NextChunk);
                    feed = _service.Query(query) as GroupsFeed;
                }
                else
                    feed = null;
            }

        }


Here the definition of the class ComboboxGroup used above.

class ComboboxGroup
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }


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...

Nessun commento:

Posta un commento