Category Archives: PowerShell

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

Sorting content types in the new button on a list

As i mentioned in my last post it’d be useful to be able to sort the content types in a document library’s new button. This builds on the examples in the previous post but it can be run on it’s own.

This script will attempt to set a default content type, if one is specified, but if it isn’t or the one listed can’t be found it’ll default to alphabetical.

Function Sort-ContentTypesInNewButton{

[CmdletBinding()]
Param ( [parameter(Mandatory=$false)][string] $DefaultContentTypeName,
[parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.SPList] $SPList)
BEGIN {
Write-Verbose "Begining Sort-ContentTypesInNewButton"
}
PROCESS { 

$rootFolder = $SPList.RootFolder

#Get content types fromt the button
$contentTypesInPlace = New-Object 'System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]'
$contentTypesInPlace = $rootFolder.UniqueContentTypeOrder

#Has a default content type name been specified?
if ($DefaultContentTypeName)
{
$contentType = $contentTypesInPlace | where { $_.Name -eq $DefaultContentTypeName }
if ($contentType -ne $null)
{
#Add the default content type
$sortedListOfContentTypes = New-Object 'System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]'
$sortedListOfContentTypes += $SPList.ContentTypes[$DefaultContentTypeName]

#Remove the default content type from the list
$contentTypesInPlace = $contentTypesInPlace | where {$_.Name -ne $DefaultContentTypeName}
}
else
{
Write-Error "$DefaultContentTypeName was not found in the list, sorting by Name alone"
}
}
else
{
Write-Verbose "No default content type specified"
}

#sort the remaining content types and add the sorted list
foreach ($contentType in $($contentTypesInPlace | Sort-Object -Property Name ))
{
#Add the content types 
$sortedListOfContentTypes = [Microsoft.SharePoint.SPContentType[]] $sortedListOfContentTypes + $contentType
}

$rootFolder.UniqueContentTypeOrder = [Microsoft.SharePoint.SPContentType[]] $sortedListOfContentTypes

#Update the root folder
$rootFolder.Update()
Write-Verbose "ContentType(s) sorted and added to the new button in list $($SPList.Name)"

}
End{
Write-Verbose "Ending Sort-ContentTypesInNewButton"
}

}