Change content type recursively using PowerShell SharePoint 2013

Change content type recursively using PowerShell SharePoint 2013

Scenario:
My client has created a site collection in sharepoint 2013 with more than 300 sub sites and 500+ document libraries with default content types. Now the business want to stream line the meta data collecting from end user by associating “MSTechnology Content Type” content type to “Finance Documents” library in all the sub sites across site collection. Implementing this change manually is completely a lay man and more cumbersome job. Recently we discussed about: SharePoint 2013 Create Site Column and Content Types Using PowerShell

As we all know that Microsoft had extended PowerShell abilities from SharePoint 2010 onwards to such an extent that we can avoid any amount of manual work by implementing scripts in PowerShell.

SharePoint deveopment training course

Solution:
I have implemented the below script to accomplish the user requirement.
What we should know in PowerShell:
-> Implementing recursive looping
-> Using where conditions

I have manually created a content type called “MSTechnology Metadata” at site collection level and the rest has been taken care by script:

You can use Windows PowerShell ISE or Visual studio code to write and debug the powershell script.



1.$site = Get-SPSite http://sharepoint13:12345

2.$site | Get-SPWeb -Limit all | ForEach-Object {

3.write-host “Checking site:”$_.Title

4.$_.Lists | where { $_.BaseTemplate -eq “DocumentLibrary” –and $_.Title -eq “Finance Documents”} | ForEach-Object {

5.write-host “Checking Document:”$_.Title

6.$_.ContentTypesEnabled = $true

7.$ctToAdd = $site.RootWeb.ContentTypes[“MSTechnology Content Type”]

8.$ct = $_.ContentTypes.Add($ctToAdd)

9.write-host “Content type” $ct.Name “added to library” $_.Title

10.$_.Update()

11.}

12.}

13.$site.Dispose()

Let us walk through the code line by line.

Line 1: $site = Get-SPSite http://sharepoint13:12345
Creating object to site collection.
Line 2: $site | Get-SPWeb -Limit all
In this line we are fetching all the sites in the site collection iteratively with the parameter “-Limit all”.
By specifying this parameter “-Limit all” PowerShell iteratively loop through all the sites in the site collection till the leaf node.

It shows the output as below:




Url

http://sharepoint13:12345
http://sharepoint13:12345/-1
http://sharepoint13:12345/SubSite Level-10-1
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-1-1
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …

Etc will list all the sites till leave node.

Line 4: $_.Lists | where { $_.BaseTemplate -eq “DocumentLibrary” –and $_.Title -eq “Finance Documents”}
Here I am querying the result set to get all the lists whose base template is “Document library” and Title “Finance Documents” to associate “MSTechnology Content Type” content type in the site collection till the leave node.
Line 7: $_.ContentTypesEnabled = $true
This enables allow content types property for the document library. This we can see as part of Advanced properties in library setting.
Line 8: $ctToAdd = $site.RootWeb.ContentTypes[“MSTechnology Content Type”]
Creating object of the content type to be attached to the library
Line 9: $ct = $_.ContentTypes.Add($ctToAdd)
Attaching content type to the library.
Line 15: $site.Dispose()
To avoid memory leaks I am closing the site collection object.

The power of this script is, it updated Content Type of 100 document libraries in 4 munities.

Change content type recursively using PowerShell SharePoint 2013
Change content type recursively using PowerShell SharePoint 2013

Hope this helps, happy automation in PowerShell !!!

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →