,

SharePoint 2010 PowerShell Script Series Part 1 – Hidden Content Types

Posted by

I’m excited to kick this series off because I really think PowerShell rocks. The goal of this series is to provide some automation for SharePoint Administrators to perform various deployment and/or troubleshooting tasks. To use the script simply copy it to notepad and save it with a ps1 extension. To run the script on your SharePoint 2010 environment, simply launch PowerShell and get to the directory where the script is located. To run the script type:

.\scriptname.ps1

The first script was created based on a blog I previously authored here. Please review that blog for more information. The following script provides the following functionality:

 

Selecting Option 1:

Creates a Content type derived from document and puts it in a special _hidden group at the site level. It also adds the hidden content type to a document library

Selecting Option 2:

If the hidden content type was created previously, choosing option 2 will add the hidden content type

Note: You will need to keep track of the names of the hidden content type by name after they are created.

 

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

Start-SPAssignment -Global
$siteurl = read-host “Enter the name of your site and press enter”
$site = New-Object Microsoft.SharePoint.SPSite($siteurl)
[Microsoft.SharePoint.SPWeb]$web = $site.OpenWeb()

Write-Host “Press 1 to create a hidden Content Type and add it to a Document Library”
Write-Host “Press 2 to add a previously created hidden Content type to a Document Library”

[int]$result = read-host

if($result -eq 1)
{
       [Microsoft.SharePoint.SPContentTypeCollection]$cts = $web.ContentTypes
       $parent = $web.ContentTypes | where {$_.id -eq “0x0101”}

       $ctName = Read-Host “Enter the name of your Content Type”
      
       #Creating ContentType and adding to the site collection
       $ct = New-Object Microsoft.SharePoint.SPContentType($parent, $cts, $ctName)
       $cts.Add($ct)
      
       #Putting the Content Type in the hidden Group
       $Ct.Group = “_Hidden”
       $ct.Update()
      
       #Adding Content Type to Document library
       Write-host “Adding Hidden Content Type to Document Library”
      
       $doclib = Read-host “Type the name of the Document Library you want to add it to and press Enter key.”
       [Microsoft.SharePoint.SPList]$list = $web.Lists[$doclib];
      
       [Microsoft.SharePoint.SPDocumentLibrary]$oDocumentLibrary = $list;
      
       $oDocumentLibrary.ContentTypesEnabled = “true”
       
       write-host “Adding new hidden Content Type to Document Library”
       $oDocumentLibrary.ContentTypes.Add($ct)
       $list.Update()
      
       write-host(“Operation Completed Successfully”)

}

elseif($result -eq 2)
{
       $hiddenct = Read-Host “Enter the name of the Hidden Content Type and press Enter”
       $doclib = Read-host “Enter the name of the Document Library you want to add it to and press Enter”
      
       #Referencing DocLib
       [Microsoft.SharePoint.SPList]$list2 = $web.Lists[$doclib]
       [Microsoft.SharePoint.SPDocumentLibrary]$oDocumentLibrary = $list2;
       $oDocumentLibrary.ContentTypesEnabled = “true”
      
       #Pull the ContentType from the Hidden Group and Add it to the DocLib
       $cthid = $web.AvailableContentTypes[$hiddenct]
      
       #Add hidden Content Type to DocLibrary
       write-host “Adding hidden Content Type to Document Library”
       $oDocumentLibrary.ContentTypes.Add($cthid);
        $list2.Update();
       write-host “Operation Completed Successfully”
}

else {write-host “Run the script again and choose option 1 or 2”}

#Disposing Objects now
Stop-SPAssignment -Global

 

Thanks!

-Russmax   Alien