Skip to content
Johan Vervloet edited this page Mar 30, 2015 · 2 revisions

Chaining

Simple chaining

If you want to get a person with all his addresses, you can use a chained call, e.g.

        var request = new ContactRequest
        {
            ExternalIdentifier = "60115",
            AddressGetRequest = new AddressRequest(),
        };

If you send this request to ContactGetSingle, the resulting contact will have in its AddressResult property the result of an API call to retrieve all addresses with the ID of the contact as ContactId. Because the address request is a child of the contact request, CiviCRM will use the ID of the parent to filter the addresses on ContactId. But it seems that this magic only works when the parent request is a ContactRequest. For other entities, you have to explicitly specify the filter to be used in the child request, but more about this later on.

using System;
using System.Linq;
using System.ServiceModel;
using Chiro.CiviCrm.Api;
using Chiro.CiviCrm.Api.DataContracts;
using Chiro.CiviCrm.Api.DataContracts.Entities;
using Chiro.CiviCrm.Api.DataContracts.Requests;
using Chiro.CiviSandbox.Chaining.Properties;

namespace Chiro.CiviSandbox.Chaining
{
    class Program
    {
        static void Main(string[] args)
        {
            string apiKey = Settings.Default.ApiKey;
            string siteKey = Settings.Default.SiteKey;

            Contact contact;

            var request = new ContactRequest
            {
                ExternalIdentifier = "60115",
                AddressGetRequest = new AddressRequest(),
                MembershipGetRequest = new MembershipRequest
                {
                    MembershipTypeId = (int)MembershipType.DubbelpuntAbonnement
                }
            };

            using (var channelFactory = new ChannelFactory<ICiviCrmApi>("*"))
            {
                using (var client = channelFactory.CreateChannel())
                {
                    var result =
                        client.ContactGet(apiKey, siteKey, request);
                    if (result.IsError != 0)
                    {
                        throw new ApplicationException(result.ErrorMessage);
                    }
                    contact = result.Values.First();
                }
            }

            // process addresses
            foreach (var a in contact.AddressResult.Values)
            {
                Console.WriteLine("{0} {1} {2}", a.StreetAddress, a.PostalCode, a.City);
            }

            Console.WriteLine("Druk enter.");
            Console.ReadLine();
        }
    }
}

More fine grained chained calls

If you send a chained call to the API, you send two (or more) requests. In the example above, the second request was empty, but you could do something more specific. This example gets a contact and all memberships with MembershipTypeId 2:

        var request = new ContactRequest
        {
            ExternalIdentifier = "60115",
            MembershipGetRequest = new MembershipRequest
            {
                MembershipTypeId = (int)2
            }
        };

This kind of filtering only works if the nested request is a 'real' request (contact, membership, event, relationship, address); it does not work for 'EntityRequests', like e-mail and phone. (See RequestsAndEntities.)

Clone this wiki locally