PowerShell - Create SharePoint 2010 Content Type with Site columns
#*===========================================================================
#* Purpose: To create new Content Type with its corresponding fields.
#*
#*===========================================================================
$url = Read-Host "Enter Site URL?"
Write-Host "Processing.... " -ForegroundColor Yellow
#This fucntion is used to create Site Columns with any field type listed within it.
Function Set-CustomField
{
param([string] $FieldName,[string] $FieldType,$web,$contentType,$choices)
switch ($FieldType)
{
"Text" {$web.fields.add($FieldName, ([Type]“Microsoft.SharePoint.SPFieldType”)::Text, $false)}
"Choice" {$web.fields.add($FieldName, ([Type]“Microsoft.SharePoint.SPFieldType”)::Choice, $false, $false, $choices)}
default {$web.fields.add($FieldName, ([Type]“Microsoft.SharePoint.SPFieldType”)::Text, $false)}
}
$field = $web.fields.getfield($FieldName)
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
#Add new fields/Site Columns to corresponding content type
$contentType.fieldlinks.add($fieldLink)
#Update content type with new fields..
$contentType.Update()
}
$site = get-spsite $url
$web = $site.openweb()
$ctypeName = “Records Management” #Content Type Name
$ctypeParent = $web.availablecontenttypes["Document"]
$contentType = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)
$contentType.Group = “Client Content Type” # Content Type Group Name
$contentType.Description = “Used to capture metadata for client documents.”
#Add new content type to corresponding web.
$web.contenttypes.add($contentType)
#Set-CustomField "myField" "Text" $web $contentType $null
$choices = New-Object System.Collections.Specialized.StringCollection
$choices.Add("Agreements")
$choices.Add("Business Operations")
$choices.Add("Compensation")
$choices.Add("Complaints")
$choices.Add("Compliance")
$choices.Add("Corporate Governance")
$choices.Add("Customer Service")
$choices.Add("Financial")
$choices.Add("Personnel")
$choices.Add("Product Development")
$choices.Add("Sales Materials")
$choices.Add("Training")
Set-CustomField "Record Category" "Choice" $web $contentType $choices #Creating Site column with choice field type...
$choices=$null
$choices = New-Object System.Collections.Specialized.StringCollection
$choices.Add("A - 2 years")
$choices.Add("B - 3 years")
$choices.Add("C - 4 years")
$choices.Add("D - 5 years")
$choices.Add("E - 6 years")
$choices.Add("F - 8 years")
$choices.Add("G - 10 years")
$choices.Add("H - 20 years")
$choices.Add("I - Permanent")
Set-CustomField "Retention Category" "Choice" $web $contentType $choices #Creating Site column with choice field type...
$choices=$null
$choices = New-Object System.Collections.Specialized.StringCollection
$choices.Add("Physical")
$choices.Add("Electronic")
Set-CustomField "Secondary Medium" "Choice" $web $contentType $choices
$choices=$null
Set-CustomField "Secondary Location" "Text" $web $contentType $null #Creating Site column with Text field type...
Write-Host "Process Completed...!!!" -ForegroundColor Yellow
#Dispose objects
$web.Dispose()
$site.Dispose()
1) Enter Site Url where content type has to get created.
Note: In above script only custom columns of type Text and Choice has been created.
No comments:
Post a Comment