Adding Fields to a List in Sharepoint

There are a couple of types that you can add to a list, Single Line of Text, Multiple Lines o Text, Users etc. Here’s how to get a complete list of all available list types:


PS > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb(); $OpenWeb.FieldTypeDefinitionCollection | Select TypeName; $OpenWeb.Dispose(); $SPSite.Dispose()

TypeName
--------
Counter
Text
Note
Choice
MultiChoice
GridChoice
Integer
Number
ModStat
Currency
DateTime
Lookup
Boolean
Threading
ThreadIndex
Guid
Computed
File
Attachments
User
URL
Calculated
Recurrence
CrossProjectLink
ContentTypeId
MultiColumn
LookupMulti
UserMulti
WorkflowStatus
AllDayEvent
WorkflowEventType
PageSeparator

Let’s Add a couple of Fileds to our Custom List. First we have to create a couple of objects that hold the information about the Fields that we want to add


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb(); $User = $OpenWeb.FieldTypeDefinitionCollection["User"]; $OpenWeb.Dispose(); $SPSite.Dispose()

PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb(); $Text = $OpenWeb.FieldTypeDefinitionCollection["Text"]; $OpenWeb.Dispose(); $SPSite.Dispose()

Next step is adding the Fields to our List. This is done through the Add() method:


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb(); $List = $OpenWeb.Lists["My Custom List"]; $OpenWeb.Dispose(); $SPSite.Dispose()

PS > $List.Fields.Add("UserField", "User", $User)
PS > $List.Fields.Add("TextField", "Text", $Text)

This Adds the Fields to our Custom list but they don’t show up in Sharepoint. To solve this we have to add them to the Lists View:


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb();$Views = $list.Views["All Items"]; $OpenWeb.Dispose(); $SPSite.Dispose()

PS > $Views.ViewFields.Add("UserField")
PS > $Views.ViewFields.Add("TextField")
PS > $Views.Update()

Adding Text Fields and User fields is rather simple, but say we want to add a Choice field and a LookUp field. This is a little bit tricky. Let’s start with the Choice Field. First, we’ll create the different choices. this is don through the System.Collections.Specialized.StringCollection.


$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("First Choice")
$Choices.Add("Second Choice")
$Choices.Add("Third Choice")

Now that we have created our Choices, we can go aheead and add them to the Sharepoint list. It’s done a little different than before.


$List.Fields.Add(
	"ChoiceField",
	[Microsoft.SharePoint.SPFieldType]::Choice,
	$FALSE,
	$FALSE,
	$Choices
)

And then update the Views.


$Views = $List.Views["All Items"]
$Views.ViewFields.Add("ChoiceField")
$Views.Update()

Now, lets add a Lookup Field. To do this, we need a List to Lookup the information from. In this example I’ve created a simple List with 3 items in as shown below.

sharepoint-10

First we have to tell Sharepoint which list we want to Lookup the information in.


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb();$LookupList = $OpenWeb.Lists["LookupList"]
PS > $LookupListID = $LookupList.ID

The Method that we are going to use AddLookup(), takes the Lists ID as an argument, so thats why I made a $LookupListID variable to hold this information. Next, we run the method.


PS > $List.Fields.AddLookup("LookupField",$ID,$FALSE)

PS > $Views = $list.Views["All Items"]
PS > $Views.ViewFields.Add(“LookupField”)
PS > $Views.Update()

The List should look something like this now.

sharepoint-11

sharepoint-12

Rating 3.00 out of 5
[?]
Categories: Sharepoint Tags:
  1. Ramesh
    April 5th, 2011 at 06:09 | #1

    Hey, thanks for the wonderful post. Can you please let me know how can change the relationship of the lookup column, by default it takes the title column of the sourcelist.

  1. No trackbacks yet.

Comment Spam Protection by WP-SpamFree