Add or Remove WebParts into SharePoint 2007 on runtime using SPLimitedWebPartManager
If you want to install or remove SharePoint's WebParts on runtime using SharePoint's object model, you need first of all get a reference to the page's SPLimitedWebPartManager. This reference can be easy obtained through the SPWeb object, invoking the GetLimitedWebPartManager() method. Here's a code snippet:
string AbsolutePageUrl = "http://YourSite/Page.aspx";using (SPSite site = new SPSite(AbsolutePageUrl))
{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
}
}
Once you got your SPLimitedWebPartManager you can add or remove your WebParts by calling the AddWebPart or DeleteWebPart methods.{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
}
}
Imagine that you have installed in the farm a custom WebPart and now you need to insert it or delete it from a SharePoint's page. Let's says that your WebPart is named MyWebPart and declared as:
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
The code snippets that you need to use for accomplishing this task are: Adding a WebPart
string AbsolutePageUrl = "http://YourSite/Page.aspx";using (SPSite site = new SPSite(AbsolutePageUrl))
{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
// if MyWebPart is already installed, leave! foreach (var webpart in SpWebPartManger.WebParts)
{
if (webpart is MyWebPart)
return;
}
// else add it in the bottom! SpWebPartManger.AddWebPart(new MyWebPart(), "Main",10);
SpWebPartManger.Dispose();
}
}
{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
// if MyWebPart is already installed, leave! foreach (var webpart in SpWebPartManger.WebParts)
{
if (webpart is MyWebPart)
return;
}
// else add it in the bottom! SpWebPartManger.AddWebPart(new MyWebPart(), "Main",10);
SpWebPartManger.Dispose();
}
}
Removing a WebPart
string AbsolutePageUrl = "http://YourSite/Page.aspx";using (SPSite site = new SPSite(AbsolutePageUrl))
{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
WebPart webPartToDelete = null;
// let's locate the webpart first.... foreach (WebPart webpart in SpWebPartMaanger.WebParts)
{
if (webpart is MyWebPart)
{
webPartToDelete = webpart;
break;
}
}
if (webPartToDelete != null)
SpWebPartManger.DeleteWebPart(webPartToDelete);
SpWebPartManger.Dispose();
}
}
{
using (SPWeb web = site.OpenWeb(AbsolutePageUrl))
{
SPLimitedWebPartManager SpWebPartManger = web.GetLimitedWebPartManager(AbsolutePageUrl,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
WebPart webPartToDelete = null;
// let's locate the webpart first.... foreach (WebPart webpart in SpWebPartMaanger.WebParts)
{
if (webpart is MyWebPart)
{
webPartToDelete = webpart;
break;
}
}
if (webPartToDelete != null)
SpWebPartManger.DeleteWebPart(webPartToDelete);
SpWebPartManger.Dispose();
}
}
No comments:
Post a Comment