Access Form items and fields from Forms database in Sitecore 9.x

To access sitecore form items you need to work with Form data provider class which needs data service instance from Service Locator. With the help of Form data provider which is a class of
Sitecore.ExperienceForms.Data namespace. Here I am writing code sample which I used in my requirement of accessing or validation some form entries from form data base.

Initialize Form data provider.

private IFormDataProvider FormDataProviders = (IFormDataProvider)ServiceLocator.ServiceProvider.GetService(typeof(IFormDataProvider));


//Model to return result

public class FormDataDetail
    {
        public bool IsUserValid { getset; }
        public DateTime SubscriptionDate { getset; }


    }

//Code to access form entries for email and subscription date



public FormDataDetail GetDataToValidateUser(Guid formId, DateTime? startDate, DateTime? endDate, string userEmailId)
        {
            FormDataDetail formDataDetail = new FormDataDetail(); // FormDataDetail is a model having some property.
            try
            {
                IReadOnlyCollection<FormEntry> sampleFormEntries = FormDataProviders.GetEntries(formId, startDate, endDate);
                //check form have some entries or not
                if (sampleFormEntries == null || !sampleFormEntries.Any())
                {
                    formDataDetail.IsUserValid = true;
                }

                sampleFormEntries = sampleFormEntries.Where(x => x.Fields.Any(y => y.FieldName == "Email" && y.Value == userEmailId)).ToList();

                foreach (FormEntry item in sampleFormEntries)
                {
                    var emailValue = item.Fields.Where(x => x.FieldName == "Email").FirstOrDefault();
                    var userSubscriptionDate = item.Fields.Where(x => x.FieldName == "Created").FirstOrDefault();

                    //check user email is already registered with form subscription or not
                    if (emailValue != null && emailValue.Value.ToLower() == userEmailId.ToString().ToLower())
                    {
                        formDataDetail.IsUserValid = false;
                        if (userSubscriptionDate != null)
                        {
                            formDataDetail.SubscriptionDate = Convert.ToDateTime(userSubscriptionDate);
                        }
                    }
                    else
                    {
                        formDataDetail.IsUserValid = true;
                    }
                }
            }
            catch (Exception exInfo)
            {
                //log exception here
            }

            return formDataDetail;
        }



    }


//Calling method to get user information from form database.


FormDataProvider formDataProvider = formDataProvider.GetFormDataToValidateUser(new Guid(SampleFormId), nullnull, userEmailId);


Here you just change your field name and get your form/field/value detail from form database. I found it's very simple as compare to other approaches available for forms data base access.


Happy Learning!!!

Comments

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

Operators in Asterisk with Linux

ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'