SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a troublesome webpart which I would like to remove from all the places it is being used. (A large number of personalized site pages).

How can I do this in SharePoint 2007?

share|improve this question
up vote 0 down vote accepted

First of all, I recommend that you fix the web part! Modifying lots of pages programmatically can take a long time and needs to be tested.

Here's some code to begin with. Apologies for nestings etc...

using(var site = new SPSite(""))
using(var web = site.OpenWeb())
    var user = web.SiteUsers[@"domain\dave"];    //User with a personalised page

    //Open site with user token
    using(var userSite = new SPSite("", user.UserToken))  
    using(var userWeb = userSite.OpenWeb())
        var page = userweb.GetFile("");

        using(var webpartManager = page.GetLimitedWebPartManager(PersonalizationScope.User))
             var webpartsToDelete = new List<WebPart>();

             foreach(var webpart in webpartManager.WebParts)
                 if(webpart is My.Dodgy.WebPart)   //Replace with your web part type

             foreach(var webpart in webpartsToDelete)

You'll have to iterate over each web application, site, web, user and page... not a trivial amount of time.

To reduce this effort, you could make a "Self Service Fix" page for users to paste in the URLs of broken pages. (I would imagine that the majority of pages on your server won't be personalized or have the web part on it).

And test it first!!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.