The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context. This scenario is not supported. See “Impersonation Overview” in Books Online.


I got this error while I was writing code to retrieve data from CRM’s filtered view.

Here we just need to modify the connection string by adding Pooling=false

SqlConnection connection = new SqlConnection(“Data Source=dsname;Initial Catalog=dbname ;Integrated Security=SSPI; Pooling=false);

Bye

Using filtered view in Callout and Plugin


For using filtered view within the callout/plugin I used the following code

SqlConnection connection = new SqlConnection(“Data Source=dsname;Initial Catalog=dbname ;Integrated Security=SSPI;”);

string sqlQuery = @”select * from filteredOpportunity”;

SqlCommand cmd = new SqlCommand(sqlQuery, connection);

connection.Open();

//// your logic here

connection.Close();

 

But this code wasn’t returning any results .It was because callout/plugin run under the context of NT Authority\Network Service.

So we need to use impersonation in this case, for this we can use the following code

 

SqlConnection connection = new SqlConnection(Data Source=dsname;Initial Catalog=dbname ;Integrated Security=SSPI; Pooling=false”);

string sqlQuery = @” SETUSER ‘domainname\administrator’ select * from filteredOpportunity;

SqlCommand cmd = new SqlCommand(sqlQuery, connection);

//// your logic here

connection.Open();

 

SETUSER – To impersonate the admin who has access to the filtered views. More about SETUSER

http://msdn.microsoft.com/en-us/library/ms186297.aspx

 

Pooling-False This is important otherwise we will get the below error

 

The connection has been dropped because the principal that opened it subsequently assumed a new security context, and then tried to reset the connection under its impersonated security context.

Check this wonderful post as well

http://blogs.msdn.com/b/crm/archive/2007/05/21/writing-crm-callouts-with-filtered-views.aspx

 

Bye

 

Passed Exam MB2-634: CRM 4.0 Extending Microsoft Dynamics


Today I cleared the extension exam for Microsoft Dynamics CRM 4.0 with score of 82.

For version 3.0 the number of questions were 40 and passing score was 80.

This time around for version 4.0 there were 50 questions but how much is the passing score it wasn’t mentioned anywhere.

For preparation i used the the course material 8969 and CRM as a Rapid Development Platform book.

CRM as a Rapid Development Platform – I found this book really very useful..

Although from exam point of view the course material 8969 is more than enough !!!

Bye ..

The request failed with HTTP status 401: Unauthorized. Mandatory updates for Microsoft Dynamics CRM could not be applied successfully


We faced this problem while installing  CRM outlook client in one of our laptops.

I  followed the instructions mentioned below which i found while searching for the same.

On the client computer open the control panel.
Open the User Accounts icon.
Click the Advanced tab.
Click Manage Passwords.
Click Add.
Enter the server name of the CRM server
Enter the username as domain\username
Enter the password.
Click OK, then click Close and then click OK.

 

On the client open IE, click the Tools menu and then click Internet Options.
Click the Security tab.
Click the Local Intranet icon.
Click the Sites button.
Click the Advanced button.
Enter the website for access Microsoft CRM and then click the Add button.
Click OK 3 times.
Close any open IE sessions.

The problem got solved for us !

Bye..

Developing Custom Workflow Activities in CRM 4.0


We can create our own custom workflow activities and register it so that they can appear inside workflow editor in CRM.

For creating custom workflow activity we need to first select Workflow Activity Library project template inside Visual Studio with framework version 3.0.

Class within the activity library should derive from Activity class or could derive from SequenceActivity class.

Let’s create a simple custom workflow class

For this add a class inside your Workflow Activity Library project.

Add reference to Microsoft.Crm.Sdk and Microsoft.Crm.SdkTypeProxy dlls.

using System.Workflow.ComponentModel;

using System.Workflow.Runtime;

using System.Workflow.Activities;

using System.Workflow.Activities.Rules;

using Microsoft.Crm.Workflow; 

namespace MyWorkflow

