Wednesday, November 24, 2010

CRM 4 Plugins – Read Entity Attributes within the Plugin


CRM 4.0 Plugins are a way to integrate custom business logic into CRM 4.0's platform. Here are the high-level steps to follow in plugin creation and registration. For the plugin registration, Plugin Registration Tool for CRM 4.0 delivered with CRM 4.0 SDK is used.

Implement Plugin

  1. Visual Studio class library project can be used to implement plugins
  2. Inherit the class from IPlugin class
  3. Implement "Execute" method taking IPluginExecutionContext type context as a parameter as follows

    public void Execute(IPluginExecutionContext context)

  4. Here is a sample "Execute" method implemented for Opportunity entity

public void Execute(IPluginExecutionContext context)
{
string m_OpportunityName = string.Empty;
string m_OpportunityId = string.Empty;
if (context.MessageName.Equals("Create"))
{
// Entity creation.
DynamicEntity postEntity;
if (context.PostEntityImages.Contains("Target"))
{
postEntity = ((DynamicEntity)context.PostEntityImages["Target"]);
if (postEntity.Properties.Contains("opportunityid"))
{
Key opportunityId = (Key)postEntity.Properties["opportunityid"];
if (opportunityId != null)
{
m_OpportunityId = opportunityId.Value.ToString();
}
}
if (postEntity.Properties.Contains("name"))
{
m_OpportunityName = postEntity.Properties["name"].ToString();
}
}
}
else if (context.MessageName.Equals("Update"))
{
// Entity update.
DynamicEntity pretEntity;
DynamicEntity postEntity;
if (context.PreEntityImages.Contains("Target"))
{
pretEntity = ((DynamicEntity)context.PreEntityImages["Target"]);
}
if (context.PostEntityImages.Contains("Target"))
{
postEntity = ((DynamicEntity)context.PostEntityImages["Target"]);
}
if (postEntity.Properties.Contains("opportunityid"))
{
Key opportunityId = (Key)postEntity.Properties["opportunityid"];
if (opportunityId != null)
{
m_OpportunityId = opportunityId.Value.ToString();
}
}
if (postEntity.Properties.Contains("name"))
{
m_OpportunityName = postEntity.Properties["name"].ToString();
}
}
}

Register Plugin

Copy plugin dll(s) to "……\Program Files\Microsoft Dynamics CRM\Server\bin\assembly"

Run "Plugin Registration Tool" come with the SDK to register the plugin for an entity.

Then create a step for "Create" and another step for "Update". "Create Step" executes the plugin in new entity instance creation, while "Update Step" executes the plugin in entity update.

Then create a "Post Image" for "Create Step" and "Pre Image" and a "Post Image" for "Update step".

That is it. If the Steps are created as "Synchronous", plugin can be debugged by attaching to "w3wp.exe" process. If the Steps are created as "Asynchronous", plugin can be debugged by attaching to "CrmAsyncService.exe" process.

One thing that should be noted is, Active Directory account used to deploy and register new assembly has to be added into "Deployment Administrators" group in CRM Deployment Manager. Having assigned only "System Administrator" role is not sufficient for this task.

Wednesday, November 3, 2010

How to Get Row Count of All Tables - SQL Server

For this purpose doing a rowcount for each table will take forever, if the DB is fairly large. In both of following ways we don't count rows of each table, instead just look in SQL Server where it is already stored.

Query 1:
SELECT
[TableName] = O.name,
[RowCount] = MAX(I.rows)
FROM
sysobjects O,
sysindexes I
WHERE
O.xtype = 'U' AND I.id = OBJECT_ID(O.name)
GROUP BY O.name
ORDER BY [TableName] ASC


