Wednesday, 21 August 2013

Find the field type enumerator for a Sharepoint field type in Powershell

I have been trying to add a new field to an SPWeb. It sounds simple enough, but it ended up being a little tricky. The problem came from the 'FieldType' column.

After a little research, I found that the easiest way is to use the corresponding Integer from the FieldType enumerator (see here for more details).

I ended up with the following code that returns the corresponding enumerator for the '$fieldType'. For example, '$fieldType = Integer' returns 1.

$int = [Convert]::ToInt32(( [Microsoft.SharePoint.SPFieldType]::GetValues([Microsoft.SharePoint.SPFieldType]) | Where-Object  { $_ -eq $fieldType }))

I can now add fields the easy way.

$web.Fields.Add("FieldName", $int, $CanIBeNullableOrNot)

No comments:

Post a comment