Sunday, July 14, 2013

Check attribute type/formats in CRM 2011 javascript.


Now we can check the attribute formats in CRM java script.

To do this you just need to use the function “getFormat”. This function returns a string value that represents formatting options for the attribute. You can refer the below code to check the format of different types of attributes.

function GetFormatOfAttributes() {
    var attributeFormat;
    try {

        //Get the format of String
        attributeFormat = Xrm.Page.getAttribute("name").getFormat();

        alert("The format of name is " + attributeFormat);





        //Get the format of Email attribute
        attributeFormat = Xrm.Page.getAttribute("emailaddress1").getFormat();

        alert("The format of emailaddress1 is " + attributeFormat);




               

        //Get the format of the Date and Time attribute
        attributeFormat = Xrm.Page.getAttribute("createdon").getFormat();

        alert("The format of createdon is " + attributeFormat);





               

        //Get the format of Duration
        attributeFormat = Xrm.Page.getAttribute("new_duration").getFormat();

        alert("The format of Duration is " + attributeFormat);






        //Get the format of Date Only
        attributeFormat = Xrm.Page.getAttribute("new_date").getFormat();

        alert("The format of new_date is " + attributeFormat);






        //Get the format of Time Zone
        attributeFormat = Xrm.Page.getAttribute("address1_utcoffset").getFormat();

        alert("The format of address1_utcoffset is " + attributeFormat);






       //Get the format of Url
        attributeFormat = Xrm.Page.getAttribute("websiteurl").getFormat();

        alert("The format of websiteurl is " + attributeFormat);





               

        //Get the format of multi line text
        attributeFormat = Xrm.Page.getAttribute("description").getFormat();

        alert("The format of description is " + attributeFormat);





    }
    catch (e) {
        alert("GetFormatOfAttributes Err >> " + e.description);
    }
}




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.







Monday, June 24, 2013

How to set Set ChartColorPalette.

We have ChartColorPalette property of charts to set different color palettes. If you do not want to specify custom colors for palette, at that time you can use following specified palettes.

The ChartColorPalette represents the palettes to be used for the root Chart object and any Series objects. If no colors are assigned to data points and their associated series, the default colors will be assigned to the data

Normally Chart palette is set to None means No palette is used and specified custom colors for chart. As shown below.



To avoid specifying custom colors you can set palette property values as follows. For that you need to change chart XML file and import that chart again.



Note: if you specify both Palette value(other than None) and PaletteCustomColors then Palette will overwrite PaletteCustomColors.

We have following values to set palette colors.

·         Berry - Palette will use blue and purple shades.



 


    Bright - Palette will use bright colors.





·         BrightPastel - Palette will use bright pastel colors shades.







           Chocolate - Palette will use shades of brown.





·         Earth Tones - Palette will use earth tone colors such as green and brown.



 


           
           Excel - Palette will use Excel-style colors.





·         Fire - Palette will use red, orange and yellow colors.
 
 


·         Gray scale -  Palette will use grayscale colors like shades of black and white.






·         Light - Palette will use light colors.






·         Pastel - Palette will use pastel colors.




·         SeaGreen - Palette will use colors that range from green to blue.




·         SemiTransparent - Palette will use semi-transparent colors.