/// <summary> /// Helper method helping locate the desired layout control, whcih will be used as a placeholder for our widget /// </summary> /// <param name="targetedLayoutPlaceHolders"></param> /// <param name="dashboard"></param> /// <param name="placeHolderIndex"></param> /// <returns></returns> private PageDraftControl GetTargetedControl(string[] targetedLayoutPlaceHolders, PageDraft dashboard, int placeHolderIndex) { PageDraftControl targetedControl = null; for (int layoutIndex = 0; layoutIndex < targetedLayoutPlaceHolders.Length; layoutIndex++) { var targetedLayoutPlaceHolder = targetedLayoutPlaceHolders[layoutIndex]; foreach (var dashboardControl in dashboard.Controls) { if (dashboardControl.IsLayoutControl) { if (dashboardControl.PlaceHolders != null && dashboardControl.PlaceHolders.Length > placeHolderIndex) { if (dashboardControl.PlaceHolders[placeHolderIndex].StartsWith(targetedLayoutPlaceHolder)) { targetedControl = dashboardControl; break; } } } } if (targetedControl != null) { break; } else { continue; } } return(targetedControl); }
/// <summary> /// Here we are taking care of adding the Db diagnostics dashboard widget to the Sitefintiy dashboard /// First, we are registering our widget in the backend pages toolbox /// Then we are instantiating the Db diagnostics dashboard widget /// And adding it to the Dashboard backend page /// Note that we must specify a valid placeholder where the widget should be placed /// We must also specify the widget caption /// Another specific you should pay attention to is the permissions configuration for the widget /// In this case we are making sure the widget is visible only to Administrators /// </summary> /// <param name="initializer"></param> /// <param name="pageNode"></param> private void AddDashboardDBDiagnosticsWidgetToPage(SiteInitializer initializer, PageNode pageNode) { initializer.Installer .Toolbox(CommonToolbox.PageWidgets) .LoadOrAddSection("Dashboard") .LocalizeUsing <Labels>() .SetTitle("DashboardToolboxSectionTitle") .SetDescription("DashboardToolboxSectionDescription") .SetTags(ToolboxTags.Backend) .SetOrdinal(-1) // should be first .LoadOrAddWidget <DashboardDBDiagnosticsView>("DashboardDBDiagnosticsView") .SetTitle("DashboardDBDiagnosticsViewTitle") .SetDescription("DashboardDBDiagnosticsViewDescription") .LocalizeUsing <DBDiagnosticsToolResources>() .Done(); var dashboardSystemStatusView = new DashboardDBDiagnosticsView(); dashboardSystemStatusView.ID = "dashboardDBDiagnosticsView"; if (pageNode != null) { var topWidgetLayout = pageNode.GetPageData().Controls.Where(c => c.IsLayoutControl && c.PlaceHolders.Length > 0 && c.PlaceHolders[0].StartsWith("WidgetsLayoutTop")).FirstOrDefault(); if (topWidgetLayout != null) { var placeholder = topWidgetLayout.PlaceHolders[0]; PageDraftControl pageDraftControl = initializer.PageManager.CreateControl <PageDraftControl>(dashboardSystemStatusView, placeholder); pageDraftControl.Caption = Res.Get <DBDiagnosticsToolResources>().DashboardDBDiagnosticsViewTitle; pageDraftControl.Permissions.Clear(); // Should be visible only for administrators AddWidgetToDashboard(initializer.PageManager, pageNode, pageDraftControl, new string[] { "WidgetsLayoutTop" }, true, placeHolderIndex: 1); } } }
/// <summary> /// Helper method facilitating the logic of adding a widget to a page /// </summary> /// <param name="pageManager"></param> /// <param name="pageNode"></param> /// <param name="control"></param> /// <param name="targetedLayoutPlaceHolders"></param> /// <param name="skipIfContainsControl"></param> /// <param name="placeHolderIndex"></param> private void AddWidgetToDashboard(PageManager pageManager, PageNode pageNode, PageDraftControl control, string[] targetedLayoutPlaceHolders, bool skipIfContainsControl = false, int placeHolderIndex = 0) { RemoveDraftsVersions(pageNode, pageManager); var dashboard = pageManager.PagesLifecycle.Edit(pageNode.GetPageData()); PageDraftControl targetedControl = GetTargetedControl(targetedLayoutPlaceHolders, dashboard, placeHolderIndex); if (targetedControl == null) { return; } var layoutPlaceholder = targetedControl.PlaceHolders[placeHolderIndex]; var controlsWithSamePlaceholder = dashboard.Controls.Where(c => c.PlaceHolder == layoutPlaceholder); var containsControl = controlsWithSamePlaceholder.Any(c => c.ObjectType == control.ObjectType); if (!containsControl || (containsControl && !skipIfContainsControl)) { PageDraftControl firstControl = null; if (controlsWithSamePlaceholder != null) { firstControl = controlsWithSamePlaceholder.Where(c => c.SiblingId.Equals(Guid.Empty)).FirstOrDefault(); } control.PlaceHolder = targetedControl.PlaceHolders[placeHolderIndex]; control.SiblingId = Guid.Empty; if (firstControl != null) { firstControl.SiblingId = control.Id; } dashboard.Controls.Add(control); dashboard.ApprovalWorkflowState.Value = "Published"; pageManager.PagesLifecycle.Publish(dashboard); } else { RemoveDraftsVersions(pageNode, pageManager); } }
public string GetControlContent(PageDraftControl pageDraftControl) { var control = GetPageManagerForProvider().LoadControl(pageDraftControl) as Telerik.Sitefinity.Mvc.Proxy.MvcControllerProxy; return(control.Settings.Values[Constants.Content]); }