The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
In the previous article we have seen how we can automatically generate commands based on the Views that we add to the Control Panel class. In many scenarios, this feature will fit perfectly with our requirements and it will save us some time.
There are, however, cases when we need to have a better control over the command panels and the commands inside of them. Let us take a look at the built-in Blogs module for an example of such scenario.
On the first image we see the blogs module and its command panels when the module is in “All blogs” mode. On the second image we see the different command panels that are visible when we are in a “Single blog mode” or in other words, when we enter one of the blogs.
Obviously, the command panels change based on some condition (displaying all blogs or single blog). This is achieved by adding command panels and commands manually and in this article we are going to explore this approach.
First thing we need to do is ensure that commands are not being generated automatically.
*** BEGIN NOTE ***
By default, base CommandPanel and ProviderCommandPanel classes will generate commands automatically.
*** END NOTE ***
In order to turn off the automatic command generation we have to construct the base ControlPanel or ProviderControlPanel class with the false value sent as the autoGenerateViewCommand argument. For example, in our sample contacts module we have done so in following manner:
/// <summary> |
/// Initializes a new instance of the <see cref="ContactsControlPanel"/> class. |
/// </summary> |
public ContactsControlPanel() : base(false) |
{ |
} |
/// <summary> |
/// Loads configured views. |
/// </summary> |
protected override void CreateViews() |
{ |
AddView<ContactsItemsView>(null, "AllContacts", "CommandPanel_ContactsItems_Desc", "all", Messages.ResourceManager); |
AddView<PermissionsView<ContactsControlPanel>>(null, "Permissions", "CommandPanel_Permissions_Desc", "globalPerm", Messages.ResourceManager); |
} |
/// <summary> |
/// When overridden this method returns a list of standard Command Panels. |
/// </summary> |
/// <param name="viewMode">The view mode.</param> |
/// <param name="commandsInfo">A list of <see cref="CommandItem">ControlPanel.CommandItem</see> objects.</param> |
/// <param name="list">A list of Command Panels for this Control Panel. This list can be used to add and remove Command Panels.</param> |
/// <remarks> |
/// This method will automatically create Command Panels |
/// based on the information passed with CommandItem classes. |
/// One command panel will be created per each |
/// unique panel name specified in the collection of command items. |
/// Command panels will appear in the order they were created. |
/// </remarks> |
protected override void CreateStandardCommandPanels(string viewMode, List<CommandItem> commandsInfo, List<Telerik.Web.ICommandPanel> list) |
{ |
switch (viewMode) |
{ |
case "BlogsView": |
case "BlogsPermissionsView": |
Cms.Web.UI.Backend.CommandPanel blogsCommandPanel = new Cms.Web.UI.Backend.CommandPanel(); |
blogsCommandPanel.Title = Messages.ExploreBlogsMogule; |
blogsCommandPanel.AddCommand("BlogsView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, Messages.AllBlogs, Messages.AllBlogsDescription, "all"); |
blogsCommandPanel.AddCommand("BlogsPermissionsView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, Messages.Permissions, Messages.PermissionsDescription, "globalPerm"); |
list.Add(blogsCommandPanel); |
break; |
default: |
// helper command panel |
Cms.Web.UI.Backend.CommandPanel helperCommandPanel = new Cms.Web.UI.Backend.CommandPanel(); |
helperCommandPanel.AddCommand("BlogsView", "BlogsControlPanel", null, null, Messages.AllBlogs, null, "backWrapp"); |
list.Add(helperCommandPanel); |
// posts command panel |
Cms.Web.UI.Backend.CommandPanel postsCommandPanel = new Cms.Web.UI.Backend.CommandPanel(); |
postsCommandPanel.Title = Messages.ExploreThisBlog; |
postsCommandPanel.AddCommand("PostsView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, this.ParentId.ToString(), null, Messages.Posts, null, "all"); |
if(this.Manager.Provider.AllowComments) |
postsCommandPanel.AddCommand("BlogCommentsView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, this.ParentId.ToString(), null, Messages.Comments, null, "all"); |
postsCommandPanel.AddCommand("BlogCategoriesView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, this.ParentId.ToString(), null, Messages.Categories, null, "all"); |
postsCommandPanel.AddCommand("BlogTagsView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, this.ParentId.ToString(), null, Messages.Tags, null, "all"); |
list.Add(postsCommandPanel); |
// blog settings command panel |
Cms.Web.UI.Backend.CommandPanel blogSettingsPanel = new Cms.Web.UI.Backend.CommandPanel(); |
blogSettingsPanel.Title = Messages.BlogSettingsTitle; |
blogSettingsPanel.AddCommand("BlogsSettingsView", "BlogsControlPanel.BlogSettingsView", null, this.ParentId.ToString(), Messages.BlogSettingsTitle, null, "settings"); |
list.Add(blogSettingsPanel); |
break; |
} |
} |
private List<string> GetControlPanelRoute() |
{ |
List<string> controlPanelRoute = ControlUtils.ParseRoute(ControlUtils.GetCurrentRoute()); |
if(controlPanelRoute.Count == 0) |
controlPanelRoute.Add(this.Name); |
return controlPanelRoute; |
} |
So the very simple explanation of the CreateStandardCommandPanels method in the example above would go like this:
If the current view is BlogsView or BlogsPermissionsView create a command panel for the all blogs mode and add several commands to it - at the end add the newly created command panel to the list of all command panels which we have received through the list parameter. On the other hand, if the current view is any other view except BlogsView or BlogsPermissionsView create three different command panels, add some commands to them and finally add all three newly created command panels to the list of all command panels we have received through the list parameter.
Having this understanding, we know that before we can add commands, we need to create a command panel. One can create command panel in following manner:
Cms.Web.UI.Backend.CommandPanel blogsCommandPanel = new Cms.Web.UI.Backend.CommandPanel(); |
The signatures of the AddCommand methods are rather simple as the following example suggests:
postsCommandPanel.AddCommand("BlogCategoriesView", ControlUtils.SliceRoute(GetControlPanelRoute(), this.Name), null, this.ParentId.ToString(), null, Messages.Categories, null, "all"); |
public virtual void AddCommand(string commandName, List<string> commandRoute, string parameter, string parentId, string commandArgs, string title, string description, string cssClass) |
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites