Kentico CMS 7.0 Developer's Guide

Managing web parts

Managing web parts

Previous topic Next topic Mail us feedback on this topic!  

Managing web parts

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 web part.

 

private bool CreateWebPart()
{

  // Get parent category for web part
  WebPartCategoryInfo category = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("MyNewCategory");

 

    if (category != null)
    {
        // Create new web part object
        WebPartInfo newWebpart = new WebPartInfo();
 
        // Set the properties
        newWebpart.WebPartDisplayName = "My new web part";
        newWebpart.WebPartName = "MyNewWebpart";
        newWebpart.WebPartDescription = "This is my new web part.";
        newWebpart.WebPartFileName = "whatever";
        newWebpart.WebPartProperties = "<form></form>";
        newWebpart.WebPartCategoryID = category.CategoryID;
 
        // Save the web part
        WebPartInfoProvider.SetWebPartInfo(newWebpart);
 
        return true;
    }
 
    return false;
}

 

The following example gets and updates a web part.

 

private bool GetAndUpdateWebPart()
{
    // Get the web part
    WebPartInfo updateWebpart = WebPartInfoProvider.GetWebPartInfo("MyNewWebpart");
    if (updateWebpart != null)
    {

      // Update the properties
       updateWebpart.WebPartDisplayName = updateWebpart.WebPartDisplayName.ToLower();

 

        // Save the changes
        WebPartInfoProvider.SetWebPartInfo(updateWebpart);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates web parts.

 

private bool GetAndBulkUpdateWebParts()
{
    // Prepare the parameters
    string where = "WebPartName LIKE N'MyNewWebpart%'";
 
    // Get the data
    DataSet webparts = WebPartInfoProvider.GetWebParts(where, null);
    if (!DataHelper.DataSourceIsEmpty(webparts))
    {
        // Loop through the individual items
        foreach (DataRow webpartDr in webparts.Tables[0].Rows)
        {
            // Create object from DataRow
            WebPartInfo modifyWebpart = new WebPartInfo(webpartDr);
 

          // Update the properties
           modifyWebpart.WebPartDisplayName = modifyWebpart.WebPartDisplayName.ToUpper();

 
            // Save the changes
            WebPartInfoProvider.SetWebPartInfo(modifyWebpart);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a web part.

 

private bool DeleteWebPart()
{
    // Get the web part
    WebPartInfo deleteWebpart = WebPartInfoProvider.GetWebPartInfo("MyNewWebpart");
 
    // Delete the web part
    WebPartInfoProvider.DeleteWebPartInfo(deleteWebpart);
        
    return (deleteWebpart != null);
}