Skip to main content

SharePoint 2007 Integration With Dynamics CRM - Solution - Part 1

The Scenario

An organization has relationships with third-parties. Accounts, customers, members, suppliers etc. The organization has a SharePoint Web site and Dynamics CRM where the information about third-parties is processed. As the current business process dictates, large number of external clients - agents in this sample - needs to contact organization via email, fax, regular mail or in person in order to submit important business information - applications in this scenario. We will create a public SharePoint Web site where agents can submit applications and then those applications will be copied automatically to Dynamics CRM and when their processing in CRM is finished some feedback will be published back to the SharePoint Web site and agents will be able to see the status of the application in CRM online on the SharePoint Web site.

We defined the solution design as below:



In order to get the solution we defined the following tasks:

1. Develop a custom SharePoint Workflow which will search CRM for existing related entities and create them if necessary and then notify the client. In our case we will create contacts, tasks to process application form and email activities programmatically.

2. Develop a custom Web part which will display number of application forms in CRM posted from SharePoint Web site in the form of Key Performance Indicator – assume there is a target for number of application forms
Note:

You could reasonably ask the question why we are going to use SharePoint workflow, but not list event handlers or other types of custom applications which SharePoint platform allows us to use? The answer is that SharePoint workflows use Windows Workflow Foundation as a base and therefore we have all the advantages of this framework – persistence, tracking, flexibility at our hand. Also we can monitor these integration activities with the mighty tools I slightly described here


Solution

1. Custom SharePoint Workflow

1.1. Create a SharePoint Sequential Workflow project in Visual Studio

1.2. In the Code View of the Workflow create the following methods – SearchForContact, CreateContact, CreateTask, SendEmailToContact:

public Guid SearchForContact(string sFirstName, string sLastName, string sEmail)
{
Guid gResult = Guid.Empty;
try
{
QueryExpression queryContact = new QueryExpression();

queryContact.ColumnSet = new MyCRMService.AllColumns();

queryContact.EntityName = EntityName.contact.ToString();

queryContact.Criteria = new FilterExpression();

queryContact.Criteria.FilterOperator = LogicalOperator.And;

ConditionExpression Condition = new ConditionExpression();

Condition.AttributeName = "emailaddress1";

Condition.Operator = ConditionOperator.Equal;

Condition.Values = new object[] { sEmail };

ConditionExpression Condition1 = new ConditionExpression();

Condition1.AttributeName = "firstname";

Condition1.Operator = ConditionOperator.Equal;

Condition1.Values = new object[] { sFirstName };

ConditionExpression Condition2 = new ConditionExpression();

Condition2.AttributeName = "lastname";

Condition2.Operator = ConditionOperator.Equal;

Condition2.Values = new object[] { sLastName };

queryContact.Criteria.Conditions = new ConditionExpression[] { Condition, Condition1, Condition2 };

BusinessEntityCollection retrievedContacts = service.RetrieveMultiple(queryContact);

if (retrievedContacts.BusinessEntities.Length > 0)
{
gResult = ((contact)retrievedContacts.BusinessEntities[0]).contactid.Value;

}

}
catch (System.Web.Services.Protocols.SoapException soapex)
{
... // error handling
}
catch (Exception ex)
{
... // error handling
}
return gResult;
}

public Guid CreateContact(string sFirstName, string sLastName, string sEmail)
{
Guid gResult = Guid.Empty;
try
{
contact mycontact = new contact();
mycontact.firstname = sFirstName;
mycontact.lastname = sLastName;
mycontact.emailaddress1 = sEmail;
gResult = service.Create(mycontact);
}
catch (System.Web.Services.Protocols.SoapException soapex)
{
... // error handling
}
catch (Exception ex)
{
... // error handling
}
return gResult;
}

public Guid CreateTask(Guid gContactId)
{
Guid gResult = Guid.Empty;
try
{
task task = new task();

task.subject = ...;

task.regardingobjectid = new Lookup();
task.regardingobjectid.Value = gContactId;
task.regardingobjectid.type = EntityName.contact.ToString();

gResult = service.Create(task);
}
catch (System.Web.Services.Protocols.SoapException soapex)
{
... // error handling
}
catch (Exception ex)
{
... // error handling
}
return gResult;
}

public void SendEmailToContact(Guid gContactId)
{
try
{
activityparty recipient2 = new activityparty();
recipient2.partyid = new Lookup();
recipient2.partyid.type = EntityName.contact.ToString();
recipient2.partyid.Value = gContactId;

email email = new email();
email.subject = “Hello gorgeous”;

// just in case we would like to put some contact details into the email body, i.e. Dear firstname lastname,...
contact myContact = (contact)service.Retrieve(EntityName.contact.ToString(), gContactId, new AllColumns());
email.description = “Hello from our mighty SharePoint 2 CRM Integration”;

email.to = new activityparty[] { recipient2 };

SendEmailRequest req = new SendEmailRequest();
req.EmailId = service.Create(email); // here we create email programmatically
req.TrackingToken = string.Empty;
req.IssueSend = true;

try
{
SendEmailResponse res = (SendEmailResponse)service.Execute(req); // here we send email programmatically
}
catch (System.Web.Services.Protocols.SoapException er)
{
... // error handling
}
}
catch (System.Web.Services.Protocols.SoapException er)
{
... // error handling
}
}

