- Using Microsoft.Sharepoint;
- Using System.Web.UI.WebControls.WebParts;
-
-
- public static string AddWebPartToPage(
SPWeb web,
string pageUrl,
string webPartName,
string zoneID,
int zoneIndex)
{
using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(
pageUrl, PersonalizationScope.Shared))
{
using (WebPart webPart =
CreateWebPart(web, webPartName, webPartManager))
{
webPartManager.AddWebPart(webPart, zoneID, zoneIndex);
return webPart.ID;
}
}
}
public static WebPart CreateWebPart(SPWeb web, string webPartName, SPLimitedWebPartManager webPartManager)
{
SPQuery qry = new SPQuery();
qry.Query = String.Format(CultureInfo.CurrentCulture,
"<WHERE><EQ><FIELDREF name="FileLeafRef"><VALUE type="File">{0}</VALUE></FIELDREF></EQ></WHERE>",
webPartName);
SPList webPartGallery = null;
if (null == web.ParentWeb)
{
webPartGallery = web.GetCatalog(
SPListTemplateType.WebPartCatalog);
}
else
{
webPartGallery = web.Site.RootWeb.GetCatalog(
SPListTemplateType.WebPartCatalog);
}
SPListItemCollection webParts = webPartGallery.GetItems(qry);
XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());
string errorMsg;
WebPart webPart = webPartManager.ImportWebPart(xmlReader, out errorMsg);
return webPart;
}
Using Microsoft.Sharepoint;
Using System.Web.UI.WebControls.WebParts;
public static string AddWebPartToPage(
SPWeb web,
string pageUrl,
string webPartName,
string zoneID,
int zoneIndex)
{
using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(
pageUrl, PersonalizationScope.Shared))
{
using (WebPart webPart =
CreateWebPart(web, webPartName, webPartManager))
{
webPartManager.AddWebPart(webPart, zoneID, zoneIndex);
return webPart.ID;
}
}
}
public static WebPart CreateWebPart(SPWeb web, string webPartName, SPLimitedWebPartManager webPartManager)
{
SPQuery qry = new SPQuery();
qry.Query = String.Format(CultureInfo.CurrentCulture,
"{0}",
webPartName);
SPList webPartGallery = null;
if (null == web.ParentWeb)
{
webPartGallery = web.GetCatalog(
SPListTemplateType.WebPartCatalog);
}
else
{
webPartGallery = web.Site.RootWeb.GetCatalog(
SPListTemplateType.WebPartCatalog);
}
SPListItemCollection webParts = webPartGallery.GetItems(qry);
XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());
string errorMsg;
WebPart webPart = webPartManager.ImportWebPart(xmlReader, out errorMsg);
return webPart;
}
Example 2: Add Content Editor web part to a page by passing site Url and page relative URL:-
- public static string AddWebPartToPage(
- string siteURL,
string pageRelativeUrl,
)
{
using (SPSite site = new SPSite(siteURL))
{
SPWeb web = site.RootWeb;
SPFile page = web.GetFile(pageRelativeUrl);
page.CheckOut();
using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
XmlElement p = new XmlDocument().CreateElement("p");
p.InnerText = "Hello World";
ContentEditorWebPart cewp = new ContentEditorWebPart();
cewp.Content = p;
wpmgr.AddWebPart(cewp, "Header", 0);
}
page.CheckIn(String.Empty);
}
}
No comments:
Post a Comment