We usually need to have our dynamic module items sorted in a particular sequence, regardless of their creation and publication date. The normal approach to do this is to add a numeric field to the module and enter values manually. This could prove to be really hard to manage ordinals when there are a lot of items.
To manage ordinal easily through UI, we will perform some steps with following points in mind.
- Assign the correct ordinal to newly created items.
- Create an ordinal service which will manage reordering items.
- Same functionality should work for all dynamic modules.
- Add action menu items (move up, move down).
Global.asax.cs
First off, we will create a logic to assign the correct sort order number to newly created items. Please note that this will require a field named SortOrder defined in your module as numeric field, hidden.
protected void Application_Start(object sender, EventArgs e) { Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized); } private void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e) { if (e.CommandName == "Bootstrapped") { EventHub.Subscribe<IDynamicContentCreatedEvent>(evt => IDynamicContentCreatedEvent(evt)); } } private void IDynamicContentCreatedEvent(IDynamicContentCreatedEvent eventInfo) { var userId = eventInfo.UserId; var creationDate = eventInfo.CreationDate; var item = eventInfo.Item; var visible = eventInfo.Visible; if (item.ApprovalWorkflowState != "Published" && item.DoesFieldExist("SortOrder")) { DynamicModuleManager manager = new DynamicModuleManager(); var items = manager.GetDataItems(item.GetType()).Where(i => i.ApprovalWorkflowState == "Published" && i.Status == ContentLifecycleStatus.Live); var num = items.Count() + 1; item.SetValue("SortOrder", num); PublishDynamicItem(item); } } private bool PublishDynamicItem(DynamicContent item) { try { var manager = DynamicModuleManager.GetManager(); item = manager.Lifecycle.GetMaster(item) as DynamicContent; item.SetWorkflowStatus(manager.Provider.ApplicationName, "Draft"); ILifecycleDataItem publishedDealerItem = manager.Lifecycle.Publish(item); item.SetWorkflowStatus(manager.Provider.ApplicationName, "Published"); manager.SaveChanges(); return true; } catch (DataStoreException dse) { return false; } catch (Exception ex) { return false; } }
This will assign next possible sort order value to the item so that sequence doesn’t break.
Ordinal Service
Copy this folder to /Sitefinity/Services folder. This contains following files:
- IOrdinalService.cs
- OrdinalScript.js
- OrdinalService.cs
- OrdinalService.svc
The service will swap the position of the moved item with the item above or below it when moved up or below respectively. This of course requires a field named SortOrder in your dynamic module.
Add Widget to Action Menu
Go to /Sitefinity/Administration/Settings/Advanced and drill down to nodes DynamicModules > Controls > [Your Dynamic Module] > Views > BackendList > Scripts > Create New.
- Script location: ~/Sitefinity/Services/Ordinal/OrdinalScript.js
- Name of the load method: itemsListLoaded
Then go to DynamicModules > Controls > [Your Dynamic Module] > Views > BackendList > View Modes > Grid > Columns > Actions > MenuItems > Create New > LiteralWidgetElement.
- Name: Separator1
- CssClass: sfSeparator
- WrapperTagName: Li
- Type: Telerik.Sitefinity.Web.UI.Backend.Elements.Widgets.LiteralWidget, Telerik.Sitefinity
- Type checkbox: Check it
Go back to MenuItems and add CommandWidgetElement.
- Command name: moveup
- Name: MoveUp
- CssClass: sfMoveUp
- CommandText: Move up
- WrapperTagName: Li
- Type: Telerik.Sitefinity.Web.UI.Backend.Elements.Widgets.CommandWidget, Telerik.Sitefinity
Again, go back to MenuItems and add another CommandWidgetElement.
- Command name: movedown
- Name: MoveDown
- CssClass: sfMoveDown
- CommandText: Move down
- WrapperTagName: Li
- Type: Telerik.Sitefinity.Web.UI.Backend.Elements.Widgets.CommandWidget, Telerik.Sitefinity
Finally, update grid sort expression so that you would see the changes right away in the list. Go to DynamicModules > Controls > [Your Dynamic Module] > Views > BackendList.
- Sort expression: SortOrder ASC
And you’re done, Happy Coding!
And we know you want to learn more! So, register now for FalafelCon 2014 and perfect your skills, expand your horizons, and get inspired.
The post Reordering Dynamic Module Items with Action Menu appeared first on Falafel Software Blog.