// Get parent category for web part WebPartCategoryInfo category = WebPartCategoryInfoProvider.GetWebPartCategoryInfoByCodeName("MyNewCategory");
if (category != null) { // Create new web part object WebPartInfo newWebpart = newWebPartInfo(); // 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); returntrue; } returnfalse; }
The following example gets and updates a web part.
privatebool 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); returntrue; } returnfalse; }
The following example gets and bulk updates web parts.
privatebool 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 = newWebPartInfo(webpartDr);
// Update the properties modifyWebpart.WebPartDisplayName = modifyWebpart.WebPartDisplayName.ToUpper();
// Save the changes WebPartInfoProvider.SetWebPartInfo(modifyWebpart); } returntrue; } returnfalse; }
The following example deletes a web part.
privatebool DeleteWebPart() { // Get the web part WebPartInfo deleteWebpart = WebPartInfoProvider.GetWebPartInfo("MyNewWebpart"); // Delete the web part WebPartInfoProvider.DeleteWebPartInfo(deleteWebpart); return (deleteWebpart != null); }