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"
    $docLib.update()
    $customContentTypeArray = $customContentTypes.Split(",")
    foreach($customContentType in $customContentTypeArray)
       {
         $customCT = $web.ContentTypes[$customContentType]
         $docLib.ContentTypes.Add($customCT)
         Write-Host -foreground "green" "Content Type -"$customContentType "Added to the List"
       }
    $docLib.update()

}



$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
$web.Lists.Add($libraryName,$libraryDescription,$libraryTemplate);
$web.Update();

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



$web.Dispose()
$site.Dispose()

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



To create site column and Content type use the Link
 

No comments:

Post a Comment