public HttpResponseMessage Save([FromBody] List <DashboardViewModel> model) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); // dashboard data is stored using the user id so, we need to get the user var currentUser = PortalsHelpers.GetCurrentUser(); // loop through the dashboard items and get them ready for persisting foreach (var dashboardModel in model) { // this will be our final list of items that we'll store var dashboardItems = new List <DashboardItem>(); foreach (var dashboardItem in dashboardModel.DashboardItems) { // this ensures that the user has access to the dashboard item they're trying to save ToolboxItem toolboxItem = null; if (PortalsHelpers.GetToolboxItem(dashboardItem.ControllerType, out toolboxItem)) { // get the properties that users are able to change on the front end var configurableProperties = PortalsHelpers.GetConfigurableProperties(toolboxItem.ControllerType); var configuredProperties = dashboardItem.Properties;//.FromJson<List<PortalsItemProperty>>(); // just to keep things tidy... configuredProperties.RemoveAll(p => p.Value == string.Empty); // if the user has actually set any configurable property values, make sure they're valid foreach (var property in configuredProperties) { // check to make sure a previously configurable property is still configurable if (!configurableProperties.Any(p => p.Name == property.Name)) { configuredProperties.Remove(property); } } // done with sanitization, let's build the item we're going to persist var item = new DashboardItem() { ControllerType = dashboardItem.ControllerType, Properties = configuredProperties.Select(p => new PortalsItemProperty() { Name = p.Name, Value = p.Value }).ToList() }; dashboardItems.Add(item); } } currentUser.SaveDashboard(dashboardModel.DashboardId, dashboardItems); } return(response); }
// *sigh* yes....i'm doing this in the controller. Feel free to move it somewhere else. :) private DashboardViewModel GetDashboardModel(bool executeControllers = true) { var dashboardsManager = UserDashboardsManager.GetManager(); // dashboard data is stored using the user id so, we need to get the user var currentUser = PortalsHelpers.GetCurrentUser(); // get this dashboard's data for the current user var dashboard = currentUser.GetDashboard(DashboardId); // instantiate a new DashboardViewModel and set the property values that we know we need var model = new DashboardViewModel(); model.DashboardId = DashboardId; model.DashboardItems = new List <DashboardItemViewModel>(); // loop through the dashboard items and create view models for them foreach (var dashboardItem in dashboard.DashboardItems) { // get the corresponding Sitefinity ToolboxItem for the given dashboard item type ToolboxItem toolboxItem = null; if (PortalsHelpers.GetToolboxItem(dashboardItem.ControllerType, out toolboxItem)) { // MvcControllerProxy is the source of all this sorcery var proxy = new MvcControllerProxy() { ControllerName = toolboxItem.ControllerType }; // get the properties that users are able to change on the front end // this will *not* include any user defined values...just properties that *can* be configured var configurableProperties = PortalsHelpers.GetConfigurableProperties(toolboxItem.ControllerType); // this will be the property list (with user values set) that we pass to the view var propertyViewModels = new List <PortalsItemPropertyViewModel>(); foreach (var property in configurableProperties) { string value = string.Empty; // if the user has actually set any configurable property value, transfer the value to the controller if (dashboardItem.Properties.Any(p => p.Name == property.Name)) { value = dashboardItem.Properties.Single(p => p.Name == property.Name).Value; // type the value correctly object propValue = PortalsHelpers.GetValueFromString(value, property.PropertyType); // fairly confident that this isn't a good way to do this but...it works var backingProperty = configurableProperties.Where(p => p.Name == property.Name).Single().DashboardConfigurableAttribute.BackingProperty ?? property.Name; proxy.Controller.SetProperty(backingProperty, propValue); } // build the view model for the front end var propertyViewModel = new PortalsItemPropertyViewModel(); propertyViewModel.Name = property.Name; propertyViewModel.DisplayName = configurableProperties.Where(p => p.Name == property.Name).Single().DashboardConfigurableAttribute.DisplayName ?? property.Name; propertyViewModel.Value = value; propertyViewModels.Add(propertyViewModel); } // build a new DashboardItemViewModel with all the properties we need to render, configure, and save the item var dashboardItemModel = new DashboardItemViewModel(); dashboardItemModel.Title = toolboxItem.Title; dashboardItemModel.ControllerType = toolboxItem.ControllerType; dashboardItemModel.Properties = propertyViewModels; dashboardItemModel.Html = executeControllers ? ExecuteController(proxy) : string.Empty; model.DashboardItems.Add(dashboardItemModel); } } return(model); }