PowerShell Script: Enable and Add Content Type in Document Library

Scenario:

We created content types using power shell script. Now, we need to enable content types and add these content types in the list.

Resolution:

$site = Get-SPSite “url of site collection”

$rootWeb $site.RootWeb

$docLib $rootWeb.Lists[“Shared Documents”]

$customContentTypes “ContentType1,ContentType2,ContentType3”

$docLib.ContentTypesEnabled $true

$docLib.update()

$customContentTypeArray $customContentTypes.Split(“,”)

foreach($customContentType in $customContentTypeArray)

{

    $customCT $rootWeb.ContentTypes[$customContentType]

    $docLib.ContentTypes.Add($customCT)

}

$docLib.update()

Advertisements

3 Comments »

  1. Good post Farhan.
    One line 01 of code snippet, you don’t need to put brackets ( ).

    • Farhan Faiz said

      @ Abdur Raheem: Thanks. Corrected

  2. Anonymous said

    Great post thanks

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: