PlugIn Impersonation In Microsoft Dynamics CRM 4.0(i.e Executing plugin code on behalf of another user.)


If we want to execute some code in behalf of another user not the calling user then we have to handle this in PlugIn code. Whatever the user selected during PlugIns registration, it doesn’t matter, code will alwaysexecute on behalf of user mentioned in code.Example Create a Record in CRM on behalf of another user.

 

try

                            {

                                CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();

                                token.AuthenticationType = 0;

                                token.OrganizationName = OrganizationName;

                              

 

                                // Create CrmService instance

                                CrmSdk.CrmService service= new CrmSdk.CrmService();

                                service.Url = CrmServiceUrl;

                                service.CrmAuthenticationTokenValue = token;

 

                                //execute on behalf of another user

                                service.Credentials = new System.Net.NetworkCredential(“username”,”password”,”domain”);

                                //service.CorrelationTokenValue = correlationToken;

                                //Create Proposal record

                                CrmSdk.new_proposal proposal = new CrmSdk.new_proposal();

                               

                                // set value of Opportunity Lookup in Proposal entity.

                                CrmSdk.Lookup oppLookup = new CrmSdk.Lookup();

                                oppLookup.type = EntityName.opportunity.ToString();

                                oppLookup.Value = new Guid(oppId);

 

                                proposal.new_opportunityid = oppLookup;

                                service.Create(proposal);

                            }

                            catch (SoapException ex)

                            {

                               // show message here.. 

                            }

Error Importing custom entity in Microsoft Dynamics CRM 4.0


While importing custom entity from one deployment to another deployment we are receiving this error

 

Failure: EntityName_BulkDeleteFailures: A SQL Server error occurred. Try this action again. If the problem…..

 

Resolution for this issue: I exported the customizations and removed the system reference to “BulkDeleteFailures” in xml before importing it. It works but it is unsupported.

Handling SOAP Exceptions In Microsoft Dynamics CRM


The only exception thrown by the CRM Web service is a common

SoapException whose message property is always “Server was unable to process

request.”

 

To handle that exception, catch the exception and display the InnerXml error

code, error text, and error description

 

Example

 

try

{

// Delete the non-existent Contact

service.Delete(EntityName.contact.ToString(), contactGuid);

}

catch (SoapException ex)

{

// This will contain the error message from the platform

Console.WriteLine(“Microsoft Dynamics CRM Error

Information:”);

Console.WriteLine(ex.Detail.InnerText);

// The InnerXml contains the details of the error in a

parsable XML format

XmlDocument error = new XmlDocument();

error.LoadXml(ex.Detail.InnerXml);

// Render out the details of the error

Console.WriteLine(“Error Code: ” +

error.SelectSingleNode(“/error/code”).InnerText);

Console.WriteLine(“Error Description: ” +

error.SelectSingleNode(“/error/description”).InnerText);

Console.WriteLine(“Error Type: ” +

error.SelectSingleNode(“/error/type”).InnerText);

}

 

 

Using the RetrieveMultiple Method in Microsoft Dynamics CRM 4.0


// Set up the CRM Service.

CrmAuthenticationToken token = new

CrmAuthenticationToken();

// You can use enums.cs from the SDK\Helpers folder to get

the enumeration for AD Authentication.

token.AuthenticationType = 0;

token.OrganizationName =”AdventureWorksCycle”;

CrmService service = new CrmService();

service.Url

=”http://<servername&gt;:<port>/mscrmservices/2007/crmservice.

asmx”;

service.CrmAuthenticationTokenValue = token;

service.Credentials =

System.Net.CredentialCache.DefaultCredentials;

// Create the ColumnSet indicating the properties to be

retrieved

ColumnSet cols = new ColumnSet();

// Sets the ColumnSet’s Properties

cols.Attributes = new string [] {“fullname”, “contactid”};

// Create the ConditionExpression

ConditionExpression condition = new ConditionExpression();

// Set the Condition for the Retrieval to be when the

contact’s address’ city is Sammamish

condition.AttributeName =”address1_city”;

condition.Operator = ConditionOperator.Like;

