Thursday, July 28, 2011

5 syntax changes in Dynamics CRM 2011 plugins

There are number of changes between Dynamics CRM 2011 SDK and CRM 4 SDK. Let’s take a look at what has changed between plugins.
1. The IPlugin now resides in Microsoft.Xrm.Sdk namespace instead of Microsoft.Crm.Sdk
public class ClassName : Microsoft.Crm.Sdk.IPluginpublic class ClassName : Microsoft.Xrm.Sdk.IPlugin



2. The Execute method signature of the IPlugin interface has changed, it now expects an IServiceProvider instead of the IPluginExecutionContext.
public void Execute(IPluginExecutionContext context)
public void Execute(IServiceProvider serviceProvider)


3. To get a reference to the IPluginExecutionContext you now need to call the GetService method of the IServiceProvider interface.
public void Execute(IPluginExecutionContext context)
Microsoft.Xrm.Sdk.IPluginExecutionContext context =(Microsoft.Xrm.Sdk.IPluginExecutionContext)
    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));


4. ICrmService has been changed to IOrganizationService.
ICrmService sdk = context.CreateCrmService(true);
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService sdk = factory.CreateOrganizationService(context.UserId);


5. DynamicEntity has been changed to Entity.    ·         Properties property of DynamicEntity no longer exists    ·         Getting and Setting values no longer use *Property classes,
      eg: no more KeyProperty, StringProperty…etc, simply do int value = (int)entity[“schema_name”];
if (context.InputParameters.Properties.Contains("Target") &&
    context.InputParameters.Properties["Target"is DynamicEntity)
if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"is Entity)

 

List of changes : Type mapping between CRM 4.0 and CRM 2011

In Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online, the programming model has been changed to use native .NET types whenever possible. A beautiful post from Resultondemand explains it all, with all credit to their great job the excerpts of the post is :
Type Mapping Between Versions
The following table shows the mapping between the defined type for an entity attribute, the type that is used in a record, and the type that was used in Microsoft DynamicsCRM4.0.
Attribute Type Microsoft Dynamics CRM 2011 Type Microsoft DynamicsCRM4.0 Type
AttributeTypeCode.Booleanbool or System.BooleanCrmBoolean
AttributeType.CalendarRulesEntityCollectionDynamicEntity[] or calendarrule[]
AttributeType.CustomerEntityReferenceCustomer
AttributeType.DateTimeSystem.DateTimeCrmDateTime
AttributeType.Decimaldecimal or System.DecimalCrmDecimal
AttributeType.Doubledouble or System.DoubleCrmFloat
AttributeType.Integerint or System.IntegerCrmNumber
AttributeType.
Internal
System.Object
Not used in records.
Not used in records.
AttributeType.LookupEntityReferenceLookup
AttributeType.Memostring or System.StringSystem.String
AttributeType.MoneyMoneyCrmMoney
AttributeType.OwnerEntityReferenceOwner
AttributeType.PartyListEntityCollection or ActivityParty[]activityparty[] or DynamicEntity []
AttributeType.PicklistOptionSetValuePicklist
AttributeType.PrimaryKeySystem.GuidKey
AttributeType.StringSystem.StringSystem.String
AttributeType.StateOptionSetValue or enumeration generated for the entity stateEntityNameStateInfo
AttributeType.StatusOptionSetValue orintStatus
AttributeType.UniqueidentifierSystem.GuidUniqueIdentifier
AttributeType.VirtualSystem.Object
Not used in records.
Not used in records.
Other Type Changes
Old Type New Type
CrmAttributeType Class (MetadataService)Microsoft.Xrm.Sdk.Metadata.AttributeTypeCode
Moniker Class (CrmService)Microsoft.Xrm.Sdk.EntityReference
SecurityPrincipal Class (CrmService)Microsoft.Xrm.Sdk.EntityReference
Happy Coding Smile

Sunday, July 17, 2011

Using CRM 2011 Service to Create Record from Code C# - IOrganizationService

// sample to test the method   
private void CreateAccount(string AccountName)
    {
        IOrganizationService _service = GetCRMService("http://<ServerName>:<PortNumber>", "<UserName>", "<Password>", "<Domain>");
        try
        {
            Entity account = new Entity("account");
            account["name"] = AccountName;
            _service.Create(account);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

// GetCRMService   
    public static IOrganizationService GetCRMService(string ServerURL, string UserName, string Password, string DomainName)
    {
        ClientCredentials credentials = new ClientCredentials();
        credentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, DomainName);
        Uri organizationUri = new Uri(ServerURL + "/<OrgName>/XRMServices/2011/Organization.svc");
        Uri homeRealmUri = null;
        OrganizationServiceProxy orgService = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
        IOrganizationService _service = (IOrganizationService)orgService;
        return _service;
    }