Question

I need to create two list fields by powershell.
One that accept only Integer (type Number with no decimal) and the other User (multiple selection).

I can create Number field and Single User field with this code:

$spFieldType = [Microsoft.SharePoint.SPFieldType]::User
$myCustomList.Fields.Add("myUsers",$spFieldType,$false) 

$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$myCustomList.Fields.Add("myNumber",$spFieldType,$false)

How can I add Integer and multiple users fields?

Was it helpful?

Solution

For number field accepting only Integers (no decimal), do following:

$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$myField = $myCustomList.Fields.Add("myInteger",$spFieldType,$false)
$myIntegerField = [Microsoft.SharePoint.SPFieldNumber]$myCustomList.Fields[$myField]
$myCustomList.Fields[$myIntegerField].DisplayFormat = SPNumberFormatTypes.NoDecimal
$myCustomList.Fields[$myIntegerField].Update()

User multi-selection

$spFieldType = [Microsoft.SharePoint.SPFieldType]::User
$myField = $myCustomList.Fields.Add("myMultiUsers",$spFieldType,$false)
$myMultiUsers = [Microsoft.SharePoint.SPFieldUser]$myCustomList.Fields[$myField]
$myCustomList.Fields[$myMultiUsers].AllowMultipleValues = true
$myCustomList.Fields[$myMultiUsers].Update()

This should work !

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top