Thursday, February 23, 2012

Get the Context instance of ms crm workflow and plugin

To get the context of ms crm is the first phase every MS CRM developer come across when going to develop a custom workflow or plugin. The context instance of workflow or plugin gives us the details of the entity on which entity record this workflow is running which helps us to fetch more details to lead the custom logic.

//Workflow
[CrmWorkflowActivity("My Test Workflow")]
public partial class MyTestWorkflow : SequenceActivity
{
        #region Designer generated code
        ---
        ---
        #endregion
      protected override ActivityExecutionStatus Execute(ActivityExecutionContext context)
      {
         IContextService contextService = (IContextService)context.GetService(typeof(IContextService));                                  
         IWorkflowContext workflowContext = contextService.Context;
         Guid entityID = (Guid)context.OutputParameters.Properties["id"];
      }
}

//Plugin
public class Plugin:IPlugin
{
    public void Execute(IPluginExecutionContext context)
    {
        DynamicEntity entityInstance;
        if (context.InputParameters.Properties.Contains("Target") && 
               context.InputParameters.Properties["Target"] is DynamicEntity)
        {
             entityInstance= (DynamicEntity)context.InputParameters.Properties["Target"];             
        }                        
    }     
}

MS CRM Retrieve and RetrieveMultiple methods do not work on Custom or Dynamic Entities

RetrieveMultiple do not work to retrieve data from custom entities and it ends with the runtime exception if we try to get the records using this method.
Exception: Error in XML document. The specified type was not recognized.

This is because RetrieveMultiple works on strongly typed entities only. When we make use of SDK's we return custom entities as dynamic entities which are not strongly typed. In order to retrieve the Dynamic Entity, we should use Execute method as shown in the below code.

RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.Query = queryExpression;
req.ReturnDynamicEntities = true;
RetrieveMultipleResponse resp = (RetrieveMultipleResponse)_service.Execute(req);
if (resp != null && resp.BusinessEntityCollection.BusinessEntities.Count > 0)
{
  Guid columnID=Guid.Empty;
  dynEntity = (DynamicEntity)resp.BusinessEntityCollection.BusinessEntities[0];
  if ( dynEntity.Properties.Contains("new_columnid"))
     columnID = ((Lookup)dynEntity.Properties[" new_columnid"]).Value;
}

If we use crm webservices instead of sdk, we can make use of RetrieveMultiple without any issues.

Dynamically update or assign the owner of an entity in MS CRM 4.0

In general we use the Update method to update the fields in MSCRM. But, we cannot update the owner of an entity using update method. To update the owner, we have to make use of AssignRequest.

AssignRequest req = new AssignRequest();

SecurityPrincipal assignee = new SecurityPrincipal();
assignee.Type = SecurityPrincipalType.User; //User or Queue
assignee.PrincipalId = ownerID; //crm userid to whom we assign 

// Request changes depending on the entity. For account, it is TargetOwnedAccount
TargetOwnedLead target = new TargetOwnedLead(); 
target.EntityId = leadID; //Id of the lead record whose owner has to be updated

req.Assignee = assignee;
req.Target = target;
_service.Execute(req); //_service is the crm service

Wednesday, February 22, 2012

Add new column in search criteria of MS CRM 4.0

Search in MS CRM is a handy option to easily search records of a particular entity. But, sometimes we cannot find the records if we search based on certain fields. For example, you would like to search the list of Opportunities whose Potential Customer is XYZ and entered it in the search text box in Opportunities view and ended with no  results even there are Opportunities for that Potential Customer. This is due to the fields that are being used by the search criteria to search Opps. You need to add the Potential Customer field in the Find columns of Search criteria.
In order to add new search column in search criteria, follow the below steps:
  1. Open Entity from Customizations
  2. Under Forms and Views, open Quick Find View
  3. Add Find Columns
  4. Check the fields that you would like to include in the search criteria, in this case check the Potential Customer field
  5. Save and Close the view 
  6. Publish the entity


MS CRM - Update manager for crm user

Once we are done with creating an MS CRM User, then the Manager field becomes read only. I n order to update the Manager field, Open the user and go to Actions menu and click on Change Manager...

Tuesday, February 21, 2012

Details on MSCRM Entities ObjectCodes and Relationships in CRM Metadata Browser

