The following sample code shows how you can create a new widget category under the root category and add it to the system:
[C#]
using CMS.PortalEngine;
...
// Get parent category WidgetCategoryInfo parent = WidgetCategoryInfoProvider.GetWidgetCategoryInfo("/");
// If parent exists if (parent != null) { // Create new category object WidgetCategoryInfo wci = new WidgetCategoryInfo();
// Set properties wci.WidgetCategoryDisplayName = "Example category"; wci.WidgetCategoryName = "ExampleCategory"; wci.WidgetCategoryParentID = parent.WidgetCategoryID;
// Save to database WidgetCategoryInfoProvider.SetWidgetCategoryInfo(wci); } |
The following sample code shows how you can create a new widget based on the Abuse report web part under an existing category and add it to the system:
[C#]
using CMS.PortalEngine; using CMS.FormEngine;
...
// Get parent web part and category for widget WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo("AbuseReport"); WidgetCategoryInfo category = WidgetCategoryInfoProvider.GetWidgetCategoryInfo("ExampleCategory");
// If parent web part and category exist + // Parent web part must not be inherited - widgets cannot be created from an inherited web part if ((wpi != null) && (wpi.WebPartParentID == 0) && (category != null)) { // Create new widget object WidgetInfo wi = new WidgetInfo();
// Set properties from parent web part wi.WidgetName = wpi.WebPartName; wi.WidgetDisplayName = wpi.WebPartDisplayName; wi.WidgetDescription = wpi.WebPartDescription;
wi.WidgetProperties = FormHelper.GetFormFieldsWithDefaultValue(wpi.WebPartProperties, "visible", "false");
wi.WidgetWebPartID = wpi.WebPartID; wi.WidgetCategoryID = category.WidgetCategoryID;
// Save new widget to database WidgetInfoProvider.SetWidgetInfo(wi); } |
The following sample code shows how you can delete an existing widget from the system:
[C#]
using CMS.PortalEngine;
...
// Get widget from database WidgetInfo wi = WidgetInfoProvider.GetWidgetInfo("AbuseReport");
// If widget exists if (wi != null) { // Delete from database WidgetInfoProvider.DeleteWidgetInfo(wi.WidgetID); } |