Using SharePoint CSOM (Client Side Object Model) in CRM 2011 On-Premise Plugin.

Hi,

We recently had a requirement to create List Item in SharePoint every time a case is resolved in CRM.

For this we decided to use SharePoint CSOM i.e.

Added reference to following assemblies in our plugin that will fire on Case Resolution

They can be found at

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\

The plugin gets the content of the KB Article associated to the Case and creates a list item (for a Custom List) in SharePoint.

Sample Code


public void Execute(IServiceProvider serviceProvider)
 {
 IPluginExecutionContext context =
 (IPluginExecutionContext) serviceProvider.GetService(typeof (IPluginExecutionContext));
 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
 {

Entity entity = (Entity) context.InputParameters["Target"];

OptionSetValue serviceStage = (OptionSetValue) (entity.Attributes["statecode"]);
 if (serviceStage.Value == 1)
 {

IOrganizationServiceFactory serviceFactory =
 (IOrganizationServiceFactory)
 serviceProvider.GetService(typeof (IOrganizationServiceFactory));

IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);

// Get the KB Article Associated
 ColumnSet cols = new ColumnSet();
 cols.AddColumn("kbarticleid");

// Get the content of the article
 Entity caseEntity = orgService.Retrieve("incident", context.PrimaryEntityId, cols);
 EntityReference entityRef = (EntityReference) caseEntity.Attributes["kbarticleid"];
 Guid kbId = entityRef.Id;
 Entity kbEntity = orgService.Retrieve("kbarticle", kbId, new ColumnSet(true));
 string title = kbEntity.Attributes["title"].ToString();
 string content = kbEntity.Attributes["content"].ToString();
 // create the custom list item in SharePoint using Client Side Object Model of SharePoint 2013

ClientContext clientContext = new ClientContext("http://sharepoint/sites/portal/");
 clientContext.Credentials = new System.Net.NetworkCredential("username", "password", "mydomain");
 List announcementsList = clientContext.Web.Lists.GetByTitle("Kb List");

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
 ListItem newItem = announcementsList.AddItem(itemCreateInfo);
 newItem["Title"] = title;
 newItem["Content"] = content;

newItem.Update();

clientContext.ExecuteQuery();

}
 }
 }

Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Using SharePoint CSOM (Client Side Object Model) in CRM 2011 On-Premise Plugin.”

  1. I am using CRM13 onpremise with SP2013 Online
    I am getting following error in plugin
    “The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel”

    Like

Share your thoughts

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