Question

We have custom code to allow users to remove, add or reorder web parts on a page in their personal view. It is a publishing web part page created by a module using templates.

How can we make updates to this page, say to remove a web part? (Apparently, if I set IgnoreIfAlreadyExists=true web parts will get added to the page.) Thus far we have been deleting and recreating the page. This destroys any web part personalizations a user may have done, and that is no longer acceptable as we are now in production.

Is there a way to backup and restore a users' personal view after deleting and recreating a page?

Was it helpful?

Solution

In simple terms doing something like (important here is of course PersonalizationScope.User):

        SPLimitedWebPartManager webPartManager = SPContext.Current.Web.GetLimitedWebPartManager(this.Request.Url.ToString(), PersonalizationScope.User);
        int wpCounter = webPartManager.WebParts.Count; //get counter
        /*either start looping or for a single web part use*/        
WebPart webPart = webPartManager.WebParts[wpId];
         /*do something with the web part*/

But this has 1 important limitation - it only works for Web parts placed in Web part zone. In addition there is a method (actually part of the ASP.NET Web part infrastructure) called SavePersonalizationState via the Personalization property on each Web part, see more here (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.personalizationprovider.savepersonalizationstate(v=vs.90).aspx). I haven't really been in a situation to try myself!

The biggest challenge would be maybe to collect personalization data for all users. Maybe the fact that it uses the Provider pattern could help, by using the FindState method (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.personalizationprovider.findstate(v=vs.90).aspx) to extract all data.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top