public static void init()
        {
            ContactsEditorViewModel vm = new ContactsEditorViewModel();

            OrganizationServiceProxy.GetUserSettings();
            // Data Bind Grid
            List<Column> columns = GridDataViewBinder.ParseLayout(",entityState,20,First Name,firstname,200,Last Name,lastname,200,Birth Date,birthdate,200,Account Role Code,accountrolecode,200,Number of Children,numberofchildren,100,Currency,transactioncurrencyid,200,Credit Limit,creditlimit,100,Gender,gendercode,100,Owner,ownerid,100,Parent Customer,parentcustomerid,100");

            // Set Column formatters and editors
            columns[0].Formatter = delegate(int row, int cell, object value, Column columnDef, object dataContext)
            {
                EntityStates state = (EntityStates)value;
                return ((state == EntityStates.Changed) || (state == EntityStates.Created)) ? "<span class='grid-edit-indicator'></span>" : "";
            };

            // First Name Column
            XrmTextEditor.BindColumn(columns[1]);

            // Last Name Column
            XrmTextEditor.BindColumn(columns[2]);

            // Birth Date Column
            XrmDateBindingOptions dateOptions = new XrmDateBindingOptions();
            dateOptions.Hour = 9;
            dateOptions.Minute = 0;
            XrmDateEditor.BindColumn(columns[3], false).Options = dateOptions;

            // Account Code Column
            XrmOptionSetEditor.BindColumn(columns[4], "contact", columns[4].Field, true);

            // Number of Children Column
            XrmNumberEditor.BindColumn(columns[5], 0, 100, 0);

            // Currency Column
            XrmLookupEditor.BindColumn(columns[6], vm.TransactionCurrencySearchCommand, "transactioncurrencyid", "currencyname", "");

            // Credit Limit Column
            XrmMoneyEditor.BindColumn(columns[7], -10000, 10000);

            // Another optionset
            XrmOptionSetEditor.BindColumn(columns[8], "contact", columns[8].Field, true);

            // Owner Column
            XrmLookupEditorOptions options = (XrmLookupEditorOptions)XrmLookupEditor.BindColumn(columns[9], vm.OwnerSearchCommand, "id", "name", "").Options;
            options.showFooter = true;

            // Account Column
            XrmLookupEditorOptions accountLookupOptions = (XrmLookupEditorOptions)XrmLookupEditor.BindColumn(columns[10], vm.AccountSearchCommand, "id", "name", "").Options;
            accountLookupOptions.showFooter = true;
            accountLookupOptions.footerButton = new XrmLookupEditorButton();
            accountLookupOptions.footerButton.Label = "Add New";
            accountLookupOptions.footerButton.Image = "/_imgs/add_10.png";
            accountLookupOptions.footerButton.OnClick = vm.AddNewAccountInLine;

            // Create Grid
            GridDataViewBinder contactGridDataBinder = new GridDataViewBinder();
            Grid contactsGrid = contactGridDataBinder.DataBindXrmGrid(vm.Contacts, columns, "container", "pager",true,false);

            // Data Bind
            ViewBase.RegisterViewModel(vm);

            Window.SetTimeout(delegate()
            {
                vm.Init();
            }, 0);
        }
 public static XrmDateBindingOptions GetDateBindingOptions(Column columnDef)
 {
     object options = columnDef.Options;
     XrmDateBindingOptions dateOptions = null;
     if (options != null && options.GetType() == typeof(string))
     {
         dateOptions = new XrmDateBindingOptions();
         dateOptions.OverrideUserDateFormat = (string)columnDef.Options;
         return dateOptions;
     }
     else if (options!=null)
     {
         dateOptions = (XrmDateBindingOptions)options;
     }
     else
     {
         dateOptions = new XrmDateBindingOptions();
     }
     return dateOptions;
 }
        public override void Init(System.Html.Element element, Func <object> valueAccessor, Func <System.Collections.Dictionary> allBindingsAccessor, object viewModel, object context)
        {
            XrmDateBindingOptions dateOptions = (XrmDateBindingOptions)((object)allBindingsAccessor()["dateOptions"]);
            jQueryObject          container   = jQuery.FromElement(element);
            jQueryObject          dateTime    = container.Find(".sparkle-input-datepicker-part");
            jQueryObject          dateButton  = container.Find(".sparkle-input-datepicker-button-part");
            // Add Date Picker
            DatePickerOptions2 options = new DatePickerOptions2();

            options.ShowOn          = "";
            options.ButtonImageOnly = true;
            options.FirstDay        = OrganizationServiceProxy.OrganizationSettings != null ? OrganizationServiceProxy.OrganizationSettings.WeekStartDayCode.Value.Value : 0;

            string dateFormat = "dd/MM/yy";

            if (OrganizationServiceProxy.UserSettings != null)
            {
                dateFormat = OrganizationServiceProxy.UserSettings.DateFormatString;
            }
            options.DateFormat = dateFormat;
            dateTime.Plugin <DatePickerPlugIn>().DatePicker(options);
            dateButton.Click(delegate(jQueryEvent e)
            {
                // Note: This is using a custom plugin definition since the standard didn't include show
                dateTime.Plugin <DatePickerPlugIn>().DatePicker(DatePickerMethod2.Show);
            });

            //handle the field changing
            KnockoutUtils.RegisterEventHandler(dateTime.GetElement(0), "change", delegate(object sender, EventArgs e)
            {
                Observable <DateTime> observable = (Observable <DateTime>)valueAccessor();
                bool isValid = true;

                if (((string)Script.Literal("typeof({0}.IsValid)", observable)) != "undefined")
                {
                    isValid = ((IValidatedObservable)observable).IsValid() == true;
                }

                if (isValid)
                {
                    DateTime selectedDate = (DateTime)dateTime.Plugin <DatePickerObject>().DatePicker(DatePickerMethod.GetDate);
                    // Get Current observable value - we only want to set the date part
                    DateTime currentValue = observable.GetValue();
                    // If the existing value is null, set the default time
                    if (currentValue == null && dateOptions != null)
                    {
                        currentValue = new DateTime(1900, 1, 1, dateOptions.Hour != null ? dateOptions.Hour.Value : 0, dateOptions.Minute != null ? dateOptions.Minute.Value : 0);
                    }

                    DateTimeEx.SetTime(selectedDate, currentValue);
                    observable.SetValue(selectedDate);
                }
                dateTime.Blur();
            });

            Action disposeCallBack = delegate()
            {
                Script.Literal("$({0}).datepicker(\"destroy\")", element);
            };

            //handle disposal (if KO removes by the template binding)
            Script.Literal("ko.utils.domNodeDisposal.addDisposeCallback({0}, {1})", element, (object)disposeCallBack);
        }