Thursday, 30 June 2011

How to save an entity record in CRM 2011 using javascript

In CRM 2011, you could save an entity record using javascript.

There are 3 types of save possible using javascript in CRM 2011.

1. Equivalent to 'SAVE' button found on the entity form

Xrm.Page.data.entity.save();
2. Equivalent to 'SAVE AND NEW' button found on the entity form

Xrm.Page.data.entity.save('saveandnew');

3. Equivalent to 'SAVE AND CLOSE'  button found on the entity form

Xrm.Page.data.entity.save('saveandclose');

Saturday, 25 June 2011

How to clear Lookup in CRM 2011/ Lookup filtering and clearing lookup in CRM 2011 / How to pass argument to js function in CRM 2011

As you all know, one of the best features of CRM 2011 is lookup filtering just because it reduces coding. Consider that we are filtering second lookup based on first one. We need to clear the second lookup whenever the user change the selection of first lookup.Lookup feature Reference: http://crmdm.blogspot.com/2011/04/lookup-fitering-feature-in-crm-2011-or.html

Scenario:
2 lookups Country and City

When the user selects UK from first lookup then second lookup lists cities belongs to UK( based on lookup filtering feature)
Now the user would like to update Country to Denmark. so when the user selects country as Denmark then the city lookup should be cleared. And then the user gets a chance to choose cities belongs to Denmark.

This could be achieved by simple javascript by passing an attribute.


Code Snippet:

function SetLookupNull(lookupAttribute)
{
var lookupObject = Xrm.Page.getAttribute(lookupAttribute);



if (lookupObject != null)
 {

Xrm.Page.getAttribute(lookupAttribute).setValue(

null);

}

}

The function call is on the onchange event of first lookup


Please note that we need to pass the attribute name of the lookup which should be cleared.


The key advantage of the above function is reusability. You could clear any lookup value as per the need by supplying the attribute name as shown.

Tuesday, 21 June 2011

Difference between Secure / Unsecure Configuration of Plugin Registration tool in CRM 2011

As you all know the plugin registration tool in CRM 2011 contains Secure and Unsecure configuration sections as shown below.

Following are the two key differences found between these two configuration settings.



Unsecure Configuration of
Plugin Registration tool in CRM 2011
Secure Configuration of Plugin
Registration tool in CRM 2011


Unsecure configuration information could be read by any user in CRM. Remember its public information (Eg: Parameter strings to be used in plugin could be supplied here)
The Secure Configuration information could be read only by CRM Administrators.(Eg: Restricted data from normal user could be supplied here)
Imagine that you include a plugin, plugin steps and activate them in a solution. Later solution was exported as Managed Solution to another environment. In this scenario, the supplied Unsecure configuration values would be available in the new environment.

Imagine that you include a plugin, plugin steps and activate them in a solution. Later solution was exported as Managed Solution to another environment. In this scenario, the supplied Secure configuration  information would NOT be available in the new environment. The simple  reason behind this is to provide more security to the contents of Secure Configuration.


Pitfall:

Consider a scenario that you have developed a plugin and certain parameter strings are designed to supply to the plugin in such a way that it is required to run the plugin smoothly.If you supply these parameter strings under secure configuration then the plugin will work fine only for the CRM Administrators. The simple reason is secure configuration can only be read by a CRM Administrator. So if the user is not a CRM administrator then the plugin would try to read  but it would fail just because its under the secure configuration.All public information should be supplied via Unsecure configuration section. So please remember these tips when you supply the secure and unsecure configuration via Plugin Registration tool.

Monday, 20 June 2011

"Plug-in assembly does not contain the required types or assembly content cannot be updated"-CRM 2011

 Plugin Registration tool used in CRM 2011 may throw some generic error message "Plug-in assembly does not contain the required types or assembly content cannot be updated"
Lets explore how it could happen.



Error Message:


Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Plug-in assembly does not contain the required types or assembly content cannot be updated.
Detail: <OrganizationServiceFault xmlns="
http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ErrorCode>-2147204725</ErrorCode>
  <ErrorDetails xmlns:a="
http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Plug-in assembly does not contain the required types or assembly content cannot be updated.</Message>
  <Timestamp>2011-06-20T08:31:56.6192334Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147204725</ErrorCode>
    <ErrorDetails xmlns:a="
http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>Plug-in assembly does not contain the required types or assembly content cannot be updated.</Message>
    <Timestamp>2011-06-20T08:31:56.6192334Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText i:nil="true" />
