Add Fields To List with PowerShell in SharePoint 2010 – Part1

Add Fields To List with PowerShell in SharePoint 2010 – Part1
10 votes, 5.00 avg. rating (99% score)

Introduction

We will move further from creating custom List with powershell to adding various types of fields to sharepoint list.
I hope covering most of the common fields should make our day brighter while working with powershell and we won’t be lost for small tasks


I will start the code again with creating list(but very precise) and then continue further how to add various fields to the list to complete the platter

Adding field of type ‘Single line of text’




$errorlabel=$false

try
{
$TestSiteUrl = "http://mysitecollection/mysite"
$NewWebobj = Get-SPWeb -identity $TestSiteUrl
$newListName = "EmpList"
$newListDescription = "Employee Information"
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
$lstId = $NewWebobj.Lists.Add($ListName,$ListDescription,$ListTemplateType)
write-host "list " $ListName " created successfully" $ListName -ForegroundColor Green

$listUrl = $NewWebobj.ServerRelativeUrl + "/lists/" + $newListName;
$myCustomList = $NewWebobj.GetList($listUrl)

#column of type 'Single line of text'
$firstNameColXml = "<Field Type='Text' DisplayName='FirstName' Required='TRUE' MaxLength='255' 
StaticName='FirstName' Name='FirstName' />"
$myCustomList.Fields.AddFieldAsXml($firstNameColXml,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

$myCustomList.Update()
}
catch
{

write-host "Error" $_.exception
$errorlabel = $true
}
finally
{
if($NewWebobj -ne $null) {$NewWebobj.Dispose()}

if($errorlabel -eq $true){exit 1}
else {exit 0}
}

exit 0

Above code will create sharepoint custom list ‘EmpList’ with column ‘FirstName’ which is of type ‘Single line of text’.

Now we will only concentrate on the following code snippet from the above code


$firstNameColXml = "<Field Type='Text' DisplayName='FirstName' Required='TRUE' MaxLength='255' 
StaticName='FirstName' Name='FirstName' />"
$myCustomList.Fields.AddFieldAsXml($firstNameColXml,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

“$firstNameColXml” is the variable which holds the xml schema string for creating ‘Single line of text’ column “FirstName”
AddFieldAsXml method will create the column in the list and add it to default view.

Following are the different schema xml’s for various fields.

Adding field of type ‘Multiple lines of text’

Plain text field xml

Adds plain text field ‘MultiLineCol1’


$multiLineCol1 = "<Field Type='Note' DisplayName='MultiLineCol1' Required='FALSE' NumLines='6' RichText='FALSE' 
Sortable='FALSE' StaticName='MultiLineCol1' Name='MultiLineCol1' />"

$myCustomList.Fields.AddFieldAsXml($multiLineCol1,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Rich text field xml

Adds rich text field ‘MultiLineCol2’

$multiLineCol2 = "<Field Type='Note' DisplayName='MultiLineCol2' Required='FALSE'   NumLines='6' 
RichText='TRUE' RichTextMode='Compatible' Sortable='FALSE' StaticName='MultiLineCol2' Name='MultiLineCol2' />"
$myCustomList.Fields.AddFieldAsXml($multiLineCol2,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Enhanced rich text field xml

Adds enhanced rich text field ‘MultiLineCol3’

$multiLineCol3 = "<Field Type='Note' DisplayName='MultiLineCol3' Required='FALSE'  NumLines='6' RichText='TRUE' 
RichTextMode='FullHtml' IsolateStyles='TRUE' Sortable='FALSE'  StaticName='MultiLineCol3' Name='MultiLineCol3' />"
$myCustomList.Fields.AddFieldAsXml($multiLineCol3,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Adding field of type ‘Choice’

dropdown field xml

Adds Choice field ‘ChoiceCol1’ with choices in dropdown

$choiceCol1 = "<Field Type='Choice' DisplayName='ChoiceCol1' Required='FALSE' Format='Dropdown' 
FillInChoice='FALSE' StaticName='ChoiceCol1' Name='ChoiceCol1'>
			<Default>MyChoice1</Default>
			<CHOICES>
				<CHOICE>MyChoice1</CHOICE>
				<CHOICE>MyChoice2</CHOICE>
			</CHOICES>
	       </Field>"
	
$myCustomList.Fields.AddFieldAsXml($choiceCol1,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

radio buttons field xml

Adds Choice field ‘ChoiceCol2’ with choices as radio buttons

$choiceCol2 = "<Field Name='ChoiceCol2' Type='Choice' DisplayName='ChoiceCol2' Format='RadioButtons'
		FillInChoice='FALSE' >
			<Default>MyChoice1</Default>
			<CHOICES>
				<CHOICE>MyChoice1</CHOICE>
				<CHOICE>MyChoice2</CHOICE>
			</CHOICES>
		</Field>"
$myCustomList.Fields.AddFieldAsXml($choiceCol2,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

checkboxes(allow multiple selection) field xml

Adds Choice field ‘ChoiceCol3’ with choices as multiple selection allowed checkboxes

$choiceCol3 = "<Field Name='ChoiceCol3' Type='MultiChoice' DisplayName='ChoiceCol3' FillInChoice='FALSE'>
					<Default>MyChoice1</Default>
					<CHOICES>
						<CHOICE>MyChoice1</CHOICE>
						<CHOICE>MyChoice2</CHOICE>
					</CHOICES>
				</Field>"
$myCustomList.Fields.AddFieldAsXml($choiceCol3,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Adding field of type ‘Number’

Adds Number field ‘NumCol1’ which allows numbers only

$numCol1 = "<Field Type='Number' DisplayName='NumCol1' Required='FALSE' Name='NumCol1'/>"
$myCustomList.Fields.AddFieldAsXml($numCol1,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Adding field of type ‘Date and Time’

DateOnly field xml

Adds Data and Time field ‘DateTimeCol1’ which displays only date

$fldXml = "<Field Type='DateTime' DisplayName='DataTimeCol1' Format='DateOnly'   Name='DataTimeCol1'/>" 
$myCustomList.Fields.AddFieldAsXml($fldXml,$true,
[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Dateandtime field xml

Adds Data and Time field ‘DateTimeCol2’ which displays both date and time

$fldXml = "<Field Type='DateTime' DisplayName='DataTimeCol2' Format='DateTime'   Name='DataTimeCol1'/>"
$myCustomList.Fields.AddFieldAsXml($fldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

Conclusion

I will continue other field xmls in continuation to this post. Hope this is useful when you are working on the task of creating fields in SharePoint 2010 lists with powershell2.0

March 28, 2012 · Adi · 9 Comments
Tags: , , , , ,  · Posted in: Packaging and Deployment, Powershell, Sharepoint 2010, Sharepoint Deployment

9 Responses

  1. Alex - September 27, 2012

    Thanks this was really useful for me. How did you find the XML schemas? I have not been able to find them in any documentation?

  2. Adi - September 28, 2012

    Small reverse engineering will help. We can view SchemaXml of already existing column through object model or powershell.
    Good to hear that the article helped you :)

  3. Richard - October 26, 2012

    Thanks for this article. It is VERY useful.

    I was wondering what XML would you have for Lookups and also What code would you put to change the display name of the “Title” column to something else.

    Thanks,

    Richard

  4. Bob Eldredge - March 3, 2013

    ADI – YOU SIMPLY ROCK!!!!

  5. Johannes - October 31, 2014

    Small nitpick:
    if($NewWebobj -ne $null) {$myTestWeb.Dispose()}
    should be
    if($NewWebobj -ne $null) {$NewWebobj.Dispose()}

    At least I guess so 😉

  6. Adi - October 31, 2014

    You are right my friend. Thanks for pointing out. I have updated the script and appreciate your sharp eyes :)

  7. Dave - September 5, 2015

    Lines 10 and 11 need “new” prepended to the var names, but I got the gist.

    SUPER useful example, thank you!

  8. ravi - October 21, 2015

    Text type filed not woking . gettinf below error:

    Exception calling “AddFieldAsXml” with “3” argument(s): “There is an unclosed literal string. Line 1, position 108.”
    At ..\test.ps1:34 char:35
    + $myCustomList.Fields.AddFieldAsXml <<<< ($siteUrlColXml,$true,[Microsoft.Shar
    ePoint.SPAddFieldOptions]::AddFieldToDefaultView)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

  9. satish - August 24, 2016

    Excellent post, Thanks alot for this…

Leave a Reply

What is 7 + 7 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)