Wednesday, February 19, 2014

CRM - Adding an activity through code

There can be scenarios where we have to add activities programmatically to the CRM organization either through a plugin or through a workflow. Below is how we can achieve this.
 
Entity followupActivity = new Entity("phonecall");
followupActivity["subject"] = "Followup Phone call activity";
Entity activityPartyFrom = new Entity("activityparty");
activityPartyFrom.Attributes.Add("partyid", new EntityReference("systemuser", workflowContext.UserId));
followupActivity.Attributes.Add("from", new Entity[] { activityPartyFrom });
Entity activityPartyTo = new Entity("activityparty");
activityPartyTo.Attributes.Add("partyid", new EntityReference("lead", "Lead's Guid"));
followupActivity.Attributes.Add("to", new Entity[] { activityPartyTo });
followupActivity["scheduledend"] = DateTime.Now.AddDays(30);
service.Create(followupActivity);
 
 Here I am trying to add a phone call activity and assuming that this is from the workflow, workflowcontext used is the workflow context to get the owner of the activity. I am here trying to add a phone call and the entity should change according to our need. Due date is set to 30 days from today and the from is the user under which the workflow is running and to is a lead whose Guid we need to pass so as to set the To field.
 
Hope this helps !!!

Monday, February 17, 2014

CRM - Passing data between Plugins

When working with Plugins its quite natural to come across the scenarios where a data needs to be shared by two different plugins. This can be achieved using the shared variables. Below is how it can be done. In the plugin which is going to set the variable write the code as follows,
 
context.SharedVariables["UniqueName"] = "Data to be shared";
 
context is the plugin execution context. Now the data that needs to be shared is available in the shared variable UniqueName.
 
Next step is to consume the data available in the other plugin. For that first check whether the shared variable has the respective UniqueName. If available consume the data in it using the below code.
 
string uniqueName = (string)context.SharedVariables["PrimaryContact"];
 
All done. We have the data from first plugin in second one now. Enjoy !!!

Monday, February 10, 2014

WPF - Supressing Script Errors in WPF WebBrowser control

When Web sites hosted in Webbrowser controls are throwing JScript errors then we have to hide them. WebBrowser controls don't have the property to hide script errors so we have to handle it explicitly or we have to use WinForms WebBrowser control.
 
For the first approach we can use the below method in the browsers navigated event.

private void HideScriptErrors(WebBrowser webBrowser, bool hide)
{FieldInfo fieldInfo = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo == null)
return;
object objectWebBrowser = fieldInfo.GetValue(webBrowser);
if (objectWebBrowser == null)
return;
objectWebBrowser.GetType().InvokeMember("silent", BindingFlags.SetProperty, null, objectWebBrowser, new object[] { hide });
}

You can look at the original post here.

This worked for me. Hope it will for you as well !!! :)

Thursday, February 6, 2014

CRM 2013 - Create a SDK code project in Visual Studio

As a developer, there can be scenarios where we have to use CRM SDK in our code so as to connect to CRM and use the messages from SDK. To make sure our VS project is ready to use we have to follow some steps.

Even though the title says 2013 this is applicable to 2011 as well.
 

Prerequisites

Below are the prerequisites for the same.
  • Any Visual Studio (even the express edition works): CRM SDK is built on .NET 4.0 so even VS 2010 will work in this case.
  • CRM SDK: If CRM SDK is not installed on your computer, it can be downloaded from here (CRM 2011, CRM 2013).
  • Windows Identity Foundation: If you are using windows 8 or Windows server 2012, then we have to just enable already existing WIF. If you are using windows 7 or windows server 2008, we have install the WIF 3.5. For installing, download it from here.
 Once all the prerequisites are in place we can start creating a project which will interact with CRM from our code. Follow the below steps for achieving our target,
  1. Create a console application for either C# or VB according to our comfort level.
  2. Make sure the target framework is set to MS .NET framework 4.0. By default it will be client profile.
  3. Add all the required reference to our project. This includes below references.
    1. System.Data.Linq
    2. System.DirectoryServices.AccountManagement
    3. System.Runtime.Serialization
    4. System.Security
    5. System.ServiceModel
  4. Add the required SDK assembly reference as per our requirement. This is available from the bin folder of respective CRM version SDK.
  5. Add the required identity reference file. We can get this file from the file location in the system at Program Files\Reference Assemblies\Microsoft\Windows Identity Foundation\v3.5.
We are done with adding the references and now we are ready to go with our development activity.
 
For every project we have to do this. So its better if we can save this as a project template for our future references.
 
For detailed Microsoft documentation please refer the original site here.
Hope this helped everyone. :) !!!
 
 

Configuration for CRM Plugins

CRM plugins are always great feature where we can automate many functionality which we cannot automate from user interface. But almost eve...