Showing posts with label CRM2011. Show all posts
Showing posts with label CRM2011. Show all posts

Thursday, June 27, 2013

How to convert the DateTime in Local and UTC Date Time format

While working with the plug-ins or with Custom Workflows Assemblies, we get the Datetime from CRM which doesn’t match with user’s Local DateTime format.

As we get the date from the CRM in UTC Format so we can convert it to User’s Local DateTime using LocalTimeFromUtcTimeRequest Request.

To get the Current logged in user’s Local time You need to first retrieve the Time Zone code of that user using RetrieveCurrentUsersSettings request and then you need to convert the UTC date into user’s Local DateTime.

We can also convert the Date from Local Datetime format  to the UTC Datetime Format.

Below examples illustrates how to write LocalTimeFromUtcTimeRequest  and UtcTimeFromLocalTimeRequest request to convert the Date.

Example :

First retrieve the time zone code from UserSettings entity and then excecute request LocalTimeFromUtcTimeRequest to convert the DateTime from UTC Format to Local DateTime format and UtcTimeFromLocalTimeRequest to convert the DateTime from Local to UTC DateTime format as given below.

                        
DateTime convertDate = new DateTime();



           //get the Time Zone Code of user

            int? getTimeZoneCode = RetrieveCurrentUsersSettings(service);



//Convert the UTC Date time into Users Local DateTime Format using request LocalTimeFromUtcTimeRequest

            DateTime localDateTime = RetrieveLocalTimeFromUTCTime(convertDate, getTimeZoneCode, service);

            //Convert the Local Date time into UTC DateTime Format using request UtcTimeFromLocalTimeRequest

            DateTime utcDateTime = RetrieveUTCTimeFromLocalTime(convertDate, getTimeZoneCode, service);



        /// <summary>

        /// Retrieves the current users timezone code

        /// </summary>

        /// <param name="service"> IOrganizationService </param>

        /// <returns></returns>

        private int? RetrieveCurrentUsersSettings(IOrganizationService service)

        {

            var currentUserSettings = service.RetrieveMultiple(

                new QueryExpression("usersettings")

                {

                    ColumnSet = new ColumnSet("timezonecode"),

                    Criteria = new FilterExpression

                    {

                        Conditions =

                        {

                            new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)

                        }

                    }

                }).Entities[0].ToEntity<Entity>();

            //return time zone code

            return (int?)currentUserSettings.Attributes["timezonecode"];

        }



        /// <summary>

        ///  Retrive the local time from the UTC time.

        /// </summary>

        /// <param name="utcTime">UTC Date time which needs to convert to Local DateTime</param>

        /// <param name="timeZoneCode">TimeZoneCode</param>

        /// <param name="service">IOrganizationService service</param>

        /// <returns></returns>

        private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, int? timeZoneCode, IOrganizationService service)

        {

            if (!timeZoneCode.HasValue)

                return DateTime.Now;

            var request = new LocalTimeFromUtcTimeRequest

            {

                TimeZoneCode = timeZoneCode.Value,

                UtcTime = utcTime.ToUniversalTime()

            };

            var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);

            return response.LocalTime;

        }



        /// <summary>

        ///  Retrive the UTC DateTime from Local Date time format.

        /// </summary>

        /// <param name="localTime">Local Date time which needs to convert to UTC</param>

        /// <param name="timeZoneCode">TimeZoneCode</param>

        /// <param name="service">IOrganizationService service</param>

        /// <returns></returns>

        private DateTime RetrieveUTCTimeFromLocalTime(DateTime localTime, int? timeZoneCode, IOrganizationService service)

        {

            if (!timeZoneCode.HasValue)

                return DateTime.Now;



            var request = new UtcTimeFromLocalTimeRequest

            {

                TimeZoneCode = timeZoneCode.Value,

                LocalTime = localTime

            };

            var response = (UtcTimeFromLocalTimeResponse)service.Execute(request);

            return response.UtcTime;

        }









Tuesday, June 25, 2013

How to show List Box Items Horizontally in CRM 2011 using Silverlight

As we know default List box shows list of items in vertical Format. We need to scroll down to view all Items. If you want to show Items horizontally with the horizontal Scrollbar then it can be done with the following way:

1)      You need to first setup <ListBox> in XAML Design. <ListBox> should contain <ItemsPanelTemplate> to where it will change the default template of <ListBox> from vertical to horizontal by adding <StackPanel> with its orientation as "Horizontal".

