Provisioning Web Parts via the AllUsersWebParts element

Right, so you want to have a Page Layout that contains some web parts when a user creates a page using that layout. Cool, no problem, use an <AllUsersWebParts> element just like Andrew Connell blogs about problem is, if you activate that feature multiple time you’ll get an instance of the web parts in the provisioned page for each activation.

Now, Waldek suggests some approaches to use for provisioning web part instances and the one that fits is going to depend on your needs.

But what happens if you’ve gone down the AllUsersWebParts path and are now running into problems, as I was today? Well, with a quick bit of coding I whipped up a command line tool to remove all the web parts form a page layout. If you do this then re-activate the feature that provisions the page layouts (and their web parts) you will get back to a state of one instance of each desired web part.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;

namespace RemoveWebPartsFromPageLayout
{
    class Program
    {
        static void Main(string[] args)
        {
            if(2 != args.Length)
            {
                Console.WriteLine("usage: RemoveWebPartsFromPageLayout <SPSiteUrl> <PageLayoutTitle>");
                return;
            }
            using(SPSite site = new SPSite(args[0]) )
            {
                using (SPWeb web = site.RootWeb)
                {
                    if (PublishingWeb.IsPublishingWeb(web))
                    {
                        PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
                        foreach (var layout in pubWeb.GetAvailablePageLayouts())
                        {
                            if (args[1] == layout.Title)
                            {
                                DeleteWebParts(layout);
                            }
                        }
                    }
                }
            }
        }

        private static void DeleteWebParts(PageLayout layout)
        {
            SPFile file = layout.ListItem.File;
            if (file.CheckOutType == SPFile.SPCheckOutType.None)
            {
                file.CheckOut();
            }
            using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                List<WebPart> deletes = (from object webPart in wpm.WebParts select webPart as WebPart).ToList();
                foreach (var webPart in deletes)
                {
                    wpm.DeleteWebPart(webPart);
                }
                file.CheckIn("WebParts Deleted");
                file.Publish("WebParts Deleted");
                file.Approve("WebParts Deleted");
            }
        }
    }
}
Advertisements
This entry was posted in Development, SharePoint. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s