ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Wednesday, March 14, 2012

How to Create Fields to Sharepoint List by Windows PowerShell ISE


Below is list data type of fields

TypeName
--------
Counter
Text
Note
Choice
MultiChoice
GridChoice
Integer
Number
ModStat
Currency
DateTime
Lookup
Boolean
Threading
ThreadIndex
Guid
Computed
File
Attachments
User
URL
Calculated
Recurrence
CrossProjectLink
ContentTypeId
MultiColumn
LookupMulti
UserMulti
WorkflowStatus
AllDayEvent
WorkflowEventType
Create new list for lookup with name is Departments then add new item as follows
Create new custom list to create new field for this custom list
Copy this segment code and paste to here

$SPSite = New-Object Microsoft.SharePoint.SPSite("http://quochung-axioo:90");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Open Your List
$List = $OpenWeb.Lists["MyCustomList"];
#Add User Field to list
$List.Fields.Add("UserField", "User", $User)
#Add TextField to list
$List.Fields.Add("TextField", "Text", $Text)
#Add UrlHyperField Field to list
$List.Fields.Add("UrlHyperField","URL",$false)
$List.Fields[“UrlHyperField”].Description = “My UrlHyperField Field”
#Add DateTime Field to list
$List.Fields.Add("DateTimeField","DateTime",$false)
$List.Fields[“DateTimeField”].Description = “My DateTime Field”
#Define Choice Field
$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("First Choice")
$Choices.Add("Second Choice")
$Choices.Add("Third Choice")
#Add Choice Field to list
$List.Fields.Add("ChoiceField",       [Microsoft.SharePoint.SPFieldType]::Choice,     $FALSE,          $FALSE,    $Choices)
#Get List Item from list Lookup (Departments)
$LookupList = $OpenWeb.Lists["Departments"]
$LookupListID = $LookupList.ID
#Add Field Lookup to list
$List.Fields.AddLookup("LookupField",$LookupListID,$FALSE)
#Update view
$Views = $List.Views["All Items"]
$Views.ViewFields.Add("UserField")
$Views.ViewFields.Add("TextField")
$Views.ViewFields.Add("UrlHyperField")
$Views.ViewFields.Add("DateTimeField")
$Views.ViewFields.Add("ChoiceField")
$Views.ViewFields.Add(“LookupField”)
$Views.Update()
$OpenWeb.Dispose();
$SPSite.Dispose()
Run script Completed
Open your list and see result as follows
Try Add new item and see result as follows:

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