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

$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number

How can I add Integer and multiple users fields?


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

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

This should work !

| improve this answer | |
  • The user is fine, but the Integer display only nodecimal in the view. (I want to avoid to insert new items with .00) – Nk SP Apr 30 '14 at 8:21
  • Even if the user adds .00 it will trim it and only show integer.. If you want client side validation on the control, you might need to inject JavaScript on the page.. – Arsalan Adam Khatri Apr 30 '14 at 8:58
  • I tried to add a validation formula "=MOD(['NameField'],1)<>0" but no luck :/ – Nk SP Apr 30 '14 at 10:00
  • No this is client side validation and AFAIK you cannot have that until you restrict the textbox using some JavaScript.. – Arsalan Adam Khatri Apr 30 '14 at 10:36
  • $myCustomList.Fields[$myIntegerField].ValidationFormula is fine, now I need the MOD formula – Nk SP Apr 30 '14 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.