Web parts
List of examples:
Web part categories
Creating a web part category
// Gets the root category of the web part tree
WebPartCategoryInfo root = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("/");
if (root != null)
{
// Creates a new web part category object
WebPartCategoryInfo newCategory = new WebPartCategoryInfo();
// Sets web part category properties
newCategory.CategoryDisplayName = "New category";
newCategory.CategoryName = "NewCategory";
newCategory.CategoryParentID = root.CategoryID;
// Saves the web part category to the database
WebPartCategoryInfoProvider.SetWebPartCategoryInfo(newCategory);
}
Updating a web part category
// Gets the web part category
WebPartCategoryInfo updateCategory = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("NewCategory");
if (updateCategory != null)
{
// Updates the web part category properties
updateCategory.CategoryDisplayName = updateCategory.CategoryDisplayName.ToLower();
// Saves the changes to the database
WebPartCategoryInfoProvider.SetWebPartCategoryInfo(updateCategory);
}
Updating multiple web part categories
// Gets all Web part categories whose display name starts with 'New category'
var categories = WebPartCategoryInfoProvider.GetCategories().WhereStartsWith("CategoryDisplayName", "New category");
// Loops through the web part category objects
foreach (WebPartCategoryInfo modifyCategory in categories)
{
// Updates the properties
modifyCategory.CategoryDisplayName = modifyCategory.CategoryDisplayName.ToUpper();
// Saves the changes to the database
WebPartCategoryInfoProvider.SetWebPartCategoryInfo(modifyCategory);
}
Deleting a web part category
// Gets the web part category
WebPartCategoryInfo deleteCategory = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("NewCategory");
// Deletes the web part category
WebPartCategoryInfoProvider.DeleteCategoryInfo(deleteCategory);
Web parts
Creating a new web part
// Gets a parent category for the new web part
WebPartCategoryInfo category = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("NewCategory");
if (category != null)
{
// Creates a new web part object
WebPartInfo newWebpart = new WebPartInfo();
// Sets the web part properties
newWebpart.WebPartDisplayName = "New web part";
newWebpart.WebPartName = "NewWebpart";
newWebpart.WebPartDescription = "This is a new web part.";
newWebpart.WebPartFileName = "FileName";
newWebpart.WebPartProperties = "<form></form>";
newWebpart.WebPartCategoryID = category.CategoryID;
// Saves the web part to the database
WebPartInfoProvider.SetWebPartInfo(newWebpart);
}
Updating a web part
// Gets the web part
WebPartInfo updateWebpart = WebPartInfoProvider.GetWebPartInfo("NewWebpart");
if (updateWebpart != null)
{
// Updates the web part properties
updateWebpart.WebPartDisplayName = updateWebpart.WebPartDisplayName.ToLower();
// Saves the changes to the database
WebPartInfoProvider.SetWebPartInfo(updateWebpart);
}
Updating multiple web parts
// Gets all web parts whose name starts with 'NewWebpart'
var webparts = WebPartInfoProvider.GetWebParts().WhereStartsWith("WebPartName", "NewWebpart");
// Loop through individual web parts
foreach (WebPartInfo webPart in webparts)
{
// Updates the web part properties
webPart.WebPartDisplayName = webPart.WebPartDisplayName.ToUpper();
// Saves the changes to the database
WebPartInfoProvider.SetWebPartInfo(webPart);
}
Deleting a web part
// Gets the web part
WebPartInfo deleteWebpart = WebPartInfoProvider.GetWebPartInfo("NewWebpart");
// Deletes the web part
WebPartInfoProvider.DeleteWebPartInfo(deleteWebpart);
Web part layouts
Creating a new layout for a web part
// Gets the web part
WebPartInfo webpart = WebPartInfoProvider.GetWebPartInfo("NewWebpart");
if (webpart != null)
{
// Creates a new web part layout object
WebPartLayoutInfo newLayout = new WebPartLayoutInfo();
// Sets the web part layout properties
newLayout.WebPartLayoutDisplayName = "New layout";
newLayout.WebPartLayoutCodeName = "NewLayout";
newLayout.WebPartLayoutWebPartID = webpart.WebPartID;
newLayout.WebPartLayoutCode = "This is the new layout of the NewWebpart web part.";
// Saves the web part layout to the database
WebPartLayoutInfoProvider.SetWebPartLayoutInfo(newLayout);
}
Updating a web part layout
// Gets the web part layout
WebPartLayoutInfo updateLayout = WebPartLayoutInfoProvider.GetWebPartLayoutInfo("NewWebpart", "NewLayout");
if (updateLayout != null)
{
// Updates the web part layout properties
updateLayout.WebPartLayoutDisplayName = updateLayout.WebPartLayoutDisplayName.ToLower();
// Saves the changes to the database
WebPartLayoutInfoProvider.SetWebPartLayoutInfo(updateLayout);
}
Updating multiple web part layouts
// Gets all web part layouts whose code name starts with 'NewLayout'
var layouts = WebPartLayoutInfoProvider.GetWebPartLayouts().WhereStartsWith("WebPartLayoutCodeName", "NewLayout");
// Loops through individual web part layouts
foreach (WebPartLayoutInfo layout in layouts)
{
// Updates the web part layout properties
layout.WebPartLayoutDisplayName = layout.WebPartLayoutDisplayName.ToUpper();
// Saves the changes to the database
WebPartLayoutInfoProvider.SetWebPartLayoutInfo(layout);
}
Deleting a web part layout
// Gets the web part layout
WebPartLayoutInfo deleteLayout = WebPartLayoutInfoProvider.GetWebPartLayoutInfo("NewWebpart", "NewLayout");
// Deletes the web part layout
WebPartLayoutInfoProvider.DeleteWebPartLayoutInfo(deleteLayout);