Add Web Part Programatically on Home / Default Page
Hi,
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Get a reference to a web and a list
SPWeb web = properties.Feature.Parent as SPWeb;
string pageURL=web.RootFolder.WelcomePage;
if(pageURL=="")
pageURL="default.aspx";
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageURL , System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
// Add the web part
mgr.AddWebPart(CreatePageWebPart(web.Url), "Left", 1);
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
// Get a reference to a web and a list
SPWeb web = properties.Feature.Parent as SPWeb;
string pageURL = web.RootFolder.WelcomePage;
if (pageURL == "")
pageURL = "default.aspx";
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageURL, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
for (int i = 0; i < mgr.WebParts.Count; i++ )
{
if (mgr.WebParts[i].ID == "Tasks-My_Tasks")
{
mgr.DeleteWebPart(mgr.WebParts[i]);
break;
}
}
}
catch (Exception ex) { string s = ex.ToString(); }
}
private XsltListViewWebPart CreatePageWebPart(string webURL)
{
// create our List View web part
XsltListViewWebPart wp = new XsltListViewWebPart();
using (SPSite site = new SPSite(webURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Tasks"];
wp.ID = "Tasks-My_Tasks";
wp.ListId = list.ID;
wp.ListName = list.ID.ToString();
wp.ViewGuid = list.Views["My Tasks"].ID.ToString();
wp.XmlDefinition = list.Views["My Tasks"].GetViewXml();
// set basic properties along with the title
wp.AllowConnect = false ;
wp.Title = "My Tasks :";
wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.Default ;
}
}
return wp;
}
Enjoy Codding!!!
- Create a SharePoint 2010 Solution in Visual Studio.
- Add Feature & its scope.
- Add Feature Receiver Class & the add below mentioned code :
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Get a reference to a web and a list
SPWeb web = properties.Feature.Parent as SPWeb;
string pageURL=web.RootFolder.WelcomePage;
if(pageURL=="")
pageURL="default.aspx";
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageURL , System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
// Add the web part
mgr.AddWebPart(CreatePageWebPart(web.Url), "Left", 1);
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
// Get a reference to a web and a list
SPWeb web = properties.Feature.Parent as SPWeb;
string pageURL = web.RootFolder.WelcomePage;
if (pageURL == "")
pageURL = "default.aspx";
SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageURL, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
for (int i = 0; i < mgr.WebParts.Count; i++ )
{
if (mgr.WebParts[i].ID == "Tasks-My_Tasks")
{
mgr.DeleteWebPart(mgr.WebParts[i]);
break;
}
}
}
catch (Exception ex) { string s = ex.ToString(); }
}
private XsltListViewWebPart CreatePageWebPart(string webURL)
{
// create our List View web part
XsltListViewWebPart wp = new XsltListViewWebPart();
using (SPSite site = new SPSite(webURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Tasks"];
wp.ID = "Tasks-My_Tasks";
wp.ListId = list.ID;
wp.ListName = list.ID.ToString();
wp.ViewGuid = list.Views["My Tasks"].ID.ToString();
wp.XmlDefinition = list.Views["My Tasks"].GetViewXml();
// set basic properties along with the title
wp.AllowConnect = false ;
wp.Title = "My Tasks :";
wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.Default ;
}
}
return wp;
}
Enjoy Codding!!!
Comments