Archiv der Kategorie: Dynamics CRM

Manage Dynamics CRM 2011 with PowerShell

With the beta of Dynamics CRM 2011, Microsoft offers the ability to script deployment management tasks with PowerShell.

The server role ‚Deployment Tools‘ contains a set of PowerShell cmdlets to script Deployment Manager tasks.

After you have installed the Deployment Tools role you are able to add the Dynamics CRM PowerShell-Snap-In to your PowerShell session.

To use the cmdlets you have to enter following command into a PowerShell console, which adds the PowerShell-Snap-In to your active session.

Add-PSSnapin Microsoft.Crm.PowerShell

After this the CRM Snap-In will be listed when you enter this command

Get-PSSnapIn

Make sure that the result contains

Name : Microsoft.Crm.PowerShell
PSVersion : 2.0
Description : A Windows PowerShell snap-in containing cmdlets that manage components of Microsoft Dynamics CRM.

Now, you can view the cmdlets which are defined in the Snap-In with following command

Get-Command -Module Microsoft.Crm.PowerShell

At the moment of writing this post, following cmdlets are available

Disable-CrmOrganization
Disable-CrmServer
Edit-CrmOrganization
Enable-CrmOrganization
Enable-CrmServer
Get-CrmCertificate
Get-CrmDefaultOrganization
Get-CrmDeploymentAdministrator
Get-CrmEtmSettings
Get-CrmOrganization
Get-CrmServer
Get-CrmSetting
Get-CrmThrottleSettings
Import-CrmOrganization
New-CrmDeploymentAdministrator
New-CrmOrganization
Remove-CrmCertificate
Remove-CrmDeploymentAdministrator
Remove-CrmOrganization
Remove-CrmServer
Set-CrmCertificate
Set-CrmEtmSettings
Set-CrmSetting
Set-CrmThrottleSettings
Update-CrmOrganization

This is a great enhancement for managing a crm deployment. You can speed up your administrative tasks, if you add this Snap-In to your profile. See http://technet.microsoft.com/en-us/library/dd315342.aspx

Why OptimizeOrgImport is not always an optimization

Update Rollup 8 introduced an improved organization import process which speeds up the organization import.

You have to enable the optimized import process by adding the DWORD key OptimizeOrgImport with value 1 to the registry hive of Dynamics CRM (see the detailed description http://support.microsoft.com/kb/977867)

Recently, I had a problem during an import of an organization which failed with following error

Message: Exception during import of organization (Name=‘‚, Id=3e58a0cc-c277-df11-b2c1-0050569473db):
System.Data.SqlClient.SqlException: A row with a duplicate key cannot be added to the object ‘dbo.SystemUserOrganizations’-object with unique index ‘SystemUserOrganizations_CrmUserId’

The organization which I tried to import was a backup of an organization which was created in the same deployment. The import runs successful on every other deployment. With help of the Microsoft Support, we identified the cause of the issue: the OptimizeOrgImport key.

The knowledgebase article for OptimizeOrgImport contains following passage:

When you use the registry entry OptimizeOrgImport and have the value of this entry set to 1, you cannot import the same organization database more than one time.
If you want to import the same organization database more than one time, you must do one of the following things:

  • Set OptimizeOrgImport = 0 and use the normal import process.
  • Delete the organization, and then import the organization again.

This passage is a little bit misleading, as the optimized import process fails already at the first time, if the organization was created in the same deployment. In order to import a backup of an organization into the originating deployment, you have to deactivate OptimizeOrgImport.

Howto: Use pre-generated Guids for records

A customer asked me recently if it is possible to get the id of a record in a pre-create plugin. By default, it is not possible because the record is not yet created at this point and has not got an id.

However, Dynamics CRM allows you to create your own id for a record. The primary key of a record is stored in its Key-Property. On all default entities, for example account, the id property is marked as Valid for create (see accountid). This means, the sdk allows you to pass your own id.

using System;
using Microsoft.Crm.Sdk;
 
namespace PreCreateKeyPlugin
{
    public class PreCreateKeyPlugin : IPlugin
    {
        public void Execute(IPluginExecutionContext context)
        {
            if (IsPreCreate(context))
            {
                if (context.InputParameters.Contains(ParameterName.Target))
                {
                    DynamicEntity target = context.InputParameters[ParameterName.Target] as DynamicEntity;
 
                    if (target != null)
                    {
                        Guid customId = new Guid("{d7256b93-a5b5-45f9-9f2d-a1838279c35c}");
 
                        target["accountid"] = new Key(customId);
                    }
                }
            }
        }
 
        private bool IsPreCreate(IPluginExecutionContext context)
        {
            return  context.MessageName == MessageName.Create && 
                    context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction;
        }
    }
}

If you register this plugin for pre-create on the account entity and create a new account, it will have the provided id.

Please keep in mind, that you are in the responsibility to assign an new GUID for every execution of this plugin, but this should be clear when working with primary keys.

Howto: Get label for an attribute with JavaScript

If you have to retrieve the label of an attribute in a form script, you have two options. The first one, is of course not supported, but quick and easy to implement.

function GetFieldLabel(fieldname)
{
  var field = crmForm.all[fieldname+ '_c'];
 
  if (field != null){
    return field.firstChild.firstChild.nodeValue;
  }
  else {
    return '';
  }
}
 
alert(GetFieldLabel('subject'));

The other option is, to retrieve the label for the user language from the crm service. I will cover this in another article.