The scenario:
One of our current projects required that we had to create a SharePoint list item (task) when specific condition occurs in CRM - "Responsible person" is selected on opportunity level.
The solution:
1. Create web service on SharePoint Server
Often, you will have you SharePoint infrastructure reside on different server than CRM. Then, you cannot access the object model directly so you need to create a web serivice. Decide also on service authentication.
In our case - the web service creates an list item (task) in SharePoint, and fills it with the necessary properties.2. Create custom workflow activity2.1. Create sharepoint workflow activity library. Add CRM assemblies (from CRM SDK). Annotate class with appropriate attributes.
[CrmWorkflowActivity("Crm Workflow Activity", "SharePoint Activity")] public partial class SharePointActivity: SequenceActivity { public SharePointActivity() { InitializeComponent(); } }
2.2. Register dependency properties, which will then be fed from CRM
public static DependencyProperty responsiblePersonProperty = DependencyProperty.Register("responsiblePerson", typeof(string), typeof(SharePointActivity)); [CrmInput("responsiblePerson")] [CrmDefault(@"")] public string responsiblePerson { get { return (string)base.GetValue(responsiblePersonProperty); } set { base.SetValue(responsiblePersonProperty, value); }}
2.3. Call the SharePoint web service. For simplicity - no error handling & no security calling the service.
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { SamsService sams = new SamsService(); sams.CreateTask(responsiblePerson); return ActivityExecutionStatus.Closed; }
3. Register the custom workflow activity in CRM and enable debugging
Registration is pretty straighforward with tools in CRM SDK (plugin registration). CRM workflows are run "assyncronously" by CRM Assyncronours Service - this means that in order to debug your custom workflow activites, you need to attach to this service, not to w3wp. Also, your code is run with service's credentials (network service by default), so you might experience security issues.
Remember also iisreset & restart assync service on every change
4. Create CRM workflow that uses the custom activity
The workflow is started automatically when opportunity is created. The workflow waits untill "Reponsible person" is not blank. Then the workllow executes our custom workflow activity for SharePoint integration, passing all the required properties.
You can pass data from opportunity level, or any related entites such as Primary Contact or Account to every custom activity's dependency property that has been marked with the CrmInput attribute.
Sincerely,Rossen