Programmatically add a web part to SharePoint

To add an web part to a page (e.g. default.aspx) in SharePoint use the following code

// Get a reference to a web and a list  
SPSite site = new SPSite("http://localhost:8000");  
SPWeb web = site.OpenWeb();  
SPList list = web.Lists["Tasks"];  
// Instantiate the web part  
ListViewWebPart webpart = new ListViewWebPart();  
webpart.ZoneID = "Left";  
webpart.ListName = list.ID.ToString("B").ToUpper();  
webpart.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();  
// Get the web part collection  
SPWebPartCollection coll =  web.GetWebPartCollection("default.aspx", Storage.Shared);  
// Add the web part  

Read data programmatically from Content Editor Web part

using (SPSite site = new SPSite(""))
using (SPWeb web = site.OpenWeb())
SPFile file = web.GetFile("default.aspx");   
SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);                                   SPLimitedWebPartCollection webParts = lwpm.WebParts;
System.Web.UI.WebControls.WebParts.WebPart wp = webParts[1];                    
string content = (((Microsoft.SharePoint.WebPartPages.ContentEditorWebPart)(wp)).Content).InnerText;                

Customizing Web part shortcut menu (chrome)

Web part shortcut menu allows you to minimize, Close, Modify web part and export. To add new item, edit or modify existing item in menu you need to override CreateWebpartmenu method of WebPart base class and access the MenuItems collection of the Web Part’s WebPartMenu property.
List of Item IDs
• MSOMenu_Minimize
• MSOMenu_Restore
• MSOMenu_Close
• MSOMenu_Delete
• MSOMenu_Edit
• MSOMenu_Connections
• MSOMenu_Export
• MSOMenu_Help

Sample example

public override void CreateWebPartMenu()
MenuItem ParentItem, Item1, Item2;

//first we create three menu items 
//one as a parent item, and two submenu items 
//create the parent item 

ParentItem = new MenuItem("ParentItem", "", "ParentItemID");

//create a submenu item with a server event on click 
Item1 = new MenuItem("Item1", "Item1ID", new EventHandler(EventHandlerForItem1));

//create a submenu item with a client event on click 
Item2 = new MenuItem("Item2", "javascript:alert('Item2 was clicked');", "Item2ID");
// We then add the submenu items to the parent item. 


// We then decide that we'd like to add the parent item after the 
// "Edit Web Part Properties" item in the menu. 
// First we find the index of the "Edit" item 
int EditIndex =

// Then we insert the parent item after the "Edit" item 
this.WebPartMenu.MenuItems.Insert(EditIndex + 1, ParentItem);

// Make sure the parent item has a separator above it 
ParentItem.BeginSection = true;

// Check a custom property to decide whether to display the parent menu 
if (this.CustomProperty1 == true)
ParentItem.Visible = true;
ParentItem.Visible = false;
// Check another custom property to decide which menu item to enable 
// within the parent item 
if (this.CustomProperty2 == true)
Item1.Enabled = true;
Item2.Enabled = false;
Item1.Enabled = false;
Item2.Enabled = true;

Get to the WebPart Maintenance Page

For quick and easy access to the WebPart Maintenance Page simply append the following to the URL of any page with webparts: “?contents=1”.
So for example if your default page URL will look like this:
If you need to go to the WebPart Maintenance page for this page you would add the above to the end of the URL as follows:
This will open the WebPart Maintenance page for this page.You can delete, close, remove the webparts from the page easily.
Note: The changes made on this page are applicable for all the users.