Query 2:
SELECT
'['+SCHEMA_NAME(T.[schema_id])+'].['+T.name+']' AS [Table_Name]
,I.rows AS Row_Count
FROM sys.tables T
INNER JOIN sysindexes I ON (T.object_id = I.id AND I.indid < 2
ORDER BY [Table_Name]


It should be noted that both of above queries give an approximate count only, because of sysindexes table is usually a little bit inaccurate, due to it is not updated constantly.

Friday, September 24, 2010

How to get a DLL Already Deployed in GAC

This is a simple task as mapping the Windows assembly folder as a network drive and opening respective folder with equivalent dll name. But since I’m always forgetting it, finally thought of blogging ...

First we need to add our assembly folder as a Network Drive by Tools -> Map Network Drive or by My Computer -> Right Click -> Map Network Drive


Specify the path to assembly folder as follows.


You will get a Network Location mapped. Go inside…



Open GAC_MSIL folder.

You will find a folder for each .dll deployed into the Global Assembly Cache with the same name as dll.


Monday, September 13, 2010

Running Dynamics CRM 4.0 with a Service Account

When I install CRM 4.0, I got the warning message "Verify Domain User account SPN for the Microsoft Dynamics CRM ASP.NET Application Pool account" in system requirements window. But it did not prevent me from installing CRM, so I proceeded.



After successfully completing other steps I couldn't browse CRM page. I was getting an error message saying "Caller does not have enough privilege to set the CallerOriginToken to the specified value.” To resolve this problem I had to follow following steps.

1. Using Setspn command line tool, setup the SPNs for the machine and service account as follows
.
setspn –a HOST/servername.domainabc:5555 domainabc\serviceusername


The Setspn command line tool is included in Windows Server 2003 Support Tools and you can download it from the Microsoft Download Center in this location.
You can find "Setspn Overview" from Microsoft TechNet in this location.

2. Verify/ Add the Microsoft Dynamics CRM server to the CRM installation’s Active Directory PrivUserGroup group by following these steps.

2.1 Locate the correct security group if you have more than one deployment of Microsoft Dynamics CRM 4.0 in the domain. Run the following SELECT statement on MSCRM_Config database using SQL Server Management Studio.

SELECT ID, DatabaseName, FriendlyName FROM Organization

2.2 Add the Microsoft Dynamics CRM server to the Active Directory PrivUserGroup group.
...1. Start -> Run -> dsa.msc, then click OK.

...2. Locate the organizational unit in which the Microsoft Dynamics CRM installed.

...3. Double click PrivUserGroup, click Members, and then click Add.

...4. Click Object Types, click Computers, and then click OK.

...5. Use Check Names option to find out Microsoft Dynamics CRM server, and then click OK.

3. Add the service account to the local machines IIS_WPG group.

4. Restart Microsoft Dynamics CRM server.
5. Restart Microsoft Dynamics CRM client for Outlook.

Tuesday, June 15, 2010

Issue in Embedding a Flash Image Rollup – SharePoint 2010

My goal is to embed a Flash control in a SharePoint site that displays several images iteratively. So one approach is, upload the .SWF file and the images to a web location, preferably a library in the same SharePoint site. Then can develop a custom WebPart control or use content editor WebPart to include following code segment.

<div>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/
cabs/flash/swflash.cab#version=9,0,28,0"

width="310" height="160">
<param name="movie" value="/FlashImages/ControlFiles/abc.swf" />
<param name="quality" value="high" />
<param name="flashvars" value="configXML=/FlashImages/ControlFiles/data.xml"/>
<embed src="/FlashImages/ControlFiles/abc.swf"
flashvars="configXML=/FlashImages/ControlFiles/data.xml"
width="310" height="160" quality="high"
pluginspage="http://www.adobe.com/shockwave/
download/download.cgi?P1_Prod_Version=ShockwaveFlash"

type="application/x-shockwave-flash">
</embed>
</object>
</div>

In here data.xml contains the URLs of the images to be displayed, which is taken as a parameter by the swf. Basically that is it. Flash control should work properly. But we need one more additional step in order to flash control to work as expected. That is we have to set “Browser File Handling” to
“Permissive”. By default this option is set to “Strict”, which prevents flash control performing as expected.

So in SharePoint Central Administration;

  • Go to “Application Management” > “Manage web applications”
  • Select your Web Application and go to “General Settings” > “General Settings”


  • Select “Permissive” option in the Browser File Handling section as following screen shot.