views:

349

answers:

3

Is it possible to analyse on which pages a webpart is in use?

I want to remove some webparts from my sharepoint server, but i don't know what's in use and what's not in use?

Is there a solution for it?

Im current using moss2007

+2  A: 

Each web part added to a page is stored in the page itself. There's no central store that records where each web part is used.

This means unfortunately you need to iterate through every web part page in the site to retrieve this information. On each page you should then check the assembly names of the web parts. Add all the web parts you find to a list that is output once the application's completed.

The SPWeb object allows you to retrieve the web part manager for each page with SPWeb.GetLimitedWebPartManager(). Use the SPLimitedWebPartManager.WebParts property to retrieve a collection of web parts.

Alex Angas
A: 

Alex's answer (iterating through sites -> pages > web parts) is good and the 'correct' way to do this but is fairly expensive on a very large site.

An alternative is a database query. All the usual caveats apply about accessing the database directly : do not change anything, could break at any time with service packs etc etc but assuming we are all big boys here :-

First you need to find out the WebPartTypeId of the web part your interested in

Next run this on ALL content databases.

SELECT DISTINCT D.SiteID, D.WebId, W.FullURL as WebURL, D.Id As DocumentId,
                D.DirName, D.LeafName, tp_ID As WebPartSK
FROM       dbo.Docs D WITH (nolock) 
INNER JOIN dbo.Webs W WITH (nolock) ON D.WebID = W.Id
INNER JOIN dbo.WebParts WITH (nolock) WP ON D.Id = WP.tp_PageUrlID
WHERE WP.tp_ListId Is Null AND WP.tp_Type Is Null AND WP.tp_Flags Is Null
      AND WP.tp_BaseViewID Is Null AND WP.tp_DisplayName Is Null 
      AND WP.tp_Version Is Null
AND WP.tp_WebPartTypeId='<your web parts id>'

You could do this the other way around (get a list of all WebPartTypeId's in use) but you can't get the assembly name from the WebPartTypeId hash so you would have to do some sort of lookup list of web parts > typeid's.

Ryan
@Ryan: We don't know @Active_t's background so be careful about assumptions ;-) When reading from the database using NOLOCK is essential to prevent deadlocks and it must be accepted that the results may be inaccurate. Apologies if preaching to the converted!
Alex Angas
I've edited to add the no lock hint but we have to remember that its not a silver bullet and introduces potential problems of its own (data in inconsistent state). Have you any good references on the chance of causing deadlocks? It would be good to get some definitive answers on that one, but not surprisingly the official line is... don't.
Ryan
Having said that the problems that would be caused by reading inconsistent data in this example are probably fairly small.
Ryan
+2  A: 

Another way to do this is use Gary Lapointe STSADM extensions,specifically enumPageWebParts

stsadm -o gl-enumpagewebparts -url "http://intranet/hr/pages/default.aspx" -verbose

It only outputs the web parts in use on a specific page but you could call this for each page on your site or even add a command to iterate over all pages in a site.

http://stsadm.codeplex.com/

Ryan
and it is open source I believe so it might be a good place to start looking for some example code
Colin