Thursday, August 3, 2017

Auto populate Web form for marketers Form (WFFM) Fields With social data using social connected Sitecore 8+

In previous post i have shown you how to get the social data using social connected.


Now we will see how to auto populate these data to a web form for marketers Form Fields.

Prerequisite:-

  1. Sitecore 8+
  2. Web form for marketers module
  3. Social connected configured.
We will be fetching LinkedIn data to a WFFM form using LinkedIn Login.

Step1- Setting up WFFM form in page.

 For adding a form in page create a form item in sitecore.


Step2- Assign WFFM form to page and add LinkedIn login control


adding-wffm-form-sitecore





Step 3 - Configuring Form fields 

To set default value to a form field we can set Rules.
  • Click on form item go to form designer
  • Select any field and scroll to bottom of the left panel.
wffm-sitecore-field-rules


By default sitecore provides few default action.



default-actions-wffm-field





  • Hide element
  • Use value from query string
  • Use value from profiles specific field
  • Use value from specific visitor tag
But since we have to populate the social data we can not use above rules.
For populating social data we need to create a new rule.

Step 4- Creating new WFFM action rule.

  • Navigate to "/sitecore/system/Settings/Rules/Definitions/Elements/Web Forms for Marketers Actions"
  • Right click and insert new Action.
  • Provide your custom method and reference details. As below.
wffm-add-action-rule



wffm-new-action-created


Method- SCcustom.ReadPropertyFromMongo

using Sitecore.Security.Accounts;
using System;
using System.Collections.Generic;
using Sitecore.Diagnostics;
using Sitecore;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Sitecore.Social.Api;

namespace SCcustom
{
    public class ReadPropertyFromMongo<T> : Sitecore.Forms.Core.Rules.ReadValue<T> where T : Sitecore.Forms.Core.Rules.ConditionalRuleContext
    {
        private string Network { get; set; }
        protected override object GetValue()
        {
          
            try
            {
                string networkname = "LinkedIn";
                networkname = Network;
                // Sitecore.Context.User.Profile.GetCustomProperty("soc_PrimaryNetwork") ;

                var socialProfile = new SocialProfileManager();
               
                // Do we have an extra profile from the user?
                if (socialProfile.SocialProfileExists(Sitecore.Context.User.Name, networkname))
                {
                    var soProfile = socialProfile.GetSocialProfile(Sitecore.Context.User.Name, networkname);
                    try
                    {
                        return soProfile.Fields[Name];
                    }
                    catch

                    { return ""; }
                }
                return "";
            }
            catch
            { return "network error"; }
        }

     
    }
}

Now we have to pass two parameters from Sitecore-"Name" and "Network" from Sitecore.

set-wffm-custom-action-sitecore-social-data


Custom rule will look for the "ln-headline" property in context user's Linkedin social profile. If any value is found it will return the value else it will return blank.

Below is the Output

wffm-populated-social-data-sitecore


No comments:

Post a Comment