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.
- Go to the existing site collection and navigate to http://server/_layouts/newdwp.aspx
- Get the type name and the assembly name for the web part from that page.
- Copy and paste the assembly name and the type name in the webpart id generator and click on calculate id
- Copy the web part id.
- Go to the sql server and expand the content database.
- 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
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
SET @OldWebPartTypeId = ‘AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA’
SET @NewWebPartTypeId = ‘BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB’
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
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();