Thursday, March 15, 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:

No comments:

Post a Comment