Monday, April 2, 2012

Close Shared WebPart Programmatically

Share/Save/Bookmark


These days i have been banging my head between personalized and shared mode of the publishing page.
Well, every publishing page has a shared mode(PageView=Shared) and personalize mode(PageView=Personal). Deleting a webpart from the mode in which it is added is possible but cross deletion is not possible. What i mean is deleting a webpart, added in shared mode from personalized mode .
SharePoint only allows to close such webparts, so how do we that operation programmatic?
Somehow SPLimitedWebPartManager did not work for me in doing this but definitely SPWebPartManager does the trick.

SPWebPartManager spwebPartManager = SPWebPartManager.GetCurrentWebPartManager(this.Page);
SPContext.Current.Web.AllowUnsafeUpdates = true;
               
if (currentWebPart.IsShared == false)
      spwebPartManager.DeleteWebPart(currentWebPart);
else if (currentWebPart.IsShared == true)
      spwebPartManager.CloseWebPart(currentWebPart);


SPContext.Current.Web.AllowUnsafeUpdates = false;


Subscribe

No comments:

Post a Comment