Kentico contacts lookup and get custom field values

Luke Hook asked on June 13, 2018 12:10

Hi there,

I'm working on a macro for email marketing that requires a lookup for custom field values for a given contact.

I'm trying to query the Contacts form data to return data matching a contacts firstName, lastName and Emailaddress and then wish to GetValue of 3 custom fields from that contact data item (let's call them Value1, Value2, Value3) but I'm not sure how to achieve this in the .NET code. I can query a BizForm for fields that way like below.

BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ExampleFormName", SiteContext.CurrentSiteID);

if (formObject != null)
    DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
    string formClassName = formClass.ClassName;

    // Loads the form's data
    ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(formClassName);

    // Loops through the form's data records
    foreach (BizFormItem item in data)
        string emailFieldValue = item.GetStringValue("Email", "");
        string firstNameFieldValue = item.GetStringValue("FirstName", "");
        string LastNameFieldValue = item.GetStringValue("LastName", "");

The above will obviously loop through all form data.

Ideally I want to know how I can query the Contacts data to pull a single contact WHERE the firstname, lastname and email all match. AND then 'get' the values of 3 custom fields (e.g. value1) for that contact.

Thanks, Luke

Recent Answers

Peter Mogilnitski answered on June 13, 2018 14:11 (last edited on June 13, 2018 14:22)

Not quite sure why would you check for firstname and last name, if an email is pretty unique by default. Just add using System.Linq; and should be something like this below:

BizFormItem Contact = BizFormItemProvider.GetItems(formClassName)
                            .WhereEquals("Email", "")
                            .WhereEquals("FirstName", "John")
                            .WhereEquals("LastName", "Doe").FirstOrDefault();
if (Contact != null)
    // You found one.
    string columnValue = ValidationHelper.GetString(Contact.GetValue("ColumnName"),"");
    int intColumnValue = ValidationHelper.GetInteger(Contact.GetValue("IntColumnName"), -1);
0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on June 13, 2018 15:53

Luke, are you trying to find the contact from Contact table or from a BizForm data table? If you are trying to find the contact (ContactInfo) the sample code is like this.

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo updateContact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)

if (updateContact != null)
    // Updates the contact's properties
    updateContact.ContactCompanyName = "Company Inc.";

    // Saves the updated contact to the database
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.