A simple post but useful at times if you are looking to find the relationship between 2 Entities or the object type code of a particular entity. MS CRM comes with a metadata browser which gives brief details on the metadata of all entities, their object type codes and their relationships with other entities.

CRM Metadata browser url: http://crmservername:port/OrganizationName/sdk/list.aspx



How to update MS CRM User logon name


There might be times where we happen to update the domain logon name of a crm user when there is a change in the name in the Active Directory. If we try to update the logon name in MSCRM, we end up with the below error:

"You are attempting to create a user with a domain logon that is already used by another user. Select another domain logon and try again"



To overcome this and to change the logon name successfully, create a dummy user in the active directory and make sure that doesn't exist in crm users. Now, open the user whose logon name should be changed. Point the logon name to the dummy account and save it. Now, re open the user and point to the updated account and save it.

Easy way to build FetchXml query using Advanced Find in MS CRM 4.0

CRM 2011 comes with advanced features and one important feature that saves time is building FetchXml query. It comes with the out of box feature which includes FetchXml button in the Advanced Find window. Whereas in CRM 4.0 we can get the fetch xml using the below technique:


Open Advanced Find and Write the query and click on Find to get the result
In the Result window, click F11 and enter javascript:alert(resultRender.FetchXml.value); which opens an alert box with the fetch xml query. Click on the alert box and press Ctrl+c to copy the xml and make  use of it for easy development.

Outlook client configuration issue in MS CRM 4.0 Unauthorized Error

I really feel happy to share this and save some time for someone else as I had some really bad experience while configuring MSCRM 4.0 Outlook client. My CRM Server is on a different domain and while configuring the outlook on my machine, it asks for the crm server url and after providing it, I simply came across the below errors:

"The configuration wizard cannot connect to the Microsoft CRM server. This might indicate that the server is down".
or
"The request failed with http status 401: UnAuthorized" 

The error directly says this is an authentication issue and even I know that this issue is related to domain as it requires crm user credentials to connect to crm server but it is not insisting for credentials. So, how to supply the credentials? 

After a few hours of debugging, I could able to supply the credentials with the below settings:
1. Make sure to login as administrator on the client machine where we install outlook client
2. Add the crm sever url in trusted sites zone in internet explorer from tools menu - Internet Options - Security - Trusted sites zone
3. open user accounts from control panel on the client machine where you install the outlook client
4. Click on the Advanced tab
5. Click Manage Passwords button
6. Click on Manage your credentials if it is Windows 7
6. Under Windows Credentials section, add the login credentials  required to login to CRM. 
7. Provide the fullname for crm server as servername.domain.com in Internet or network address field




Giving alias name or different name to Linked Server in MS SQL

Today I would like to put sometime on giving alias named to Linked Server. I got some request to update the linked server name from Server_ABC to Server_XYZ. It sounds pretty simple to update the name but I came to know the real depth in it when I tried to find the dependencies. There are many dependencies for this linked server and most of the cases I found it in Store Procedures. So, I have to update the linked server name at all the places to reflect the change. In Order to get rid of this, we can make use of alias concept where we keep the linked server name to be more generic and pointing the data source points to original server name.

Follow these settings to create an alias name for linked server:

Right click on the target linked server and Properties
General Tab -
Linked Server: generic name
Server Type: choose other data source
Provider: SQL Native Client
Product: sql_server
DataSource: original server name

Security Tab - choose the appropriate security context
If you are connecting to a remote server,  click Be made using this security context and enter sql login credentials to connect to the linked server.

Server Options -
Collation Compatible: false
Data Access - True
Rpc - True
Rpc Out - True
Use Remote Collation - True
Connection Timeout & Query Timeout are by default set to 0 which is unlimited. Any value specified greater than 0 is treated in seconds.

This alias name helps us more when we work on the linked servers in both dev and production environment. We can make use of this alias concept to maintain same linked server name on both the environments.


Hope this helps!

Entity field is read only in bulk edit in mscrm 4.0

We all are very much handy using the bulk edit feature in MSCRM 4.0 to bulk update data. We might come across some scenario where we tried to update a field during bulk edit and found that field is set as read only even though the field is not read only field. 
The reason for this is there is an OnChange script published for that field letting not the users to bulk edit that field. For time being, you can uncheck the load script button on the onchange event and publish the entity and you can now able to edit the field.