Monday, May 28, 2012

how to create a custom webpart in sharepoint 2010

Title:How to create a custom web part in share point 2010

Description:
In previous post we known how to create and filter the list in SharePoint.Here i will show how to create a custom web part in share point 2010.For this i will use a class SPWebPartCollection to get the existing web parts in page ,then add a new web part to page by creating object for web part.we have two chrome state properties.one is Minimized which can do the border in minimized state and other one is none which gives the normal state  .Chrome type property has for attributes .those are Title only which give only title bar,Border only which gives only border,Title And Border which give both title and border .None which give no one .After set the properties we can add the custom web part to web part list sing add() method

if(inplist!=null && set!=null)
{
using (SPSite ss = new SPSite(URL) 
{   
using (SPWeb sW = ss.OpenWeb())   
{     
SPWebPartCollection webpartscollection = sw.GetWebPartCollection(pagrurl,Storage.Shared);      
WebPartToBeAdded wptest = new WebPartToBeAdded();      
wptest.ZoneID = "zoneid";     
wptest.Title = "My first webpart";
wptest.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Minimized;
wptest.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleAndBorder;  
webpartscollection.Add(wptest);      
sw.Update();    
} 
}
}    

No comments:

Bel