Scenario:
Adding webpart to a page using webpart file name
Code:
public static void AddWebPartUsingFileName(SPWeb web, string pageName, string dwpFileName, string zoneID)Download :
{
string errorMessage;
using (SPSite site = web.Site)
{
using (SPWeb rootWeb = site.RootWeb)
{
string url = rootWeb.Url + "/_catalogs/wp/" + dwpFileName;
XmlTextReader reader = new XmlTextReader(new StringReader(rootWeb.GetFileAsString(url)));
SPLimitedWebPartManager wpMgr = web.GetLimitedWebPartManager(pageName, PersonalizationScope.Shared);
WebPart webPart = (WebPart)wpMgr.ImportWebPart(reader, out errorMessage);
wpMgr.AddWebPart(webPart, zoneID, 1);
wpMgr.Web.Dispose();
}
}
}
This method is also a part of SPCore now ( http://spcore.codeplex.com )
0 comments:
Post a Comment