Kentico CMS 6.0 Developer's Guide

Managing widgets

Managing widgets

Previous topic Next topic Mail us feedback on this topic!  

Managing widgets

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a widget.

 

private bool CreateWidget()
{
    // Get parent web part and category for widget
    WebPartInfo webpart = WebPartInfoProvider.GetWebPartInfo("AbuseReport");

  WidgetCategoryInfo category = WidgetCategoryInfoProvider.GetWidgetCategoryInfo("MyNewCategory");

 
    // Widgets cannot be created from an inherited web part
    if ((webpart != null) && (webpart.WebPartParentID == 0) && (category != null))
    {
        // Create new widget object 
        WidgetInfo newWidget = new WidgetInfo();
 
        // Set the properties from the parent web part
        newWidget.WidgetName = "MyNewWidget";
        newWidget.WidgetDisplayName = "My new widget";
        newWidget.WidgetDescription = webpart.WebPartDescription;

 
       newWidget.WidgetProperties = FormHelper.GetFormFieldsWithDefaultValue(webpart.WebPartProperties, "visible", "false");

 
        newWidget.WidgetWebPartID = webpart.WebPartID;
        newWidget.WidgetCategoryID = category.WidgetCategoryID;
 
        // Save new widget
        WidgetInfoProvider.SetWidgetInfo(newWidget);
 
        return true;
    }
 
    return false;
}

 

The following example gets and updates a widget.

 

private bool GetAndUpdateWidget()
{
    // Get the widget
    WidgetInfo updateWidget = WidgetInfoProvider.GetWidgetInfo("MyNewWidget");
    if (updateWidget != null)
    {
        // Update the properties
        updateWidget.WidgetDisplayName = updateWidget.WidgetDisplayName.ToLower();
 
        // Save the changes
        WidgetInfoProvider.SetWidgetInfo(updateWidget);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates widgets.

 

private bool GetAndBulkUpdateWidgets()
{
    // Prepare the parameters
    string where = "WidgetName LIKE N'MyNewWidget%'";
    string orderBy = "";
    int topN = 0;
    string columns = "";

 
  // Get the data
  DataSet widgets = WidgetInfoProvider.GetWidgets(where, orderBy, topN, columns);

    if (!DataHelper.DataSourceIsEmpty(widgets))
    {
        // Loop through the individual items
        foreach (DataRow widgetDr in widgets.Tables[0].Rows)
        {
            // Create object from DataRow
            WidgetInfo modifyWidget = new WidgetInfo(widgetDr);

 
          // Update the properties
           modifyWidget.WidgetDisplayName = modifyWidget.WidgetDisplayName.ToUpper();

 
            // Save the changes
            WidgetInfoProvider.SetWidgetInfo(modifyWidget);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a widget.

 

private bool DeleteWidget()
{
    // Get the widget
    WidgetInfo deleteWidget = WidgetInfoProvider.GetWidgetInfo("MyNewWidget");
 
    // Delete the widget
    WidgetInfoProvider.DeleteWidgetInfo(deleteWidget);
 
    return (deleteWidget != null);
}