Monday, November 29, 2010

MSCRM: LinkedIn Access from the Account Form

A client recently asked if it would be possible to look at LinkedIn connections right from the Microsoft Dynamics CRM application.

The code below inserted into the On-Load event in a form will display the LinkedIn widget on the account form. When you click on the widget, if you are signed in to LinkedIn, the connections you have with an Account are displayed in a popup.

The code to achieve this is not too complicated. It just requires some minor
adjustments to ensure proper visualization within the CRM form.

This code works by getting the name of the company from the text field that
you indicate when firing the LinkedInLoader script. So, make sure that you
provide a valid CRM form text field when you call the LinkedInLoader.

function LinkedInLoader(crmFormField) {
var ll = this;
ll.scriptSource = 'http://www.linkedin.com/companyInsider?script&useBorder=yes'
ll.field = crmFormField;
ll.container = crmFormField.parentNode;
ll.nameToSearch = crmFormField.DataValue;
ll.spanId = ll.field.id + '_linkedin';
if (ll.container != null) {
var span = document.createElement('span');
span.id = ll.field.id + '_linkedin';
var td1 = document.createElement('td');
td1.innerHTML = ll.container.innerHTML;
var td2 = document.createElement('td');
td2.appendChild(span);
td2.style.width = '15px';
var tr = document.createElement('tr');
tr.appendChild(td1);
tr.appendChild(td2);
var table = document.createElement('table');
table.width = '100%';
table.style.tableLayout = 'fixed';
table.cellSpacing = 0;
table.cellPading = 0;
table.appendChild(tr);
ll.container.innerHTML = table.outerHTML;
}
ll.ApplyCorrections = function() {
var div = document.getElementById('company-insider-info-window');
if (div != null) div.style.height = '275px';
else window.setTimeout(ll.ApplyCorrections, 500);
}
ll.Enable = function() {
new LinkedIn.CompanyInsiderPopup(ll.spanId, ll.nameToSearch);
new LinkedIn.CompanyInsiderStylesheet();
var span = document.getElementById(ll.spanId);
if (span != null) span.attachEvent('onclick', ll.ApplyCorrections);
}
ll.OnScriptReadyState = function() {
if ((event.srcElement.readyState == 'complete') ||
(event.srcElement.readyState == 'loaded')) {
ll.Enable();
}
}
ll.Load = function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = ll.scriptSource;
script.onreadystatechange = ll.OnScriptReadyState;
document.getElementsByTagName('head')[0].appendChild(script);
}
}
if (crmForm.FormType != 1) {
// Set the field that contains the company name
var linkedInLoader = new LinkedInLoader(crmForm.all.name);
linkedInLoader.Load();

1 comment:

Andriy a33ik Butenko said...

May be it is correct to post the reference to author of this code?

http://marcoamoedo.com/blog/adding-linkedin-to-dynamics-crm/