Social Connected with Sitecore (Facebook) 2: Access facebook information

In an earlier post, we went over the setup of the Social Connected Module with Sitecore.
You can control the fields that you would want to access through the ~\App_Config\Include\Sitecore.Social.ProfileMapping.Facebook.config file.

Once a user logs into your site using their facebook login, based on your configuration settings, this data will be pulled into the core database against this user record.
However, this data is not by default visible through the user manager console of sitecore.

You can however, access this data via code and then choose to show it in user manager properties.
Take a look at Creating an extended Sitecore user for your site to create custom user properties.

Following is the code which would enable you to access the facebook data:

                foreach (var customPropertyName in Sitecore.Context.User.Profile.GetCustomPropertyNames())
                {
                    if (customPropertyName.Contains("fb_"))
                    {
                        litFacebookData.Text += customPropertyName + ": " + Sitecore.Context.User.Profile.GetCustomProperty(customPropertyName) + "<br>";
                    }
                }

The fields available in the ~\App_Config\Include\Sitecore.Social.ProfileMapping.Facebook.config don’t seem to provide a way to fetch the user profile picture from facebook!
So when the requirement came up, we ended up using the Graph API facebook provides, which takes as input the facebook user id. This is available in the core database with property name ‘fb_id’.

var facebookPictureUrl = "http://graph.facebook.com/{0}/picture?type=normal";
imgFacebookPicture.ImageUrl = string.Format(facebookPictureUrl, Sitecore.Context.User.Profile.GetCustomProperty("fb_id"));

Here is the result of the above code, as you see, the facebook data pulled in has property names prefixed by ‘fb_’. Also note the profile picture (QR Code).
2014-06-29_210334

Following is a user created using a facebook login.
Please note, when a user is created using facebook, an ‘_facebook’ is appended to the name. The user name however, doesn’t change if an existing user (same email id as facebook login) attaches his user id to his facebook profile.

2014-06-30_004340

Advertisements

One thought on “Social Connected with Sitecore (Facebook) 2: Access facebook information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s