<ListBox Name="imageList">
<ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                 <StackPanel Name="horizontalOrientation" VerticalAlignment="Top" Orientation="Horizontal"/>
            </ItemsPanelTemplate>
       </ListBox.ItemsPanel>

       <ListBox.ItemTemplate>
              <DataTemplate>                           
                    <Border BorderBrush="Black" BorderThickness="1">
                            <StackPanel  Name="DataStack" Height="Auto" Width="Auto" Orientation="Vertical">                              
                                <StackPanel Orientation="Horizontal" Name="AccountName">
                                <TextBlock Name="Account" Text="Sub Account:" Width="100"/>
                                <Image Name="AccountImage" Source="/HorizontalListImagesView;component/Images/ico_16_1.png" Height="20" Width="20"/>
                                <TextBlock Name="AccountName" Text="{Binding Path=Name, Mode=TwoWay}"/>
                                </StackPanel>
                               
                                <StackPanel Orientation="Horizontal">
                                <TextBlock Name="Contact" Text="Primary Contact:" Width="100"/>
                                <Image Name="ContactImage" Source="/HorizontalListImagesView;component/Images/ico_16_2.png" Height="20" Width="20" />
                                <TextBlock Name="ContactName" Text="{Binding Path=PrimaryContactId.Name, Mode=TwoWay}"/>
                                </StackPanel>

                                <StackPanel Orientation="Horizontal">
                                <TextBlock Name="EmailAddress" Text="Email:"  Width="100"/>
                                <TextBlock Name="Email" Text="{Binding Path=EMailAddress1,Mode=TwoWay}"/>                               
                                </StackPanel>

                                <StackPanel Orientation="Horizontal">
                                <TextBlock Name="PhoneNumber" Text="Main Phone:" Width="100"/>
                                <TextBlock Name="Phone" Text="{Binding Path=Telephone1,Mode=TwoWay}"/>
                                </StackPanel>
                            </StackPanel>
                            </Border>
              </DataTemplate>
       </ListBox.ItemTemplate>
</ListBox>



2)      Now just design your List in Data Template and bind item Source to it. It will show following output.







Wednesday, October 3, 2012

How to Dynamics CRM

New to Dynamics CRM or already using Dynamics CRM? But do you do CRM the way it should be? Learn to CRM from the experts. 



Roughly around 20 CRM MVP's (I was not one of them) have got together and come out with a collaborative offering called CRMFieldGuide.

To say the least.. Its simple and powerful. CRM is simple and the book will let your CRM be effective by letting you keep it simple.

The book touches upon all offerings/tools related to Dynamics CRM. It is not restricted to explaining the functional concepts of Sales/Marketing and Service. It goes way beyond and includes the entire process right from Installing and Configuring CRM to the Best Practices for customizations and Server Maintenance. It has a total of 25 chapters to touch upon the various topics some of which we generally need to search on the web to find information on like

  • Sharepoint Integration
  • Dynamics Connector
  • Best practices for upgrade from CRM 4 to 2011
  • Understand Solutions to make it work better for you
  • Data Import

Who better than the experts to share with you their collective knowledge in one CRMFieldGuide.

Its a must have for anyone who needs to CRM the right way!

This book was released on October 1st 2012. Grab a copy right now from www.crmfieldguide.com

Tuesday, February 28, 2012

Issue with plug-in in CRM 2011

We had a requirement to do some calculation and update self record of custom entity. We have created a plug-in and registered it on post operation of update event. The code was reading the post image, updating some fields and updating the same.

The plug-in was working fine for single record. But when two records are saved simultaneously, the plug-in was giving below errors randomly.


1. "ValidateOpen - Encountered disposed CrmDbConnection when it should not be disposed"
2. "You cannot create a SqlExecutionContext from another SqlExecutionContext on which OnBeginRequest has not been called"
3. " System.InvalidOperationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #58919D7E"
4. "System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #1638FFA8"

When we checked in the Event viewer then we got that whatever error we get from CRM the Event viewer shows "System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first." error.

After searching on net we found that CRM does not re-initialize the plug-in object again, it uses the same object. In the plug-in, ignore to use class level variables. We had created service object at class level so it was using the same service object for retrieving and updating.

Also we were reading post image and updating the post image as it is. This was also creating a problem. Please create a new object of Entity and then set only those fields which you want to update. Then update the record.

Sometimes this plug-in takes too much time as it was doing so many calculation. We were getting the "Query execution time of 30.1 seconds exceeded the threshold of 10 seconds. Thread: 4; Database: inogic_MSCRM " error when plug-in takes time. To resolve this, please add below keys in the registry on CRM server.
1) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM\OLEDBTimeout
a. In seconds
b. The OLEDBTimeout value controls the SQL time-out value that is used for a single SQL query
c. Set it to 60 seconds


2) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM\ExtendedTimeout
a. In milliseconds
b. The ExtendedTimeout value controls the ASP.NET time-out value
c. Set it to 1,000,000


3) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM\NormalTimeout
a. In milliseconds
b. Specifies the SOAP call timeout for most operations
c. Set it to 300,000

Hope this helps!!

Wednesday, September 28, 2011

Debugging Silverlight Web Resources in CRM2011

In CRM2011, Silverlight Web resources can be debugged in Microsoft Visual Studio. For this you need to follow below steps:

• Build your Silverlight application.

• Upload the built version of the .xap file from the Web application project ClientBin folder.

• View your Silverlight application in the context it is designed to be used in.

• In your Silverlight application project, from the Visual Studio menu, select Debug and then Attach To Process….

• In the Attach to Process dialog box, find an iexplore.exe process where the Type column value is Silverlight, x86. (See below screen shot)



• Select that process and press Attach to close the dialog box and start debugging.

• In your Silverlight application project, set a breakpoint.

• Refresh the browser window or, in the Silverlight application, perform the action that you need to test your code.

Hope this help

Monday, August 8, 2011

Notify the dialog to end

When you design a Dialog, make sure you explicitly stop the dialog when all the steps of the dialog have completed using the “Stop Dialog” step



Failure to do so might result in you receiving the following error when all steps have been processed.



The above error is pretty misleading and we spent hours trying to identify which data is missing before we finally managed to get this to work after adding the “Stop Dialog” step.