Replace SharePoint web parts instances in the entire site collection with newer web parts instances

I recently had the task of automating the replacement of all instances of one web part with another new web part throughout all site collections in SharePoint. There may have been thousands of instances of the web part in hundreds of thousands of site collections spread over more than 100 content databases, and all needed to be replaced with the new web part in exactly the same places where the original web parts were placed.

Here I will describe the approach used to replace the web part

Download the Web Part ID Generator from here.

  1. Go to the existing site collection and navigate to http://server/_layouts/newdwp.aspx
  2. Get the type name and the assembly name for the web part from that page.
  3. Copy and paste the assembly name and the type name in the webpart id generator and click on calculate id

  4. Copy the web part
  5. Go to the sql server and expand the content database.
  6. Paste this query and execute

 select distinct     d.SiteId,D.WebId, W.FullURL as WebURL, D.Id As DocumentId, D.DirName, D.LeafName, tp_ID As WebPartSK,w.Title as SiteTitle

FROM       Docs D WITH (nolock)

INNER JOIN Webs W WITH (nolock) ON D.WebID = W.Id

INNER JOIN 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_WebPartTypeId=’b4bd2bdf-cf0c-ffce-ecb1-ae7c4882e17a’


8.The result window will show you the info about the pages which are used and the site title.
9.Once you make sure you are updating all the required pages.
10.Deploy the new web part on the system.
11.Navigate to http://server/_layouts/newdwp.aspx and get the new type name and assembly.
12.Generate the Web part id using the ID generator tool.

Go back to sql and execute the below code

DECLARE @OldWebPartTypeId uniqueidentifier

DECLARE @NewWebPartTypeId uniqueidentifier



UPDATE [WebParts]

SET [tp_WebPartTypeId] = @NewWebPartTypeId

WHERE [tp_WebPartTypeId] = @OldWebPartTypeId

13.Make sure you run the above steps 6 to 11 in all the content databases.


Code for Webpart ID generator is pretty simple, and here it is



using System.Security.Cryptography;

using System.Threading;
string data = tbAssembly.Text;
data += “|” + tbTypeName.Text;
byte[] dataBytes = Encoding.Unicode.GetBytes(data);
MD5 hashAlgo = new MD5CryptoServiceProvider();
byte[] hash = hashAlgo.ComputeHash(dataBytes);
Guid hashGuid = new Guid(hash);
tbResult.Text = hashGuid.ToString();

Leave a Reply

Your email address will not be published. Required fields are marked *