Create A Document Library Based on a Content Type

Something that’s been winding me up for quite a while is the time needed to create a library, enable content types, add a content type to it, remove the OOB ‘Document’ Type and then finally create a new view that includes the fields on the content type.

It’s not much in the grand scheme of things. Probably about 5 minutes a time. But if you’re creating one, you’ll probably be creating another, and another, and another.

So, let’s do something about that, this script should do everything in the description above. Plus let’s throw in a flag that’ll let us create a list at the same time instead of just a Document Library.

Function Create-DocumentLibraryBasedOnContentType
{

[CmdletBinding()]
Param ([parameter(Mandatory=$true)][string] $ContentTypeName,
       [parameter(Mandatory=$true)][string] $documentLibraryName,
       [parameter(Mandatory=$true)][string] $SPWebURL,
       [parameter(Mandatory=$false)][switch] $IsAList,
       [parameter(Mandatory=$false)][switch] $ShowOnQuickLaunch)
    BEGIN   
        {
            
            Write-Verbose "Beginning Create-DocumentLibraryBasedOnContentType"
            
            $SPWeb = Get-SPWeb $SPWebURL
            $SPSite = $SPWeb.Site
            if ($SPWeb.Lists[$documentLibraryName] -ne $null)
            {
                Write-Error "Library already exists. Aborting" -ErrorAction Stop
            }
            #The default behaviour is to create a document library
            if ($IsAList)
            {
                $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
                $listOrLibrary = "List"
            }
            else
            {
                $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
                $listOrLibrary = "Document Library"
            }
        }
    PROCESS
        {
            #get the content Type
            Write-Verbose "Getting content type $contentTypeName in site $siteURL"

            #Confirm that the content type exists.
            $contentType = $SPSite.RootWeb.ContentTypes | where {$_.Name -eq $contentTypeName}

            ##Abort if not found.
            if ($contentType -eq $null)
            {
                Write-Error "$ContentTypeName not found in site collection. Aborting" -ErrorAction Stop
            }
            else
            {
                #Create a library
                Write-Verbose "Creating Default $list"
                
                
                #Create the document Library, stripping out the spaces in the URL for cleanliness
                $libGuid =   $SPWeb.Lists.Add($documentLibraryName.Replace(' ',''),'',$listTemplate) 
                $docLib = $SPWeb.Lists[$libGUID]
                
                Write-Verbose "$listOrLibrary Created, updating settings"
                #Correct the title to appear without spaces
                $docLib.Title = $documentLibraryName
              
                #Add to the quick launch if relevant.
                if($ShowOnQuickLaunch)
                {
                    Write-Verbose "Adding to quick launch"
                    $docLib.OnQuickLaunch = $true
                }
                
                ##Allow modification of content types
                $docLib.ContentTypesEnabled = $true
                
                ##add content type
                 Write-Verbose "Adding the content type"
                $docLibInSitu = $doclib.ContentTypes.Add($contentType)
                
                ##remove 'document'
                if ($IsAList)
                {
                    $doclib.ContentTypes["Item"].Delete()
                }
                else
                {
                    $doclib.ContentTypes["Document"].Delete()
                }
                ##create view and set to default
                Write-Verbose "Creating Default View"
                $newView = $docLib.Views[0].clone("Default", 30, $true, $true)
                $newView.Update()
            }
        }
    END {
            $docLib.Update()
            $SPSite.Dispose()
            $SPWeb.Dispose()
            Write-Verbose "Ending Create-DocumentLibraryBasedOnContentType" 
        }
}

And the script to run it:

$contentTypeName = "AlexB_Document1b"
$siteURL = "http://SharePoint/sites/Subscriber/"
$docLibName = "Test DocLib"

Create-DocumentLibraryBasedOnContentType -ContentTypeName $contentTypeName -documentLibraryName $docLibName -SPWebURL $siteURL -Verbose -List -ShowOnQuickLaunch

Leave a Reply

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