Accessing webparts in a page using the SP Object Model
Posted by PANVEGA on September 8, 2009
The SPLimitedWebPartManager provides a limited set of Web Part operations that can be performed in the SP object model. It can be accessed by adding the microsoft.sharepoint.dll rference to your class.
In a recent project I had to can the view of some ListViewWebparts in a normal SP Page. The best thing therefore is using the SPLimitedWebPartManager.
Any SharePoint page that includes Web Parts must have an instance of the SPWebPartManager on the page to manage Web Parts. This object tracks which zones each Web Part is in, connection information related to Web Parts, personalization, and page edit mode.”
SPLimitedWebPartManager
The SPLimitedWebPartManager “Provides a limited set of Web Part operations that can be performed in our object model scenarios when there is no HttpContext and no Page object is instantiated.”
So the difference is that the SPLimitedWebPartManager has a limited set of functionality which can be used without a HttpContext.
Here is a code example:
SPViewContext viewContext = SPContext.Current.ViewContext;
SPListItem listItem = SPContext.Current.ListItem;
SPFile file = null;
using( SPLimitedWebPartManager webPartManager webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
using (SPWeb wpWeb = webPartManager.Web)
{
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webPartManager.WebParts)
{
if (Equals(wp.GetType().Name, “ListViewWebPart”))
{
ListViewWebPart listViewWebpart = (ListViewWebPart)wp;
…….
}
}
webPartManager.Web.Dispose();
}
Using the WebPartMAnager you can change, add, edit or delete all kind of webparts while using the manager.
Since SPLimitedWebPartManager implements the IDisposable pattern I am being a good citizen and wrapping its instantiation in a using {} block. The problem with this code segment is that this is what it does to the process memory. The solution is adding a dispose() explicit.
......
wpWeb.AllowUnsafeUpdates = true;
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
webPartManager.Web.GetFile(file.UniqueId).CheckOut();
}
for (int i = 0; i < listViewWebpartList.Count; i++)
{
webPartManager.DeleteWebPart(removeWebpartList[i]);
webPartManager.AddWebPart(listViewWebpartList[i], listViewWebpartList[i].ZoneID, listViewWebpartList[i].ZoneIndex);
}
listItem.Web.AllowUnsafeUpdates = true;
listItem.UpdateOverwriteVersion();
listItem.SystemUpdate();
webPartManager.Web.Update();
webPartManager.Web.GetFile(file.UniqueId).Update();
webPartManager.Web.GetFile(file.UniqueId).CheckIn(“Changed View”, SPCheckinType.MinorCheckIn);
wpWeb.AllowUnsafeUpdates = false;
Problem:
When modifying Web Part properties programmatically and trying to save changes, update, publish or approve, an exception is thrown saying that “Security validation for this page is invalid.” You have also set the AllowUnsafeUpdates to true on the current web.
Solution:
Set AllowUnsafeUpdates on the SPLimitedWebPartManager’s Web object like this:
wm.Web.AllowUnsafeUpdates = true;
wm.SaveChanges(wp);
wm.Web.AllowUnsafeUpdates = false;
Problem2:
When approving a SPFile after publishing, you have to use the following code in order to avoid permission exception:
PublishingWeb pubSite = null;
if (PublishingWeb.IsPublishingWeb(wpWeb))
{
pubSite = PublishingWeb.GetPublishingWeb(wpWeb);
if (pubSite.PagesList.EnableModeration)
{
file.Approve(“Changed View”);
}
}
Good Luck!
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
webPartManager.Web.GetFile(file.UniqueId).CheckOut();
}for (int i = 0; i < listViewWebpartList.Count; i++)
{
webPartManager.DeleteWebPart(removeWebpartList[i]);
webPartManager.AddWebPart(listViewWebpartList[i], listViewWebpartList[i].ZoneID, listViewWebpartList[i].ZoneIndex);}
listItem.Web.AllowUnsafeUpdates = true;
listItem.UpdateOverwriteVersion();
listItem.SystemUpdate();
webPartManager.Web.Update();
webPartManager.Web.GetFile(file.UniqueId).Update();
webPartManager.Web.GetFile(file.UniqueId).CheckIn(“Changed View”, SPCheckinType.MinorCheckIn);
webPartManager.Web.GetFile(file.UniqueId).Update();
string variable = “pending”;
this.Page.Session["Approval"] = variable;