privatebool CreateWebPartLayout() { // Get the web part WebPartInfo webpart = WebPartInfoProvider.GetWebPartInfo("MyNewWebpart"); if (webpart != null) { // Create new web part layout object WebPartLayoutInfo newLayout = newWebPartLayoutInfo(); // Set the properties newLayout.WebPartLayoutDisplayName = "My new layout"; newLayout.WebPartLayoutCodeName = "MyNewLayout"; newLayout.WebPartLayoutWebPartID = webpart.WebPartID;
newLayout.WebPartLayoutCode = "This is the new layout of MyNewWebpart webpart.";
// Save the web part layout WebPartLayoutInfoProvider.SetWebPartLayoutInfo(newLayout); returntrue; } returnfalse; }
// Update the properties updateLayout.WebPartLayoutDisplayName = updateLayout.WebPartLayoutDisplayName.ToLower();
// Save the changes WebPartLayoutInfoProvider.SetWebPartLayoutInfo(updateLayout); returntrue; } returnfalse; }
The following example gets and bulk updates layouts.
privatebool GetAndBulkUpdateWebPartLayouts() { // Prepare the parameters string where = "WebPartLayoutCodeName LIKE N'MyNewLayout%'"; // Get the data DataSet layouts = WebPartLayoutInfoProvider.GetWebPartLayouts(where, null); if (!DataHelper.DataSourceIsEmpty(layouts)) { // Loop through the individual items foreach (DataRow layoutDr in layouts.Tables[0].Rows) { // Create object from DataRow WebPartLayoutInfo modifyLayout = newWebPartLayoutInfo(layoutDr);