Quantcast
Channel: Engage In SharePoint » lists
Viewing all articles
Browse latest Browse all 3

Sharepoint and Powershell – Working with lists

$
0
0

Hello Everyone,

Last week I started to look at how powershell could be used to create around 30 lists quickly with all the required fields populated.
The requirements were as follows;

List Name (Text)
Student (People picker)
WB day month year (Boolean)
WB day month year (Boolean)

Basically a very simple attendance system for the after school clubs we run.
The design should be simple to use and the default for the WB columns should be True.

Now I am not a developer and I know this is probably a simple task for such wonderful people and as I didnt want to go down that route I thought I would see how I could achieve this in powershell.

Here is what I came up with, its not finished but it does work very well. (I will post a finished script later in the week)

# Load the Sharepoint DLL
[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
# URL of the site you want to open
$siteUrl = “http://extranet.samworthenterpriseacademy.org/staff/powershell/
# Select the website name
$webName = “powershell”
# Create sharepoint site object inside spSite variable
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
# Open the website into the variable
$spWeb = $spSite.OpenWeb($webName)
# Creates a custom list with the title in the first variable, description in second and listtemplate in third.
$spWeb.lists.add(“Football”,”",$spSite.listtemplates["Custom List"])
# Creates a variable containing the list declared in the [] brackets.
$listName = “Football”
$splist = $spWeb.Lists[$listName]

# Field format = $list.fields.add(“fieldname”,”type”,required) required is 1 or 0

function AddBoolField
{
param ([Microsoft.SharePoint.SPList]$spList, [string]$fieldName)
$spField = new-object Microsoft.SharePoint.SPFieldChoice($spList.Fields,”Boolean”, $fieldName)
$spList.Fields.Add($spField)
$spList.Update()
$spField = $spList.Fields[$fieldName]
#Options for a choice field
#$spField.Choices.Add(“No”)
#$spField.Choices.Add(“Yes”)
#$spField.Choices.Add(“Preferred”)
$spField.DefaultValue = 1
$spField.Update()
$spList.Update()
}
# Adds a new field  by calling the above function
AddBoolField -spList $spList -fieldName “08 Feb 10″

I am sure the developers out there will destroy me for my improper commenting (hey at least it has comments) as my understanding might not be upto speed but I know in my head what its doing and can certainly hack it around to get it to do as I wish.

I hope that helps someone as I really struggled to find some decent code online and have tried a fair few sites to get what I needed.
I will also apologise for not linking to anyone in particular as it really was a little from here, there and everywhere.

Code block image is here so its a bit more legible when colour coded.

Thanks

Matthew Hughes


Viewing all articles
Browse latest Browse all 3

Trending Articles