A retrieve request to CrmService or the pre/post image does not seem to contain all the entity attributes.

You are developing a plug in for Microsoft CRM 4.0.

Long story short, the IPluginExecutionContext instance passed to the plugin code has an InputParameters property who might contain a “Target” property. This property may be your entity so most common code examples will try to cast it to a DynamicEntity.

DynamicEntity entity = context.InputParameters.Properties["Target"] as DynamicEntity;

If you end up with a non null value you are probably in possession of your entity.

By default the code that you write in the plugin will have access under the DynamicEntity.Properties collection to only attributes of the entity that just got created or updated or deleted.

To access all the attributes, even the one that did not change you can either spawn an instance of the CrmWebService calling contect.CreateCrmWebService or register for a pre/post image.

Now, let us get to the problem that I encountered. Inspecting the entity.Properties items I noticed that they are less, … way less than the total number of attributed the entity/table has.
It took me a while until I realized that the DynamicEntity.Properties collection will not contain attributes that have null (or Nothing) values even when you implicitly requested the entity.

This is something I read in a book so while I was trying to understand what is happening the answer was actually lurking in the recesses of my brain. When I inspected the database record of the entity I just created it struck me.

Hopefuly this will save you some time if you get here first. 😀

Cheers.

Creating a linked server dynamically and executing a query against it.

Sometimes from a SQL script running on server A we need to execute a query against a database on a different SQL server B or against any other kind of database. If this other database can normally be linked to your main SQL server A than you can definitelly do that at design time and solve your problem. The question is can you do it at run time?

I mean what if you do something in a SQL script on your main server A and you have to reach out and fetch some information from another database on a server whose names are passed as parameters or read from a config file.

The script below shows how to link a Sql database on another server dynamically at runtime and then issue a query against a table from that database.

DECLARE @v_ServerName varchar(100)
DECLARE @qry varchar(500), @v_DataBaseName varchar(50)

IF NOT EXISTS (SELECT 1 Where Exists (Select [SERVER_ID] From sys.servers WHERE [Name]=@v_ServerName))
EXEC sp_addlinkedserver @v_ServerName, N'Any', N'SQLNCLI', @v_ServerName;
SET @qry = 'SELECT * FROM [' + @v_ServerName + '].[' + @v_DataBaseName + '].[dbo].[TableName] WHERE

EXECUTE (@qry);

Try it and let me know it worked for you.