Using LINQ in CRM 2013


There are few ways we can make use of LINQ to write queries against CRM.

Using the early bound entity classes along with the generated service context

Use the CrmSvcUtil tool to generated the early bound entity classes and service context

CrmSvcUtil.exe /url:http://<serverName>/<organizationName>/XRMServices/2011/Organization.svc

/out:<outputFilename>.cs /username:<username> /password:<password> /domain:<domainName>

/namespace:<outputNamespace> /serviceContextName:<serviceContextName>

 

Uri organizationUri = new Uri("http://server/orgname/XRMServices/2011/Organization.svc");
 Uri homeRealmUri = null;</pre>
<pre> ClientCredentials credentials = new ClientCredentials();
 credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain");
 OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
 orgProxy.EnableProxyTypes();
 IOrganizationService _service = (IOrganizationService)orgProxy; 
 
 var xrmServiceContext = new XrmServiceContext(_service);
 // get all contact record that has first name as Hugh
 var lstContact = (from c in xrmServiceContext.ContactSet
 where c.FirstName == "Hugh"
 select c).ToList();
 foreach(var contact in lstContact)
 {
 MessageBox.Show(contact.FullName);
 }


Using Early bound entity classes and OrganizationServiceContext

Suppose we have the early bound entity classes but haven’t generated the service context. In this case we can use OrganizationServiceContext

 


var xrmServiceContext = new OrganizationServiceContext(_service);

var lstContact = (from c in xrmServiceContext.CreateQuery<Contact>()
 where c.FirstName == "Hugh"
 select c).ToList();

foreach (var contact in lstContact)
 {
 MessageBox.Show(contact.FullName);
 }

Using Late bound and OrganizationServiceContext

Suppose we are not generating the early bound classes and using late binding

 

 


  var xrmServiceContext = new OrganizationServiceContext(_service);

 var lstContact = (from c in xrmServiceContext.CreateQuery("contact")
 where c["firstname"] == "Hugh"
 select c).ToList();

 foreach (var contact in lstContact)
 {
 MessageBox.Show(contact["fullname"].ToString());
 }

Hope it helps..

 

OptionSetId cannot be changed. EnumAttributeInfo.AttributeDescription.OptionSetId(089e077e-6055-446a-ad6e-326f0bee7c9c) != c0864f8b-2c13-e411-93ed-000d3a800961


We got this error while importing the solution. This issue is caused by having an optionset with same name but different guid.

The solution is to identify the field and delete it from the org where we are importing.

select name from optionsetview where optionsetid = ‘c0864f8b-2c13-e411-93ed-000d3a800961’ (the 2nd guid)

The helpful post

http://atriosystems.wordpress.com/2012/11/12/import-unmanaged-solution-error-optionsetid-cannot-be-changed/

Bye

Fix: Invalid ‘where’ condition. An entity member is invoking an invalid property or method while using LINQ in CRM 2013.


Hi,

We were writing a LINQ query to get the contract record information by passing in the lawyer name. The lawyer is a lookup in the contract record.

So we were using contract.lawyerid.Name field of lookup in our where condition.

However we got the below error while doing so..

 

It seems like the LINQ Implementation for CRM doesn’t correctly interpret the lookup field’s name if we are using it in the where clause.

The solution was to do a join between the entities and then use the where condition against the lawyer entity username field itself.

Hope it helps..

Sample HttpModule for CRM 2013 on premise.


Suppose we have to redirect user belonging to a specific group to some specific page, while they try to access CRM 2013.

For this we can implement an HttpModule

Sample code


public class Class1 : IHttpModule
{
public void Dispose()
{

}
public delegate void MyEventHandler(Object s, EventArgs e);
private MyEventHandler _eventHandler = null;

public event MyEventHandler MyEvent
{
add { _eventHandler += value; }
remove { _eventHandler -= value; }
}
public void Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(ContextOnAuthenticateRequest);
}

private void ContextOnAuthenticateRequest(object sender, EventArgs eventArgs)
{
HttpApplication app = sender as HttpApplication;
HttpContext context = app.Context;

// if the user belongs to adminsitrators role or reporting group
// redirect the user to bing.com
if (context.User.IsInRole(@"BUILTIN\Administrators") &&
context.User.IsInRole(@"LSS\ReportingGroup {0993772e-cfb6-47ba-8b88-2129bfc97f89}"))
{
app.Context.Response.Redirect("http://www.bing.com");
}

if (_eventHandler != null)
_eventHandler(this, null);
}
}

 

Put the dll in the bin of the CRMWeb

..\Microsoft Dynamics CRM\CRMWeb\bin

And register the http module in the web.config

(use sn –T MyModule.dll to get the public key token)

Moreover this can be implemented using URLRewrite as it runs before the authentication so we wont be able to get the username

http://www.iis.net/downloads/microsoft/url-rewrite

Hope it helps

 

 

How to – Use Pre-Filtering in Fetch XML based report in CRM 2013 online.


Hi,

We recently had a requirement to run a report on the form of a custom entity named incident.

It should show incident data and its related entity data i.e. contact and account.

Incident is having n: n relationship with Contact and Account here.

Here we will create three data sets one for showing the incident data and other two for the related entities.

Create a DataSet for Incident to fetch the incident record information (using Advanced Find View).

Make sure to set enableprefiltering as 1

This will create a report parameter in the report

Next we will create a DataSet for Contact entity which is n: n (many to many) related to Incident.

Download the fetch xml and add enableprefiletering attribute

Follow the same step for the DataSet for account entity related to Incident.

While uploading the select Display in value as “Forms for related record types” to run the report in the context of the form.

Report will start appearing in the Incident Form

Sample screenshot

 

Hope it helps

Advertisements

Debugging online plugin in CRM 2013.


Hi,

Let us take a simple plugin to understand the debugging process. We need the profile which we will use for debugging.

Register the plugin (debug version) and corresponding step using plugin registration tool.

Select Install Profiler in Plugin Registration tool and wait till the installation is complete.


Select the Step in Plugin and Click Profile in the tool

Select Ok in the dialog box

Open CRM and perform the step that will run the plugin.

Download the log file.

Open Visual Studio and attach debugger to PluginRegistrationTool.exe

Go back to Plugin Registration tool, select the step and select Debug from the tool bar.

 

Specfiy the log file in the Profile location and plugin assembly that is in debug folder.

Click on Start Execution to start debugging

Hope it helps.