CRM 2011 – Early bound LINQ queries

I have been trying to get my head around LINK and OData today.  OData was really giving me the run around, trying to bring the sample into a piece of Javascript was very frustrating.

I am a big fan of Early bound data in CRM 2011, I just like to know what I am using rather than the dynamic late binding, strong typing means you catch any problems in the code when compiling it and saves you a bunch of time debugging misspelt entites in strings.

I couldn’t figure out how to get the results of a Linq query in a early bound entity.  In link you often just use the Var variable and then LINQ converts the results into the correct type.

The SDK also didn’t seem to have any examples but I had been reading about LINQ for the 70-515 .NET web apps exam.  I knew the result had to be IEnumerable so you can go through the results.

The quick answer is you have use this

IEnumerable<Account>

instead of a

Var

here is a snippet of my code.  if anyone can tell me the easiest way to display code in WordPress that would be appreciate, please leave me some advice in the comments.  This blog post has some good information on LINQ queries

_service = (IOrganizationService)_serviceProxy;

OrganizationServiceContext orgContext =  new OrganizationServiceContext(_service);

IEnumerable<Account> accounts = from a in orgContext.CreateQuery<Account>()

select new Account

{

Name = a.Name,
Address1_County = a.Address1_County
};

System.Console.WriteLine("List all accounts in CRM");                    
System.Console.WriteLine("========================");                   
 foreach (Account a in accounts) {                        
System.Console.WriteLine(a.Name + " " + a.Address1_County);                    
}                    

System.Console.WriteLine();                    
System.Console.WriteLine("<End of Listing>");                    
System.Console.WriteLine();                    
System.Console.WriteLine();

CRM 2011 – Functional and Syntax changes in Plugins

[tweetmeme source=”BenHosk” only_single=false]

I recently wrote a blog post about 5 plugin syntax changes but then I found this blog post which looks at the difference between plugins syntax in CRM 4 and CRM 2011

Changes in CRM 2011 plugins

There are a lot of syntax and functional changes in MSCRM 2011 plugins. I have listed some of them below:
1. New References
• System.ServiceModel
• System.Runtime.Serialization2. Microsoft.Crm.Sdk has been replaced by Microsoft.Xrm.Sdk

3. The default parameter for execute method has been changed from
IPluginExecutionContext to IServiceProvide

public void Execute(IServiceProvider serviceProvider)

4. IpluginExecutionContext has been changed to a type of service and to get the reference use the following syntax.

IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

5. ICrmService has been changed to IOrganizationService

IOrganizationServiceFactory serviceFactory =
IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

6. Dynamic entity has been changed to “Entity”

if (context.InputParameters.Contains(“Target”) &&
context.InputParameters[“Target”] is Entity)
{
Entity entity = (Entity)context.InputParameters[“Target”];
}

7. Dynamic Entity’s properties syntax has been changed to attributes

entity.Attributes.Contains(“accountnumber”)8. Strongly typed classes (account, contact) are missing in Microsoft.XRM.Sdk assembly but there is an exception to this.

9. CRM 2011 provides the crm utility called “crmsvcutil” to generate strongly typed classes that can be used in plugins.

10. Strongly typed classes has been changed to use like loosely typed classes.
Task task = new Task();
Task[“description”] = ”test description”

11. No more soap exception
catch (FaultException ex)

CRM 2011 – How to set up a lookup using Javascript

[tweetmeme source=”BenHosk” only_single=false]

I haven’t had to use this javascript yet but I know I am going to need in the future, so I am going to blog about it so I can find it.

there are quite a few examples of this but this blog post compares it to CRM 4 as well, which is extra useful.

CRM 4

var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].typename = typeValue;

crmForm.all.fieldName.DataValue = value;

CRM 2011

var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].entityType = typeValue;

Xrm.Page.getAttribute(“fieldName”).setValue(value);

How about doing it on one line like this instead.

CRM 4
crmForm.all.field.DataValue = [{id: idValue, name: textValue, typename: typeValue}];

CRM 2011
Xrm.Page.getAttribute(“fieldName”).setValue( [{id: idValue, name: textValue, entityType: typeValue}]);

if you want some more examples and a discussion this forum discussion is the place to look

This page also had an interesting link to an article

Seven JavaScript Things I Wish I Knew Much Earlier In My Career

which I found interesting because I know very little about Javascript

CRM 2011- how to debug Javascript with IE8

I was trying to debug some javascript today and found the best way to do this was to use Internet Explorers debugging options.

you can bring up the debugger in internet explorer if you have at least version 8.  You simply press the F12 button.   Then go to the Script tab and next to the Start Debugging button you will see the scripts, your script will probably be well down in the list.

Also you should set internet explorer options to untick the disable script debugging.

These blog tells you how to set it up

The error checking was very useful because it told you if there were syntax errors and you can see what line an error occured.