Add fields to sharepoint list

31/03/2015 15:08

This script creates sharepoint list based on 'custom list' template and adds few fields of type

- renames default Title field

- single line of text

- date

- user

- note (rich text)

Then also hides some of them from Edit form


$webUrl="https://yoursitecollection"

Add-Pssnapin microsoft.sharepoint.powershell -erroraction 0

#define list properties
$listTitle="My special list"
$listUrlPart = "myspeciallist";
$listDescription="List for powershell example to create fields"
$templateName = "Custom List"

$web = Get-SPWeb -Identity $WebUrl
$listTemplate = $web.ListTemplates[$templateName]

$list = $web.Lists[$listTitle];
if($list -eq $null)
{
    #create with url in title and then rename title (to have nice url)
    $web.Lists.Add($listUrlPart,$listDescription,$listTemplate)

    $list = $web.Lists[$listUrlPart];
    $list.Title = $listTitle;
    $list.update();
}


function SetTitleFieldAndHideFromView($list, $fieldInternalName, $fieldTitle)
{
    $field = $list.Fields[$fieldInternalName];
    $field.Title = $fieldTitle;
 
 #in case we want to hide field from edit form (xml attribute ShowInEditForm = 'FALSE' doesn't work), uncomment this code
    #$field.ShowInEditForm = 0;
    #$field.ShowInNewForm = 0;
    $field.Update();
}

#define fields

#Page title, rename default Title field
$list.Fields["Title"].Title = "Page title";
$list.Fields["Title"].Update();


#or hide Title field from default view
$spView = $list.DefaultView
$spView.ViewFields.Delete("LinkTitle")
#in document library
$spView.ViewFields.Delete("Title")
$spView.ViewFields.Delete("LinkFilename")
$spView.ViewFields.Delete("DocIcon")

$spView.Update()

#hide Title field
$titleFld = $list.Fields.TryGetFieldByStaticName("Title");
$titleFld.Hidden = $true;
$titleFld.DefaultValue = "Detail"; #set default value, because value is required
$titleFld.Update();


#column of type 'Hyperlink or picture'
$titleFieldXml = "<Field Type='URL' Format='Hyperlink' DisplayName='myHyperlinkFld' Required='FALSE' MaxLength='255' StaticName='myHyperlinkFld' Name='myHyperlinkFld' />"
$fld = $list.Fields.AddFieldAsXml($titleFieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView);
SetTitleFieldAndHideFromView -list $list -fieldInternalName $fld -fieldTitle "my Hyperlink Fld"



#column of type 'Single line of text'
$textFieldXml = "<Field Type='Text' DisplayName='myTextField' Required='FALSE' MaxLength='255' StaticName='myTextField' Name='myTextField' />"
$fld = $list.Fields.AddFieldAsXml($textFieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView);
SetTitleFieldAndHideFromView -list $list -fieldInternalName $fld -fieldTitle "My Text Field"


#Date
$wfStartDateFieldXml = "<Field Type='DateTime' Format='DateOnly' DisplayName='wfStartDate' Required='FALSE' MaxLength='255' StaticName='wfStartDate' Name='wfStartDate' />"
$fld = $list.Fields.AddFieldAsXml($wfStartDateFieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView);
SetTitleFieldAndHideFromView -list $list -fieldInternalName $fld -fieldTitle "WF start date"

#Person, User
$wikiAdminFieldXml = "<Field Type='User' DisplayName='wikiAdministrator' List='UserInfo' ShowField='ImnName' UserSelectionMode='PeopleOnly' UserSelectionScope='0' Name='wikiAdministrator'/>"
$fld = $list.Fields.AddFieldAsXml($wikiAdminFieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
SetTitleFieldAndHideFromView -list $list -fieldInternalName $fld -fieldTitle "Wiki page administrator"


#  Note, Rich text (in case of html, use also RichTextMode='FullHtml' attribute)
$myNote = "<Field Type='Note' RichText='TRUE'  NumLines='10' DisplayName='myNote' Required='FALSE' MaxLength='255' StaticName='myNote' Name='myNote' ShowInEditForm='FALSE'/>"
$fld = $list.Fields.AddFieldAsXml($myNote); #don't add field to default view
SetTitleFieldAndHideFromView -list $list -fieldInternalName $fld -fieldTitle "My note"


$list.Update();