SharePoint PowerShell for Beginners - continued

In my first post on PowerShell for SharePoint developers I described how you could use the SPSite and SPWeb object to get information on its object model. In this post I will explain a few methods and properties of SPList.

Perhaps some of you know the featue I wrote last summer to view all properties of a SharePoint list. One of the things that you could inspect with this feature is the fields that are defined on the list. It shows you its name, internal name, id and data type. For more information see my post on the List Properties feature.

Today I will show you how to get this information by using PowerShell.

To get to the list you want to investigate, you need to perform some preliminary tasks:

Load the SharePoint assembly:

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

Open the SharePoint web:

$siteUrl = "your sharepoint site url"

$webName = "your webname"

$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)

$spWeb = $spSite.OpenWeb($webName)

Instantiate the SharePoint list: 

$listName = "your listname"

$spList = $spWeb.Lists[$listName]

The fields of a list are defined in its Fields collection property. You could execute the following but it will return you a long list of all fields with all of its properties.

$spList.Fields

View column properties of a SharePoint list

So if your interested in only a few properties you could first inspect the metadata of the field object. A field is of type SPField. As we are not able to create a new object of type SPField from a default constructor, we will get an instance of the first field of the list. To view the different properties defined on SPField, execute the following statement:

$spList.Fields[0] | gm -membertype property

Following screenshot shows you the result.

image

So, if you want to view a few properties like display name, internal name, id and data type of all fields on a list, use the following syntax:

$spList.Fields | ft Title, InteralName, Id, TypeAsString

This returns you the requested data in table format. If not all information like f.e. the Id is completely visible, you could customize the columns of the table or use the fl formatter, which stands for format-list.

Add a column to a SharePoint list

The second task is to add a column to a SharePoint list. We will create a field with name "Extra column" and of type string. First of all, you need to know which method you need to execute on the list object and what are the arguments you need to specify.

Execute $spList.Fields.Add to get an overview of the different overloads of the Add mehtod:

image

As you can see, there are 2 overloads to add a new item to a SharePoint list. The overload we are going to use is the one that needs a column name, a field type and the required boolean. Execute the following:

$spList.Fields.Add("Extra column", "Microsoft.SharePoint.SPFieldType.Text", 0)

This returns you the following error:

image

As you can see in the error message returned by PowerShell, we didn't specify a correct value for the field type. If you tak a good look at the error message you will notice that PowerShell returns all possible values for that argument in the error message!

Specify boolean values as 0 and 1. If you pass the string "false", it is accepted as "different from false" and your field will be added as a required field.

Following statement will add the column to the list:

$spList.Fields.Add("Extra column", "Text", 0)

In addition, PowerShell returns you the internal name of the column you just added, i.e. Extra_x0020_column

Return to Internet Explorer and browse to the Settings Page of your list. You will see that the column is added to the list:

image

You can add it to the default view as you want.

 

In a next post I will explain how you can write a function that adds a column to a SharePoint list. You will be able to use this function for adding columns to each different type of list.