</OrganizationServiceFault>
Server stack trace:
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Xrm.Sdk.IOrganizationService.Update(Entity entity)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.UpdateCore(Entity entity)
   at PluginRegistrationTool.RegistrationHelper.UpdateAssembly(CrmOrganization org, String pathToAssembly, CrmPluginAssembly assembly) in C:\Users\Administrator\Desktop\CRMTools\CrmSDK2011\tools\pluginregistration\RegistrationHelper.cs:line 427
   at PluginRegistrationTool.PluginRegistrationForm.btnRegister_Click(Object sender, EventArgs e) in C:\Users\Administrator\Desktop\CRMTools\CrmSDK2011\tools\pluginregistration\PluginRegistrationForm.cs:line 371

When you are trying to update the plugin using Plugin registration tool in CRM 2011, then you might get this error if there are some mismatches between old assembly and new assembly.

Consider a scenario:

2 Developers John & Harry are working in the same project. They are retaining their source code in a version control. They have maintained one common dll for the plugin assembly using IL Merge to avoid dependency issues. ( How to use IL Merge Refer: http://crmdm.blogspot.com/2011/02/crm-cannot-find-plugin-assembly-or-one.html ) . Imagine there are a total number of 10 plugins already in the project. Now John develops a plugin and he updates the plugin assembly with his plugin. But he forgets to check in the source code of new plugin in the version control. Next day Harry would like to develop a plugin. He checks out latest source code(but John's plugin missing) from the version control and starts developing his plugin. When Harry tries to update the plugin assembly he would see the above error message. Harry must get the right previous version of plugin dll ( which includes John's plugin) because the last version in the assembly contains John's plugin. In simple words, when Harry tries to update the plugin assembly in CRM 2011, his dll should have required types or assembly content. This is a sample case of mismatch.

So if you get this error message just think of the possibilities of mismatch and then you could easily resolve it.


 

Plugin Registration Tool Error in CRM 2011 OR "Can not Deploy / Update / Register Plugin in CRM 2011"

 Plugin Registration tool used in CRM 2011 may throw some generic error messages while registering or updating the plugin assembly. Following scenario is one of them.




As this error message is more generic, its a bit difficult to identify the exact secario.


Error Message:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Plug-in assembly fullnames must be unique (ignoring the version build and revision number).
Detail: <OrganizationServiceFault xmlns="
http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ErrorCode>-2147204741</ErrorCode>
  <ErrorDetails xmlns:a="
http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Plug-in assembly fullnames must be unique (ignoring the version build and revision number).</Message>
  <Timestamp>2011-06-20T10:04:09.615489Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147204741</ErrorCode>
    <ErrorDetails xmlns:a="
http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>Plug-in assembly fullnames must be unique (ignoring the version build and revision number).</Message>
    <Timestamp>2011-06-20T10:04:09.615489Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText i:nil="true" />
</OrganizationServiceFault>
Server stack trace:
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Xrm.Sdk.IOrganizationService.Update(Entity entity)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.UpdateCore(Entity entity)
   at PluginRegistrationTool.RegistrationHelper.UpdateAssembly(CrmOrganization org, String pathToAssembly, CrmPluginAssembly assembly) in C:\Users\Administrator\Desktop\CRMTools\CrmSDK2011\tools\pluginregistration\RegistrationHelper.cs:line 427
   at PluginRegistrationTool.PluginRegistrationForm.btnRegister_Click(Object sender, EventArgs e) in C:\Users\Administrator\Desktop\CRMTools\CrmSDK2011\tools\pluginregistration\PluginRegistrationForm.cs:line 371


So one possible scenario could be the user may not have enough privilege to deploy the plugin. The user who is trying to register the plugin must exist in the Deployment Administrator group of Deployment Manager. Otherwise the user would get the above error while registering the plugin or when trying to update the existing plugin. This is a security restriction in CRM 2011. System Administrator user Account can always deploy or update the plugin. By default System Administrator account would be included in the Deployment Administrator group.

How to check deployment Administrator privilege:

Its very easy to check the deployment Administrator privilege. Navigate to Deployment Manager to see the list of Deployment Administrators. You could include the user in the Deployment Administrator group by clicking on 'New Deployment Administrator' option.Please refer the screen shot below.




Saturday, 11 June 2011

How to retrieve a lookup attribute from Plugin in CRM 2011

Related Post : http://crmdm.blogspot.com/2011/03/how-to-retrieve-lookup-text-value-id.html


In CRM 2011 we could use EntityReference to retrieve the lookup attribute from a Plugin.



Consider that we have a Contact lookup named as 'new_contact'. In the plugin we could use the following code to retrieve the lookup.

Code snippets:

CRM 2011:
EntityReference
contactlookup = (EntityReference)targetEntity.Attributes["new_contact"];



CRM 4:

Lookup contactlookup = (Lookup)targetEntity["new_contact"];