Feb 26, 2015

Issue with datetime formats in OData retrievals

We now use OData Service for client side data retrievals. Yet, there is a bit of complexity when dealing with date time fields.  I usually use REST methods of XrmServiceToolkit library. When I try to retrieve datetime field and populate in some form field, I simply couldn’t do since what I retrieved was something different. Something like “/Date(1420588331000)/”. What is this?

This is a time encoded to a single integer which increases every second. This is UNIX timestamp. Though this is a logical representation of time which can be very helpful when dealing with complex time calculations, in our operation this can be frustrating.

We need to transfer this to date time which can be identified by JavaScript. So below code snippet can be helpful.

//Method

dateFormatUnixStamp: function (_uString) {
    var _date = null;
    if (_uString != null) {
        _date = new Date(parseInt(_uString.slice(_uString.indexOf("(") + 1, _uString.indexOf(")"))));
        _date.setDate(_date.getDate());
    }
    return _date;
}

//Calling

//Suppose _Ent is a entity we retrived through the service
//and we are assigning it to new_startdate of current form

var _startDate = dateFormatUnixStamp(_Ent.new_StartDate);
Xrm.Page.getAttribute(new_startdate).setValue(_startDate);

These two blogs also provides much helpful code samples in this regards;

https://rajeevpentyala.wordpress.com/2011/08/21/read-odata-service-date-field-in-crm-2011/
http://blogs.catapultsystems.com/rhutton/archive/2014/01/30/crm-2013-handling-odata-date-field-value.aspx

Thank you Deepak Mehta for the help.

Feb 12, 2015

Console Application skeleton for Dynamics CRM

Sometimes we need to do some operation in CRM through console applications, though not very common. Still we need to use CRM web services within the console application. CrmConnetion class can be used for that.

This code snippet can be useful in building your application.

Configuration file that keep the connection attributes;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
<appSettings>
  <add key="connectionStr" value="Url=http://SERVERNAME/TENANTNAME; Domain=xxxxx; Username=xxxxx; Password=xxxxx;"/>
</appSettings>
</configuration>

Program code with CrmConnection;

using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System;
using System.Configuration;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ConsoleApplication1
{
 class Program
 {
   static void Main(string[] args)
   {
     CrmConnection _crmConnection = CrmConnection.Parse(ConfigurationManager.AppSettings.Get("connectionStr"));
     OrganizationService _orgService = new OrganizationService(_crmConnection);

     try
     {
        //Logic here
     }
     catch (Exception e)
     {
         Console.WriteLine("Process failed : " + e.Message);
     }
     finally
     {
         Console.WriteLine("Process successful.");
         Console.ReadLine();
     }
   }
 }
}

Feb 3, 2015

SQL to Fetchxml converter

This is a cool tool.

As we all are from on-premise CRM arena, one of the obvious challenges we have now is doing the same complex SSRS reports in online CRMs. In fact, challenge is writing fetchxml queries in the same way we did in t-sql queries. This is a cool free online tool by Kingswaysoft. This converts your t-sql queries to fetch.

 
Note: Please read the help page before using it.