Hi All,
Below example is used to add the web parts Programmatically in a Page.
Below example is used to add the web parts Programmatically in a Page.
private void AddPageWebParts(SPWeb oWeb, string pagePath)
{
try
{
oWeb.ValidateFormDigest();
SPFile _pwHomeWP = oWeb.GetFile(pagePath);
if (_pwHomeWP.Exists)
{
if (_pwHomeWP.CheckOutType == SPFile.SPCheckOutType.None)
_pwHomeWP.CheckOut();
string zone="";
int position=1;
using (SPLimitedWebPartManager webPartMgr = oWeb.GetLimitedWebPartManager(oWeb.Url + "/" + pagePath, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
NameOfSolution.WebpartName.WebpartName _wpObject = new NameOfSolution.WebpartName.Webpa rtName();
if (_wpObject != null)
{
_wpObject.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
_wpObject.Title = "Web Part Title";
_wpObject.ImportErrorMessage = "Import Error Message";
webPartMgr.AddWebPart(_wpObject, zone, position);
webPartMgr.SaveChanges(_wpObject);
}
}
//check in page
_pwHomeWP.CheckIn("CheckedIn");
_pwHomeWP.Publish("publihsed");
}
}
catch (Exception ex)
{
//Exception
}
}
0 comments:
Post a Comment