/// <summary> /// This method called when page is loaded to restore state /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) { if (e.PageState != null && e.PageState.ContainsKey("id")) { id = Guid.Parse(e.PageState["id"].ToString()); } if (e.PageState != null && e.PageState.ContainsKey("entityMetadataEx")) { entityMetadataEx = e.PageState["entityMetadataEx"] as EntityMetadataEx; } if (e.PageState != null && e.PageState.ContainsKey("pvRecord_SelectedIndex")) { pvRecord_SelectedIndex = int.Parse(e.PageState["pvRecord_SelectedIndex"].ToString()); pvRecord.SelectedIndex = pvRecord_SelectedIndex; } if (e.PageState != null && e.PageState.ContainsKey("cbLookFor_SelectedIndex")) { cbLookFor_SelectedIndex = int.Parse(e.PageState["cbLookFor_SelectedIndex"].ToString()); } if (e.PageState != null && e.PageState.ContainsKey("cbLookIn_SelectedIndex")) { cbLookIn_SelectedIndex = int.Parse(e.PageState["cbLookIn_SelectedIndex"].ToString()); } // Restore fields and record. If you want to always retrieve latest data, then // do not restore from cache if (e.PageState != null && e.PageState.ContainsKey("fields")) { fields = e.PageState["fields"] as List <FormFieldData>; } if (e.PageState != null && e.PageState.ContainsKey("record")) { record = e.PageState["record"] as Entity; } }
/// <summary> /// This method is called when usre click a record in Related Grid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void lvList_ItemClick(object sender, ItemClickEventArgs e) { // Obtain clicked ViewData var item = e.ClickedItem as ViewData; // Retrieve EntityName string entityName = (string.IsNullOrEmpty(item.ActivityTypeName)) ? (CRMGrid.cbLookFor.SelectedItem as EntityMetadataEx).EntityMetadata.LogicalName : item.ActivityTypeName; // Then retrieve Metadata for it. EntityMetadataEx entityMetadataEx = CRMHelper.EntityMetadataExCollection. Where(x => x.EntityMetadata.LogicalName == entityName).FirstOrDefault(); if (entityMetadataEx == null) { MessageDialog dialog = new MessageDialog(loader.GetString("NotValidForMobile"), entityName); await dialog.ShowAsync(); return; } // Put temporary data for RecordDetailPage CRMHelper.temporaryData = new object[] { item.Id, entityMetadataEx }; // Then nagivate to RecordDetailPage Frame.Navigate(typeof(RecordDetailPage)); }
/// <summary> /// This method called when user navigated to this page. /// </summary> /// <param name="e"></param> protected async override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); progressRing.IsActive = true; // If UserInfo is null, then initialize the helper if (CRMHelper.UserInfo == null) { await CRMHelper.Initialize(); } // Check if there is any temporaryData object[] parameters = CRMHelper.temporaryData as object[]; if (parameters.Count() != 3) { throw new Exception("SetRegardingPage takes 3 parameters, SourceRecord, RelatedData and EntityMetadataEx"); } this.record = parameters[0] as Entity; this.relatedData = parameters[1] as RelatedData; this.entityMetadataEx = parameters[2] as EntityMetadataEx; // Then initialize page Initialize(); progressRing.IsActive = false; }
/// <summary> /// This method called when user clicks sitemap item. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ListView_ItemClick(object sender, ItemClickEventArgs e) { // Obtain clicked item EntityMetadataEx entityMetadataEx = e.ClickedItem as EntityMetadataEx; // Then nagivate to EntityGrid page. Frame.Navigate(typeof(EntityGridPage), (int)entityMetadataEx.EntityMetadata.ObjectTypeCode); }
/// <summary> /// This method is called when user clicks Add button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void abAdd_Click(object sender, RoutedEventArgs e) { EntityMetadataEx entityMetadataEx = CRMGrid.cbLookFor.SelectedItem as EntityMetadataEx; // Retrieve fields for new records. (existing one already have data in it) List <FormFieldData> fields = await CRMHelper.RetrieveFormFields(entityMetadataEx); // Generate parameters. As this is new record, passing null for record object. object[] parameters = new object[] { null, fields, entityMetadataEx }; CRMHelper.temporaryData = parameters; Frame.Navigate(typeof(RecordModifyPage)); }
/// <summary> /// This method called when page is loaded to restore state /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) { if (e.PageState != null && e.PageState.ContainsKey("record")) { this.record = e.PageState["record"] as Entity; } if (e.PageState != null && e.PageState.ContainsKey("fields")) { this.fields = e.PageState["fields"] as List <FormFieldData>; } if (e.PageState != null && e.PageState.ContainsKey("fields")) { this.entityMetadataEx = e.PageState["entityMetadataEx"] as EntityMetadataEx; } }
/// <summary> /// This called when ActivityParty (which may have more than 1 item in a field) is touched. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void ActivityParty_Tapped(object sender, TappedRoutedEventArgs e) { // Cast item to ActivityParty ActivityParty ap = (e.OriginalSource as TextBlock).DataContext as ActivityParty; // Find EntityMetadata (usually its SystemUser) EntityMetadataEx entityMetadataEx = CRMHelper.EntityMetadataExCollection.Where(x => x.EntityMetadata.LogicalName == ap.PartyId.LogicalName).FirstOrDefault(); if (entityMetadataEx == null) { return; } // Pass data by using TemporaryData CRMHelper.temporaryData = new object[] { ap.PartyId.Id, entityMetadataEx }; Frame.Navigate(typeof(RecordDetailPage)); }
/// <summary> /// This method called when user navigated to this page. /// </summary> /// <param name="e"></param> protected async override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); progressRing.IsActive = true; // If UserInfo is null, then initialize the helper if (CRMHelper.UserInfo == null) { await CRMHelper.Initialize(); } // Retrieve EntityMetadataEx by using passed parameter (ObjectTypeCode) this.entityMetadataEx = CRMHelper.EntityMetadataExCollection.Where(x => x.EntityMetadata.ObjectTypeCode == (int)e.Parameter).First(); Initialize(); progressRing.IsActive = false; }
/// <summary> /// Invoked when LookFor combobox selection item changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void cbLookFor_SelectionChanged(object sender, SelectionChangedEventArgs e) { progressRing.IsActive = true; // Clear search criteria txtSearch.Text = ""; // Clear data lvList.ItemsSource = null; // Show Search Button and Hide Cancel Button btnSearch.Visibility = Visibility.Visible; btnCancelSearch.Visibility = Visibility.Collapsed; // Set the cbLookFor and searchbox disabled until views/records loaded. cbLookFor.IsEnabled = false; cbLookIn.IsEnabled = false; txtSearch.IsEnabled = false; // Check if selected Entity is available for mobile, otherwise cannot retrieve data. EntityMetadataEx entityMetadata = CRMHelper.EntityMetadataExCollection.Where(x => x.EntityMetadata.LogicalName == ((sender as ComboBox).SelectedItem as EntityMetadataEx).EntityMetadata.LogicalName).FirstOrDefault(); if (entityMetadata == null) { MessageDialog dialog = new MessageDialog( String.Format(loader.GetString("NotValidForMobile"), ((sender as ComboBox).SelectedItem as EntityMetadata).DisplayName)); await dialog.ShowAsync(); // Reset the selection back to original (sender as ComboBox).SelectedItem = e.RemovedItems[0]; return; } // Retrieve available view definitions depending on conditions. cbLookIn.ItemsSource = await CRMHelper.RetrieveViews( (int)entityMetadata.EntityMetadata.ObjectTypeCode, isRegarding, isLookup, txtSearch.Text); // Assign display name cbLookIn.DisplayMemberPath = "Name"; cbLookIn.SelectedIndex = cbLookIn_SelectedIndex; }
/// <summary> /// This method is called when user clicks a record in a Grid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lvList_ItemClick(object sender, ItemClickEventArgs e) { // Obtain ViewData. var item = e.ClickedItem as ViewData; EntityMetadataEx entityMetadataEx; // Obtain entityMetadata if (!string.IsNullOrEmpty(item.ActivityTypeName)) //Activities { entityMetadataEx = CRMHelper.EntityMetadataExCollection. Where(x => x.EntityMetadata.LogicalName == item.ActivityTypeName).FirstOrDefault(); } else { entityMetadataEx = CRMGrid.cbLookFor.SelectedItem as EntityMetadataEx; } // Put temporary data for RecordDetailPage and Navigate to RecordDetailPage. CRMHelper.temporaryData = new object[] { item.Id, entityMetadataEx }; Frame.Navigate(typeof(RecordDetailPage)); }
/// <summary> /// This method called when user navigated to this page. /// </summary> /// <param name="e"></param> protected async override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); progressRing.IsActive = true; // If UserInfo is null, then initialize the helper if (CRMHelper.UserInfo == null) { await CRMHelper.Initialize(); } if (fields == null) // If fields are not yet has value, means just navigated to this page { // Check if there is any temporaryData object[] parameters = CRMHelper.temporaryData as object[]; if (parameters.Count() != 3) { new Exception("RecordModifyPage needs 3 parameters. Record, Fields, EntityMetadataEx"); } this.record = parameters[0] as Entity; // record for update/related new this.fields = parameters[1] as List <FormFieldData>; // form fields this.entityMetadataEx = parameters[2] as EntityMetadataEx; // target entity } else // It has been restored of page back from lookup page { // Assign lookup value when retruning from lookup page. if (CRMHelper.temporaryData != null && CRMHelper.temporaryData.GetType().Equals(typeof(FormFieldData))) { FormFieldData formfieldData = CRMHelper.temporaryData as FormFieldData; fields.Where(x => x.FieldMetadata.LogicalName == formfieldData.FieldMetadata.LogicalName).First().FieldData = formfieldData.FieldData; } } // Then initialize page Initialize(); progressRing.IsActive = false; }
/// <summary> /// This method called when user navigated to this page. /// </summary> /// <param name="e"></param> protected async override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); progressRing.IsActive = true; // If UserInfo is null, then initialize the helper if (CRMHelper.UserInfo == null) { await CRMHelper.Initialize(); } if (id == Guid.Empty) // If id does not have value, this is new page. { // Check if there is any temporaryData object[] parameters = CRMHelper.temporaryData as object[]; this.id = Guid.Parse(parameters[0].ToString()); this.entityMetadataEx = parameters[1] as EntityMetadataEx; } // if id has value, then this page is navigate back from another page. else { // Check parameter object[] parameters = CRMHelper.temporaryData as object[]; // If first parameter is Entity, then this is back from Modify Page. // In this case, as record may have newer value, clear the cache. if (parameters != null && parameters.Count() == 1 && parameters[0].GetType().Equals(typeof(Entity))) { fields = null; record = null; } } // Initalize the page. Initialize(); progressRing.IsActive = false; }
/// <summary> /// This method is called when user clicks Add button in Related PivotItem /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void abRelatedAdd_Click(object sender, RoutedEventArgs e) { // Get RelatedData to create related record RelatedData relatedData = entityMetadataEx.RelatedEntities.Where(x => x.ReferencingEntity == (CRMGrid.cbLookFor.SelectedItem as EntityMetadataEx).EntityMetadata.LogicalName).First(); // Create Entity instance to created record, by fulling mapped values. Entity targetRecord = await CRMHelper.RetrieveRecordForRelated(record, relatedData, entityMetadataEx); // Make id to empty for sure (not mandatory?) targetRecord.Id = Guid.Empty; // Get Related entity's EntityMetadata EntityMetadataEx targetEntityMetadataEx = CRMHelper.EntityMetadataExCollection.Where(x => x.EntityMetadata.LogicalName == relatedData.ReferencingEntity).First(); // Get fields for create form List <FormFieldData> targetFields = await CRMHelper.RetrieveFormFields(targetEntityMetadataEx); // Resets all the buttons ResetAppBarButtonDisplay(); // Generate parameters. As this is new record, passing null for record object. object[] parameters = new object[] { targetRecord, targetFields, targetEntityMetadataEx }; CRMHelper.temporaryData = parameters; // Then Navigate to RecordModifyPage Frame.Navigate(typeof(RecordModifyPage)); }
/// <summary> /// This method is called to initialze page. /// </summary> private async void Initialize() { #region Initialize AppBarButtons // First of all, set schemaName to check privilege if (entityMetadataEx.EntityMetadata.IsActivity == true) { MainRecordSchemaName = "Activity"; } else { MainRecordSchemaName = entityMetadataEx.EntityMetadata.SchemaName; } // Render AppBarButtons for record. if (CRMHelper.CheckPrivilege(MainRecordSchemaName, PrivilegeType.Create, PrivilegeDepth.Basic)) { abAdd = new AppBarButton(); abAdd.Icon = new SymbolIcon(Symbol.Add); abAdd.Name = "abAdd"; abAdd.Label = loader.GetString("abAdd\\Label"); abAdd.Click += abAdd_Click; commandBar.PrimaryCommands.Add(abAdd); } if (CRMHelper.CheckPrivilege(MainRecordSchemaName, PrivilegeType.Write, PrivilegeDepth.Basic)) { abEdit = new AppBarButton(); abEdit.Icon = new SymbolIcon(Symbol.Edit); abEdit.Name = "abEdit"; abEdit.Label = loader.GetString("abEdit\\Label"); abEdit.Click += abEdit_Click; commandBar.PrimaryCommands.Add(abEdit); } if (CRMHelper.CheckPrivilege(MainRecordSchemaName, PrivilegeType.Delete, PrivilegeDepth.Basic)) { abDelete = new AppBarButton(); abDelete.Icon = new SymbolIcon(Symbol.Delete); abDelete.Name = "abDelete"; abDelete.Label = loader.GetString("abDelete\\Label"); abDelete.Click += abDelete_Click; commandBar.PrimaryCommands.Add(abDelete); } // Check privilege to display Note AppBarButton if (CRMHelper.CheckPrivilege("Note", PrivilegeType.Read, PrivilegeDepth.Basic)) { // If use has read access, then check other privileges too. // AppBarButtons for record. if (CRMHelper.CheckPrivilege("Note", PrivilegeType.Create, PrivilegeDepth.Basic) && CRMHelper.CheckPrivilege(MainRecordSchemaName, PrivilegeType.AppendTo, PrivilegeDepth.Basic)) { abNoteAdd = new AppBarButton(); abNoteAdd.Icon = new SymbolIcon(Symbol.Add); abNoteAdd.Name = "abNoteAdd"; abNoteAdd.Label = loader.GetString("abNoteAdd\\Label"); abNoteAdd.Click += abNoteAdd_Click; commandBar.PrimaryCommands.Add(abNoteAdd); abNoteSave = new AppBarButton(); abNoteSave.Icon = new SymbolIcon(Symbol.Save); abNoteSave.Name = "abNoteSave"; abNoteSave.Label = loader.GetString("abNoteSave\\Label"); abNoteSave.Click += abNoteSave_Click; commandBar.PrimaryCommands.Add(abNoteSave); } if (CRMHelper.CheckPrivilege("Note", PrivilegeType.Delete, PrivilegeDepth.Basic)) { abNoteDelete = new AppBarButton(); abNoteDelete.Icon = new SymbolIcon(Symbol.Delete); abNoteDelete.Name = "abNoteDelete"; abNoteDelete.Label = loader.GetString("abNoteDelete\\Label"); abNoteDelete.Click += abNoteDelete_Click; commandBar.PrimaryCommands.Add(abNoteDelete); abNoteSelect = new AppBarButton(); abNoteSelect.Icon = new SymbolIcon(Symbol.List); abNoteSelect.Name = "abNoteSelect"; abNoteSelect.Label = loader.GetString("abNoteSelect\\Label"); abNoteSelect.Click += abNoteSelect_Click; commandBar.PrimaryCommands.Add(abNoteSelect); abNoteCancel = new AppBarButton(); abNoteCancel.Icon = new SymbolIcon(Symbol.Cancel); abNoteCancel.Name = "abNoteCancel"; abNoteCancel.Label = loader.GetString("abNoteCancel\\Label"); abNoteCancel.Click += abNoteCancel_Click; commandBar.PrimaryCommands.Add(abNoteCancel); } } else { // If no read privilege for note, then hide the pivotitem piNotes.Visibility = Visibility.Collapsed; } #endregion #region Initialize CRMRecordDetail // Assign properties this.CRMRecordDetail.EntityMetadataEx = entityMetadataEx; this.CRMRecordDetail.Id = id; // Assign fields and record if it's cached. If you want to always retrieve latest data, then // do not restore data from cache if (fields != null || record != null) { this.CRMRecordDetail.Record = record; this.CRMRecordDetail.Fields = fields; } // Then road the record await this.CRMRecordDetail.LoadData(); // Get it as reference this.record = CRMRecordDetail.Record; // Get fields as reference this.fields = CRMRecordDetail.Fields; // Put the title this.pvRecord.Title = entityMetadataEx.EntityMetadata.DisplayName.UserLocalizedLabel.Label + ": " + record[entityMetadataEx.EntityMetadata.PrimaryNameAttribute]; #endregion #region Initialize CRMGrid // Now work for related entities pivotitem // Instantiate collection for lookFor combobox EntityMetadataExCollection lookForEntities = new EntityMetadataExCollection(); // Assign all related entities foreach (RelatedData item in entityMetadataEx.RelatedEntities) { EntityMetadataEx em = CRMHelper.EntityMetadataExCollection.Where(x => x.EntityMetadata.ObjectTypeCode == item.ObjectTypeCode).First(); lookForEntities.Add(em); } // Check if there is related entities if (lookForEntities.Count() != 0) { // Assign properties this.CRMGrid.LookForEntities = lookForEntities; CRMGrid.ReferencedEntityLogicalName = entityMetadataEx.EntityMetadata.LogicalName; CRMGrid.ReferencedEntityId = id; // Then load data await this.CRMGrid.LoadData(cbLookFor_SelectedIndex, cbLookIn_SelectedIndex); } #endregion #region Initialize CRMNote // Assign properties this.CRMNotes.Id = id; this.CRMNotes.EntityMetadataEx = entityMetadataEx; // Then load the data await this.CRMNotes.LoadData(); #endregion }