Wednesday, March 14, 2012

How to create Content type – Site Column – Add site column exists to content type by Windows PowerShell ISE


Create content types

Open Windows PowerShell ISE  | copy this segment code and paste to here then click Run script

$spWeb = Get-SPWeb http://quochung-axioo:90
$spWeb.AvailableContentTypes | Select Name
$parent = $spWeb.AvailableContentTypes["Document Sets"]
$contentType =  New-Object Microsoft.SharePoint.SPContentType -ArgumentList @($parent, $spWeb.ContentTypes, "Finance")
$contentType.Group = "My Content Type"
$contentType.Description = "Standard Documents"
$spWeb.ContentTypes.Add($contentType)
$spWeb.Update()
$spWeb.Dispose()

Script run Completed
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site content types” link and see result as follows:

Create Site Column

New PS1 file | copy this segment code and paste to here then click Run script
#Assumes that you are creating the site column in the top-level site of a site collection
#Script can also be used to create a site column in the Content Type Hub
#Get site and web object
$site = Get-SPSite -Identity http://quochung-axioo:90
$web = $site.RootWeb

#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="DateTime"
Name="TestDate"
Description="This is a test date column."
DisplayName="Test Date"
StaticName="TestDate"
Group="My Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
#Output XML to console
write-host $fieldXML
#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)

#Example of changing the site column settings after creation
#Configure Test Date column to be Date Only instead of Date & Time
$dtField = $web.Fields["Test Date"]
$dtField.DisplayFormat = "DateOnly"
$dtField.Update()
#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()
Script run Completed
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site columns” link and see result as follows:

Add site column exists to content type

Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site content types” then click to “Finance” content type, you have not seen any content type was added
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site columns” link and see result as follows:
Copy and paste this segment code to here

$site = Get-SPSite -Identity http://quochung-axioo:90
$web = $site.RootWeb

$ct=$web.ContentTypes["Finance"];
$fieldAdd=$web.Fields["AddSiteColumnToContentType"]
$fieldLink=New-Object Microsoft.SharePoint.SPFieldLink($fieldAdd)
$ct.FieldLinks.Add($fieldLink);
$ct.Update()

$web.Dispose()
$site.Dispose()
Run script completed
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site content types” then click to “Finance” content type, you have seen any content type was added

2 comments:

  1. Great work and much appreciated. I was able to use Foreach and Foreach-Object statements to bulk create via a CSV.

    ReplyDelete