{

[CrmWorkflowActivity(“My First Custom Activity”,“My Custom Activity Group”)]

public class MySimpleCustomActivity :Activity

{

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

// custom logic

return ActivityExecutionStatus.Closed;

}

}

}

 

CrmWorkflowActivity Attribute – To allow our activity to be visible inside the workflow editor. First parameter is the name of the workflow activity and second parameter is the group name, which could be used for grouping similar kind of workflow activities.

We can create custom activity by inheriting SequenceActivity class, which would be a composite activity wherein we can add other activity available with windows workflow foundation like

IfElseActivity,IfElseBranchActivity,PolicyActivity,DelayActivity etc.

For this right click on your project and add a new Activity in your project.

Open it in design mode. We can see the option of (Drop Activities Here)

Therein we can drop the built in activities found in windows workflow foundation for building our custom logic. Here we need not override the Execute method like we did in earlier case. The sequence activity will execute the activities that we would have dragged and dropped for writing our logic.

[CrmWorkflowActivity(“My First Custom Activity”, “My Custom Activity Group”)]

 

public partial class Activity3: SequenceActivity

{

public Activity3()

{

InitializeComponent();

}

}

 

While building our workflow we could also define input and output parameter that will appear inside Set Properties dialog box while defining Step and Dyanmic Value editor inside Form Assistant respectively.

To define input and output parameters we need to make use of dependency property and CrmInput and CrmOutput Attribute.

[CrmInput(“Name”)]

[CrmDefault(“Nishant”)]

public string InputProperty

{

get { return (string)GetValue(InputPropertyProperty); }

set { SetValue(InputPropertyProperty, value); }

}

 

public static readonly DependencyProperty InputPropertyProperty =

DependencyProperty.Register(“InputProperty”, typeof(string), typeof(nameOfActivityClass));

 

Similary we can define output parameter only difference would that we will use [CrmOutput(“FullName”)] attribute.

CrmDefault attribute is optional one.

 

The same property could act as both input as well as output parameter.

 

[CrmInput(“FName”)]

[CrmOutput(“LName”)]

[CrmDefault(“Nishant”)]

public string MyProperty

{

. . . .

 

 

The following data type are supported that could be used as input/output parameter

 

String, CrmNumber, CrmDecimal, CrmFloat, CrmMoney , CrmDateTime, CrmMoney, CrmBoolean, Status, Picklist, Lookup.

 

For lookup we can specify the parameter as following

 

 

[CrmInput(“Territory”)]

[CrmReferenceTarget(“territory”)]

public Lookup TerritoryProperty

{

get { return (Lookup)GetValue(TerritoryPropertyProperty); }

set { SetValue(TerritoryPropertyProperty, value); }

}

 

public static readonly DependencyProperty TerritoryPropertyProperty =

DependencyProperty.Register(“TerritoryProperty”, typeof(Lookup), typeof(Activity1));

For picklist

[CrmInput(“AddressType”)]

[CrmAttributeTarget(“account”,“address1_addresstypecode”)]

 

public Picklist AddressTypeProperty

{

get { return (Picklist)GetValue(AddressTypePropertyProperty); }

set { SetValue(AddressTypePropertyProperty, value); }

}

 

public static readonly DependencyProperty AddressTypePropertyProperty =

DependencyProperty.Register(“AddressTypeProperty”, typeof(Picklist), typeof(Activity1));

 

Using CrmService and MetadataService within custom workflow activity through workflow context

We can register our own custom services with workflow runtime. The built in services provided by Windows workflow foundation are following

PersistenceService, QueuingService, SchedulerService, TransactionService, TrackingService etc.

Similary MS CRM has used it to register ContextService with the runtime. It provides the current context information for the workflow instance which is executing at that time.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));

IWorkflowContext context = contextService.Context; 

ICrmService crmService = context.CreateCrmService(); 

IMetadataService crmMetadataService = context.CreateMetadataService(); 

 

return ActivityExecutionStatus.Closed;

 

}

For registering the custom activity we need to use Plugin registration tool.

Open Plugin Registration Tool

Select Register New Assembly

