Use the below code 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"
}
$site = Get-SPSite http://WebApplication:777
$web = $site.RootWeb
$fieldXML = '<Field Type="Text"
ID="{20152dbc-7793-46cb-bc0d-5dd98b0aa1a4}" StaticName="Col1"
Name=" Col1" DisplayName=" Col1"
Description="A document classification ."
Required="TRUE" MaxLength="255"
Group="RDM Site Columns"
></Field>'
write-host -foreground "green" "Adding Site
Column"
$web.Fields.AddFieldAsXml($fieldXML)
$fieldXML =
'<Field Type="Note"
ID="{2808ba7f-78a9-4930-8840-385a18e85b53}" StaticName="Col2"
Name=" Col2" DisplayName=" Col2"
Description="The type of business conducted at a site."
Required="FALSE"
NumLines="6" RichText="TRUE"
Group="RDM Site Columns"
></Field>'
write-host -foreground "green" "Adding Site
Column"
$web.Fields.AddFieldAsXml($fieldXML)
$fieldXML = '<Field Type="Choice"
ID="{ce7f74d7-fc8c-4844-85a9-903e208e68d2}" StaticName="Col3"
Name=" Col3" DisplayName=" Col3"
Description="The status of a complete location."
Required="TRUE" Group="EICM Site Columns" >
<CHOICES>
<CHOICE>Active</CHOICE>
<CHOICE>InActive</CHOICE>
<CHOICE>Pending</CHOICE>
</CHOICES>
<Default>Pending</Default>
</Field>'
write-host -foreground "green" "Adding Site
Column"
$web.Fields.AddFieldAsXml($fieldXML)
Write-Host -foreground "green" "Creating
Content Type-CT1 ...."
$ctypeName = “CT1”
$ctypeParent =
$web.availablecontenttypes["Document"]
$ctype = new-object
Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)
$ctype.Group = "CT"
$ctype.Description = ""
$web.contenttypes.add($ctype)
$field = $web.fields.getfield(“Col1”)
$fieldLink = new-object
Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$field = $web.fields.getfield(“Col2”)
$fieldLink = new-object
Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$field = $web.fields.getfield(“Col3”)
$fieldLink = new-object
Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.update()
If u need to delete any field in child CT which got inherted
from Parent CT then you can use the below code
Write-Host -foreground "green" "Creating
Content Type-CT2 ...."
$ctypeName = “CT2”
$ctypeParent = $web.availablecontenttypes["CT1"]
$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent,
$web.contenttypes, $ctypeName)
$ctype.Group = "EICM Content Types"
$ctype.Description = ""
$web.contenttypes.add($ctype)
$field = $web.fields.getfield(“col1”)
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.Delete($fieldLink.Id)
$ctype.update()
Great Thilosh..
ReplyDelete