The Microsoft Dynamics CRM Blog
News and views from the Microsoft Dynamics CRM Team

Integrating CRM 4.0 Contacts with Facebook

Integrating CRM 4.0 Contacts with Facebook

  • Comments 4

CRM stores information about your contacts. Facebook also stores information about contacts. Let’s merge the two entities together so you can view Facebook profiles from within a CRM entity page! When we are done, our contact entity page will be able to do the following:

clip_image002

There are 2 main tasks here. The first is customizing CRM so that the contact entity can display a Facebook profile. The second is to write a web page that will redirect requests to the proper Facebook page.

I. Customize CRM

Step 1: Add an attribute to the contact entity that will store the Facebook User Id as a string. By storing this attribute in CRM, load times of Facebook profiles are greatly increased.

1. Navigate to Settings -> Customization -> Customize Entities.

2. Open up the contact entity.

3. Click “Attributes” and then click New

4. Fill out the DisplayName and the Name and select a type of nvarchar. All the defaults are fine. Click “Save and Close”.

 clip_image004

 

Step 2: Add a Tab to the contact form that will display the Facebook Profile.

1. Continuing from where we left off above, click “Forms and Views” on the Contact customization page.

2. Open up the row with main contact form, called “Form” with a description of “The main application form.”

3. Click “Add a Tab” from the Common Tasks pane, and name the tab.

4. On your new tab, click “Add a Section” from the Common Tasks pane, give your section a name and hit OK.

5. Click “Add an IFRAME” from the Common Tasks pane.

6. Name your IFRAME, and give the URL for the website that you plan to use (We will create this website next).

7. Check the box to “Pass the object-type code and the unique identifiers as parameters”.

8. Uncheck the box to “Restrict cross-frame scripting”. This will allow the Facebook profile to load directly into the contact entity page as opposed to opening in a new IE window.

 clip_image006

9. On the Formatting tab, increase the value for “Number of Rows”. I used 30, but you may want more or less. You can come back and change this later.

10. Click OK to close the new IFRAME dialog, and then click “Save And Close” to close the form.

11. From the contact customization page, choose Actions -> Publish.

II. Create a Website

CRM is now ready for Facebook integration. We need to create a website that will read the contactid passed from CRM via the querystring, retrieve the information from CRM about this contact, and then display the proper Facebook page. The flow of the web page is as follows:

1. Login to Facebook

2. Parse Querystring from the Request

3. Retrieve contact name and Facebook User Id attribute from CRM

4. If the Facebook User Id attribute is populated, display the Facebook profile.

5. If the Facebook User Id attribute is not populated, search all Facebook friends for a name match. If one is found, then update CRM with the Facebook User Id for this person, and redirect to the profile page.

6. If no matches are found, redirect to the Facebook search page.

Let’s get started on our webpage.

 

Step 1: Create the website.

1. Download the Facebook Developer Toolkit: http://www.codeplex.com/FacebookToolkit

2. Open Visual Studio and choose New->Website. Create an ASP.NET Web Site.

 

Step 2: Add a reference to the Facebook binary and to the CRM web service.

1. Right click on your new Project in Solution Explorer and choose “Add Reference”. Add a reference to Facebook.dll from the Facebook Developer Toolkit.

2. Right click on your new Project in the Solution Explorer and choose “Add Web Referece”. Enter the URL for the WSDL on your CRM server: http://SERVERNAME/MSCRMServices/2007/CrmService.asmx

 

Step 3: Authenticate against Facebook

1. For logging into Facebook, I’ll just refer to you to the Quick Start Guide for the Facebook Developer Toolkit: http://msdn.microsoft.com/vstudio/express/showcase/quickstarts/default.aspx#web . You can skip the part about adding a friendlist to the aspx page (steps 3, 4, and 11). Add the the if (!IsPostBack) block to your page, because that is where the interesting code will happen.

Step 4: Read the QueryString property of the Request to find out the Crm Contact Id and the Crm Organization name. This should be the very first thing in your Page_Load method, even before Facebook authentication occurs.

string orgQuery = this.Request.QueryString["orgname"];

if (!string.IsNullOrEmpty(orgQuery))

{

_orgName = orgQuery;

}

string idQuery = this.Request.QueryString["id"];

Step 5: Now let’s jump down to the if (!IsPostBack) block and get started processing the request. If the id is not on the QueryString then we are on a new contact page, so we can just write that there is no contact information and our job is done.

if (!string.IsNullOrEmpty(idQuery))

{

_contactId = idQuery;

}

Step 6: Retrieve the contact information from CRM – this is pretty straightforward for an On-Premise installation.

CrmService myservice = new CrmService();

NetworkCredential nc =

new NetworkCredential(UserName, Password, Domain);

myservice.Credentials = nc;

CrmAuthenticationToken token = new CrmAuthenticationToken();

myservice.CrmAuthenticationTokenValue = token;

token.OrganizationName = _orgName;

token.AuthenticationType = 0;

ColumnSet names = new ColumnSet();

names.Attributes =

new string[] { "new_facebookuserid", "firstname", "lastname" };

Guid contactId = new Guid(_contactId);

contact contact =

myservice.Retrieve("contact", contactId, names) as contact;

Step 7: If the new_facebookuserid attribute is populated then all we have to do is redirect to the Facebook profile page.

if (!string.IsNullOrEmpty(contact.new_facebookuserid))

{

_contactId = null; this.Response.Redirect(@"http://www.facebook.com/profile.php?id=" + contact.new_facebookuserid);

}

Step 8: The new_facebookuserid attribute is not populated so we need to search all friends to try and find a match to the name of the contact. If a match is found, redirect to the profile page; otherwise, redirect to the search page.

else

{

string firstName = contact.firstname.ToLower();

string lastName = contact.lastname.ToLower();

// get all friends

Collection<User> friends = this._fbService.GetFriends();

// add the logged in user

friends.Add(this._fbService.GetUserInfo());

// check

foreach (User friend in friends)

{

if (friend.LastName.ToLower().Equals(lastName) && friend.FirstName.ToLower().Equals(firstName))

{

_contactId = null;

// update the CRM database

contact.new_facebookuserid = friend.UserId;

myservice.Update(contact);

// redirect to profile page this.Response.Redirect(@"http://www.facebook.com/profile.php?id=" + friend.UserId);

}

}

// none found so redirect to search page

string url = string.Format(@"http://www.facebook.com/s.php?q={0}+{1}", firstName, lastName);

_contactId = null;

this.Response.Redirect(url);

}

There are a few things to note:

1. The variables _contactId and _orgName need to be static so that their values are maintained from the initial page load through the time authentication is completed.

2. The Facebook URLs used here were discovered by navigating to certain pages. They are not guaranteed to always work, as Facebook may change the structure of their site.

3. Name matching as implemented here has obvious drawbacks.

The similarities between CRM contact management and Facebook contact management made me think about ways of combing them. I figured that putting Facebook inside CRM would not be an arduous task given Microsoft CRM's extensibility model. A couple of hours and a few dozen lines of code later, I am enjoying my customized CRM with Facebook integration!

*This posting is provided "AS IS" with no warranties, and confers no rights.

Rob Tobkes

Page 1 of 1 (4 items)
Leave a Comment
  • Please add 7 and 6 and type the answer here:
  • Post