Select you assmebly ( It should be signed assembly and no need to specify anything else within the plugin registration tool).

Click on Register Selected Plugin

Restart Crm Asynchronous Service

Bye..

Understanding DynamicEntity in Microsoft Dynamic CRM.


We can use the DynamicEntity to develop against entities and attributes that are not defined at design time.

Or to write code that must work on entities/attributes that would be created/added after the code is deployed.

Let’s take an example of how to use DynamicEntity for creating and updating records using Dlls(Microsoft.crm.sdk and Microsoft.crm.sdktypeproxy) and also by using web service..

Setting up CrmService

CrmAuthenticationToken myToken = new CrmAuthenticationToken();

myToken.OrganizationName = “orgname”;

myToken.AuthenticationType = 0;

CrmService myService = new CrmService();

myService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myService.Url = http://servername:port/MSCrmServices/2007/CrmService.asmx”;

myService.CrmAuthenticationTokenValue = myToken;


Creating a contact record using DynamicEntity inside Microsoft.Crm.Sdk.dll

// Creating an instance of DynamicEntity Class

// Specifying the schema name of Contact entity whose record we are

// going to create

DynamicEntity myDE = new DynamicEntity();

myDE.Name = “contact”;

// Creating an instance of StringProperty to specify firstname attribute value

StringProperty myFirstName = new StringProperty();

myFirstName.Name = “firstname”;

myFirstName.Value = “Nishant”;

// Adding it to DynamicEntity’s properties

myDE.Properties.Add(myFirstName);

// Creating an instance of StringProperty to specify lastname attribute value

StringProperty myLastName = new StringProperty();

myLastName.Name = “lastname”;

myLastName.Value = “Rana”;

myDE.Properties.Add(myLastName);

try

{

contactGuid= myService.Create(myDE);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}


Updating a record using DynamicEntity


DynamicEntity myDEUpdate = new DynamicEntity();

myDEUpdate.Name = “contact”;

// Create a KeyProperty to hold the guid of the record to be updated

KeyProperty myContactGuid = new KeyProperty();

myContactGuid.Name = “contactid”;

Key myContactKey=new Key();

myContactKey.Value=contactGuid;

myContactGuid.Value = myContactKey;

myDEUpdate.Properties.Add(myContactGuid);

// Create a StringProperty with the new updated value

StringProperty myFirstNameU = new StringProperty();

myFirstNameU.Name = “firstname”;

myFirstNameU.Value = “Nishu”;

myDEUpdate.Properties.Add(myFirstNameU);

try

{

myService.Update(myDEUpdate);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}

The above code while using DynamicEntity class inside the WebService.

Create

DynamicEntity myDE = new DynamicEntity();

myDE.Name = “contact”;

StringProperty myFirstName = new StringProperty();

myFirstName.Name = “firstname”;

myFirstName.Value = “Mickey”;

StringProperty myLastName = new StringProperty();

myLastName.Name = “lastname”;

myLastName.Value = “Mouse”;

// myDE.Properties.Add(myLastName);

myDE.Properties = new Property[] {myFirstName, myLastName };

Guid mycontactGuid = new Guid();

try

{

mycontactGuid= myService.Create(myDE);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}

UPDATE

DynamicEntity myDEUpdate = new DynamicEntity();

myDEUpdate.Name = “contact”;

KeyProperty myContactGuid = new KeyProperty();

myContactGuid.Name = “contactid”;

Key myContactKey = new Key();

myContactKey.Value = mycontactGuid;

myContactGuid.Value = myContactKey;

StringProperty myFirstNameU = new StringProperty();

myFirstNameU.Name = “firstname”;

myFirstNameU.Value = “Donald”;

myDEUpdate.Properties = new Property[] { myContactGuid, myFirstNameU };

try

{

myService.Update(myDEUpdate);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}

In the same manner we can use Retrieve,Delete method with DynamicEntity.

There are certain Request classes that have a property ReturnDynamicEntities which indicates whether to return the result as a collection of instances of the that Entity or the DynamicEntity class.

Bye..