Minimize a SharePoint web part programmatically

We have lot of times minimized a web part on a SharePoint page. That day there was a requirement to minimize the web part programmatically. It’s simple, see it how

this.ChromeState = PartChromeState.Minimized;

ChromeState options are Minimized or Normal, with Normal being the default. Normal will display your Web Part on the site in its entirety, whereas Minimized will display only the Web Part’s title bar

Listing all web parts on a web part page

There are times when you need to see the list of all the web parts on a SharePoint page. It can be done in two ways

First Way:
Browse to the SharePoint page and append the following querystring to the url

e.g. http://yoursite/default.aspx?contents=1

Second Way:
You can write code to achieve the same functionality and list down the web parts on the page.

using(new SPSite("http://mysite/myweb").OpenWeb())

//give relative path of the webpartpage
SPLimitedWebPartManager wm = page.GetLimitedWebPartManager("default.aspx", 
    foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wm.WebParts)

Connecting web parts programmatically

We need SPLimitedWebPartManager to connect the Web parts with ASP.NET style interfaces. The steps for this are:

  1. Get the SPLimitedWebPartManager instance on the page;
  2. Get the provider and consumer Web parts need to be connected;
  3. Get the connection points for the provider and consumer Web parts;
  4. Connect them using SPConnectWebParts method. If necessary, certain transforming needs to be in place for compatible interfaces
SPWeb web = (SPWeb)properties.Feature.Parent;      
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);      
System.Web.UI.WebControls.WebParts.WebPart provider= mgr.WebParts["Provider"];   
System.Web.UI.WebControls.WebParts.WebPart consumer= mgr.WebParts["Consumer"];  
ConsumerConnectionPoint addConsumerConnPoint = mgr.GetConsumerConnectionPoints(consumer)["AddressConsumer_ot"];   
ProviderConnectionPoint addProviderConnPoint = mgr.GetProviderConnectionPoints(provider)["AddressProvider_ot"];      
mgr.SPConnectWebParts(provider, addProviderConnPoint, consumer, addConsumerConnPoint);