condition.Values = new string [] {“Sammamish”};

// Create the FilterExpression

FilterExpression filter = new FilterExpression();

// Set the Filter’s Properties

filter.FilterOperator = LogicalOperator.And;

filter.Conditions = new ConditionExpression[] {condition};

// Create the QueryExpression Object

QueryExpression query = new QueryExpression();

// Set the QueryExpression Object’s Properties

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

query.ColumnSet = cols;

query.Criteria = filter;

// Retrieve the Contacts

BusinessEntityCollection contacts =

service.RetrieveMultiple(query);

 

Using Update Method of CrmService WebService


// Set up the CRM Service.

CrmAuthenticationToken token = new

CrmAuthenticationToken();

// You can use enums.cs from the SDK\Helpers folder to get

the enumeration for AD Authentication.

token.AuthenticationType = 0;

token.OrganizationName =”AdventureWorksCycle”;

CrmService service = new CrmService();

service.Url

=”http://<servername&gt;:<port>/mscrmservices/2007/crmservice.

asmx”;

service.CrmAuthenticationTokenValue = token;

service.Credentials =

System.Net.CredentialCache.DefaultCredentials;

// Create the contact object.

contact contact = new contact();

// Set the contact object properties to be updated.

contact.address1_line1 =”34 Market St.”;

// The contactid is a key that references the ID of the

contact to be updated.

contact.contactid = new Key();

// The contactid.Value is the GUID of the record to be

changed.

contact.contactid.Value = new Guid(“4D507FFE-ED25-447B-

80DE-00AE3EB18B84″);

// Update the contact.

service.Update(contact);

Using Create Method of CrmService WebService


// Set up the CRM Service.

CrmAuthenticationToken token = new

CrmAuthenticationToken();

// You can use enums.cs from the SDK\Helpers folder to get

the enumeration for AD Authentication.

token.AuthenticationType = 0;

token.OrganizationName =”AdventureWorksCycle”;

CrmService service = new CrmService();

service.Url

=”http://<servername&gt;:<port>/mscrmservices/2007/crmservice.

asmx”;

service.CrmAuthenticationTokenValue = token;

service.Credentials =

System.Net.CredentialCache.DefaultCredentials;

// Create the column set object that indicates the

properties to be retrieved.

ColumnSet cols = new ColumnSet();

// Set the properties of the column set.

cols.Attributes = new string [] {“fullname”};

// contactGuid is the GUID of the record being retrieved.

Guid contactGuid = new Guid(“4D507FFE-ED25-447B-80DE-

00AE3EB18B84″);

// Retrieve the contact.

// The EntityName indicates the EntityType of the object

being retrieved.

contact contact =

(contact)service.Retrieve(EntityName.contact.ToString(),

contactGuid, cols);

 

Using the CrmDiscoveryService Web Service In CRM4.0: On-Premise


CrmDiscovery service is a global installable level service that allow the caller to determine the correct organization and URL for his/her need.

Below example show how to create and configure CrmDiscoveryService Web Service proxy.

 

// Create and configure the CrmDiscoveryService Web service

proxy.

CrmDiscoveryService discoveryService = new

CrmDiscoveryService();

discoveryService.UseDefaultCredentials = true;

discoveryService.Url

=”http://localhost/MSCRMServices/2007/AD/CrmDiscoveryServic

e.asmx”;

// Retrieve the list of organizations that the logged on

user belongs to.

RetrieveOrganizationsRequest orgRequest = new

RetrieveOrganizationsRequest();

RetrieveOrganizationsResponse orgResponse =

(RetrieveOrganizationsResponse)discoveryService.Execute(org

Request);

// Locate the target organization in the list.

OrganizationDetail orgInfo = null;

foreach (OrganizationDetail orgDetail in

orgResponse.OrganizationDetails)

{

if

(orgDetail.OrganizationName.Equals(“AdventureWorksCycle”))

{

orgInfo = orgDetail;

break;

}

}

// Check whether a matching organization was not found.

if (orgInfo == null)

throw new Exception(“The specified organization was not

found.”);

 

%d bloggers like this: