public void AddAddress()
        {
            using (var client = TestHelper.ClientGet())
            {
                var newAddress = new AddressRequest
                {
                    ContactId = _myContactId,
                    StreetAddress = "Hoefslagstraatje 2",
                    PostalCode = "9000",
                    City = "Gent",
                    CountryId = 1020,   // Belgium
                    LocationTypeId = 1,
                };

                var result = client.AddressSave(TestHelper.ApiKey, TestHelper.SiteKey, newAddress);
                Assert.AreEqual(0, result.IsError);
                Assert.AreEqual(1, result.Count);

                var resultAddress = result.Values.First();

                Assert.AreEqual(newAddress.ContactId, resultAddress.ContactId);
                Assert.AreEqual(newAddress.StreetAddress, resultAddress.StreetAddress);
                Assert.AreEqual(newAddress.PostalCode, resultAddress.PostalCode);
                Assert.AreEqual(1020, resultAddress.CountryId);
                Assert.AreEqual(newAddress.LocationTypeId, resultAddress.LocationTypeId);
            }
        }
 public void InitializeTest()
 {
     using (var client = TestHelper.ClientGet())
     {
         // ContactType seems to be mandatory for a create action of the CiviCRM
         // contact api.
         var result = client.ContactSave(TestHelper.ApiKey, TestHelper.SiteKey,
             new ContactRequest {ContactType = ContactType.Individual, FirstName = "Joe", LastName = "Schmoe"});
         _myContactId = result.Values.First().Id;
         // TODO: chain this address creation.
         var addressRequest = new AddressRequest
         {
             ContactId = _myContactId,
             StreetAddress = "Kipdorp 30",
             PostalCode = "2000",
             City = "Antwerpen",
             CountryId = 1020,   // Belgium
             LocationTypeId = 1,
             Name = MyAddressName
         };
         // If this fails, please turn off map and geocode services.
         // (Adminis, System Settings, Maps)
         var addressResult = client.AddressSave(TestHelper.ApiKey, TestHelper.SiteKey, addressRequest);
         var address = addressResult.Values.First();
         _myAddressId = address.Id;
     }
 }
        public void EventWithAddress()
        {
            // Make sure that your API user has permissions
            // 'access CiviEvent', 'view event info',
            // and 'edit all events'.

            var myAddressRequest = new AddressRequest
            {
                LocationTypeId = 1,
                StreetAddress = "Kipdorp 30",
                PostalCode = "2000",
                City = "Antwerpen"
            };

            var myEventRequest = new EventRequest
            {
                Title = "My mighty event",
                Description = "It will be fun.",
                StartDate = new Filter<DateTime?>(new DateTime(2015, 07, 01)),
                EndDate = new Filter<DateTime?>(new DateTime(2015, 07, 10)),
                EventTypeId = MyEventTypeId,
            };

            using (var client = TestHelper.ClientGet())
            {
                // Save the event by chaining everything to the loc block.
                var saveResult = client.LocBlockSave(TestHelper.ApiKey, TestHelper.SiteKey, new LocBlockRequest
                {
                    Address = myAddressRequest,
                    EventSaveRequest = new[] {myEventRequest}
                });
                Assert.IsNotNull(saveResult.Id);

                int myLocBlockId = saveResult.Id.Value;

                var eventGetRequest = new EventRequest
                {
                    LocBlockId = myLocBlockId,
                    LocBlockGetRequest = new LocBlockRequest
                    {
                        IdValueExpression = "$value.loc_block_id",
                        AddressGetRequest = new AddressRequest
                        {
                            IdValueExpression = "$value.address_id"
                        }
                    }
                };
                var getResult = client.EventGet(TestHelper.ApiKey, TestHelper.SiteKey, eventGetRequest);
                Assert.AreEqual(1, getResult.Count);

                var savedEvent = getResult.Values.First();
                Assert.AreEqual(1, savedEvent.LocBlockResult.Count);
                Assert.AreEqual(1, savedEvent.LocBlockResult.Values.First().AddressResult.Count);
                var savedAddress = savedEvent.LocBlockResult.Values.First().AddressResult.Values.First();

                // Delete first. Then assert.
                client.EventDelete(TestHelper.ApiKey, TestHelper.SiteKey, new DeleteRequest(savedEvent.Id));
                client.LocBlockDelete(TestHelper.ApiKey, TestHelper.SiteKey, new DeleteRequest(myLocBlockId));
                client.AddressDelete(TestHelper.ApiKey, TestHelper.SiteKey, new DeleteRequest(savedAddress.Id));

                Assert.AreEqual(myEventRequest.Title, savedEvent.Title);
                Assert.AreEqual(myAddressRequest.StreetAddress, savedAddress.StreetAddress);
            }
        }
        /// <summary>
        /// This example gets a contact, adds an address, and deletes the
        /// address again.
        /// </summary>
        static void Example1()
        {
            using (var client = _factory.CreateChannel())
            {
                // Get the contact, and chain the contact's addresses.
                var contact = client.ContactGetSingle(ApiKey, SiteKey, new ContactRequest
                {
                    ExternalIdentifier = ExternalId,
                    AddressGetRequest = new AddressRequest()
                });

                // Keep the contact Id for later reference.
                int contactId = contact.Id;

                // Exit if contact is not found.
                if (contact == null)
                {
                    Console.WriteLine("Contact not found.");
                    return;
                }

                ShowContact(contact);
                ShowAddresses(contact);

                // Add an address to the contact.
                var newAddress = new AddressRequest
                {
                    ContactId = contact.Id,
                    StreetAddress = "Hoefslagstraatje 2",
                    PostalCode = "9000",
                    City = "Gent",
                    Country = "BE",
                    LocationTypeId = 1,
                };

                var result = client.AddressSave(ApiKey, SiteKey, newAddress);
                Debug.Assert(result.Id != null);
                newAddress.Id = result.Id.Value;

                // Get contact again, to find out whether the address 
                // has been added.
                // Note that we now use the CiviCRM contact ID.
                contact = client.ContactGetSingle(ApiKey, SiteKey, new ContactRequest
                {
                    Id = contactId,
                    // We don't need all fields of the contact, we are only interested in the
                    // addresses.

                    // ReturnFields are still in civicrm notation, meaning lowercase and
                    // underscores (see issue #19)
                    ReturnFields = "id",
                    AddressGetRequest = new AddressRequest()
                });

                // Show adresses
                ShowAddresses(contact);

                // Delete the added addres
                client.AddressDelete(ApiKey, SiteKey, new DeleteRequest(newAddress.Id.Value));

                // Get the adresses again, to verify that the new address is gone.
                contact = client.ContactGetSingle(ApiKey, SiteKey, new ContactRequest
                {
                    Id = contactId,
                    ReturnFields = "id",
                    AddressGetRequest = new AddressRequest()
                });

                ShowAddresses(contact);
            }
        }
 public void GetAddressWithLocBlock()
 {
     using (var client = TestHelper.ClientGet())
     {
         var addressRequest = new AddressRequest
         {
             Id = _myLocBlock.AddressId,
             LocBlockGetRequest = new LocBlockRequest
             {
                 AddressIdValueExpression = "$value.id"
             }
         };
         var result = client.AddressGet(TestHelper.ApiKey, TestHelper.SiteKey, addressRequest);
         Assert.AreEqual(1, result.Count);
         Assert.AreEqual(_myLocBlock.Id, result.Values.First().LocBlockResult.Id);
     }
 }