1.3. Add Code Activity to the workflow design:



1.4. Generate an execute method for the Code Activity we have just created. In that method get values from the workflow associated SharePoint list item and execute our functions for CRM interaction:

private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
try
{
// 1. parse SharePoint list item
string sFirstName = string.Empty, sLastName = string.Empty, sEmail = string.Empty;

try
{
sFirstName = this.workflowProperties.Item[this.workflowProperties.Item.Fields["First name"].InternalName].ToString();
sLastName = this.workflowProperties.Item[this.workflowProperties.Item.Fields["Last name"].InternalName].ToString();
sEmail = this.workflowProperties.Item[this.workflowProperties.Item.Fields["Email address"].InternalName].ToString();
}
catch (Exception ex)
{
... // error handling
}
// 2. search for existing contact - First Name, Last Name, Email
Guid gCntactId = SearchForContact(sFirstName, sLastName, sEmail);
// 2.1. does not exist – create
if (gCntactId == Guid.Empty)
{
gContactId = CreateContact(sFirstName, sLastName, sEmail);
}
if (gContactId != Guid.Empty)
{
// 3. Create task and send email to contact
Guid gTaskId = CreateTask(gContactId);

SendEmailToContact(gContactId);
}

}
catch (Exception ex)
{
... // error handling
}
}

1.5. Build workflow project, deploy it to the SharePoint Web site

1.6. Create a SharePoint list with the following columns:



1.7. Associate workflow with the list or library you created, create new record and see how your SharePoint 2 CRM integration works. :)

That’s it for now. Please stay tuned for the part number 2 of the proposed solution – Key Performance Indicator Web part for Dynamics CRM.


Comments

Popular posts from this blog

Setting up External Content Type for SQL Server database using SQL Server authentication - SharePoint 2010 Foundation

This post is a follow up on the issues that I have got setting up External Content Type (ECT) on SharePoint 2010 Foundation that was going to connect to remote SQL Server database for information. I cannot use my SharePoint user accounts to access SQL Server. According to the information I have discovered ECT and Business Connectivity Services are available in the SharePoint 2010 Foundation, but there are some issues if you want to use authentication methods in your external connections that are different from Windows Identity or Current User Identity. This is because there is no Secure Store Service in SharePoint 2010 Foundation which serves as an impersonation hub and is only available in SharePoint 2010 Server edition. The issues are coming from the fact that you can actually create ECT in SharePoint Designer 2010 providing just Secure Store ID and system would ask you for credentials and here you go, but when you try to use your ECT in External Lists or as a lookup columns you w...

SharePoint 2013 Development and Consulting - Laptop & Conferencing Experience with Lync

Have just jumped on a brand new SharePoint 2013 massive Intranet project and because of specific working conditions have found some items that required addressing almost immediately in order to continue the job. Maybe this will be interesting to someone else who is about to start SharePoint 2013 development to have an idea of what might be necessary. I started working with SharePoint using my own infrastructure in 2009. I used iMac with 320GB HDD and 4GB RAM running VMWare Fusion to virtualise Windows  environment ( SQL Server 2008, standalone SharePoint 2007, Visual Studio 2008, Office 2007). It was all-in-one virtual machine. A bit slow, but enough for any SharePoint work that I had at that time. For communication with colleagues and partners we used GoToMeeting . Great tool that worked (and still works) without any problems. Voice, video, screen-sharing - all worked well using built-in audio/video hardware. In 2010 I got a MacBook Pro  with 500GB HDD and 8GB ...

Document Sets - SharePoint 2010 - Part 1

Hi again, in this post I am going to demonstrate how set up and start using Document Sets in SharePoint 2010. In Beta version there was a little problem when working with Document Sets. You could see the discussion around it here: Document Set content type issue . Now it is fixed and I will show you how to set up Document Sets properly to also use Keywords. 1. Activate two site collection features - Document Sets and Document ID Service: 2. Select a document library settings where you want to implement Document Sets. In my case it is Shared Documents. When you have selected the settings go to Advanced settings and then allow content types management: 3. Add an existing content type called Document Set: 4. Now I want to create a new Document Set. I have a sales opportunity and I have two documents related to this sales opportunity. So first I select New Document -> Document Set command, then provide name and description and there it is: 5. To check if our Document ID...