Thursday, October 9, 2014

Sharepoint create List and attach Content Type to Library or List through powershell

Below code to be used in Powershell file

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell"

function AddContentTypes($web,$libraryName,$libraryDescription,$libraryTemplate,$customContentTypes)
    #Adding CT
    Write-Host -foreground "green" "Adding Content Types to the Library - " $libraryName
    $docLib = $web.Lists[$libraryName]
    $docLib.ContentTypesEnabled = "True"
    $customContentTypeArray = $customContentTypes.Split(",")
    foreach($customContentType in $customContentTypeArray)
         $customCT = $web.ContentTypes[$customContentType]
         Write-Host -foreground "green" "Content Type -"$customContentType "Added to the List"


$site = Get-SPSite "http://Webapplication:777"
$web = $site.RootWeb

$libraryName = "Config"
$libraryDescription = ""
$libraryTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary;
Write-Host -foreground "green" "Creating Library - " $libraryName "...."
# Adding Library

$customContentTypes = "CT1,CT2,CT3"
AddContentTypes $web $libraryName $libraryDescription $libraryTemplate $customContentTypes


Read-host "Press Enter key to exit ...."

To create site column and Content type use the Link

No comments:

Post a Comment