Sharepoint find where webpart is in use
Asked Answered
C

5

5

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

Chinaware answered 30/9, 2009 at 14:18 Comment(0)
D
5

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.

Drucie answered 1/10, 2009 at 8:26 Comment(0)
E
5

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 WP WITH (nolock) 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.

Eponymy answered 1/10, 2009 at 10:27 Comment(4)
@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!Drucie
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.Eponymy
Having said that the problems that would be caused by reading inconsistent data in this example are probably fairly small.Eponymy
In my (admittedly limited) experience, plain ol' reads don't typically cause deadlocks -- what does is if you perform a write afterwards, in which case SQL may try to escalate the read lock to a write lock when another write lock is already pending, waiting for release of the read lock.Maltzman
E
3

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/

Eponymy answered 1/10, 2009 at 10:40 Comment(1)
and it is open source I believe so it might be a good place to start looking for some example codeDreadnought
B
2

And in case anyone is looking for the same query for SharePoint 2013. Here it is. The view has been removed in 2013 and the table name that has the same fields is dbo.AllWebParts.

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.AllWebParts WP WITH (nolock) 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>'
Barcellona answered 6/1, 2015 at 18:8 Comment(0)
S
1

I've had no success generating the web part type id by any method, I simply get no results when I execute the query. This was because the id was never correct.

To get around this issue, I created a new page and inserted an instance of the web part I was interested in. I then query by LeafName, giving it the page I'd created (Best to make it unique!). This returned a single result and I could then use the tp_WebPartTypeId it returned to query the entire content database.

So if you're having trouble generating the id, try this approach instead.

Slothful answered 26/10, 2011 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.