-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateListFuildsWithSchema.ps1
More file actions
75 lines (63 loc) · 2.43 KB
/
CreateListFuildsWithSchema.ps1
File metadata and controls
75 lines (63 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Add-FieldToList($SiteURL,$ListName, $FieldName, $FieldType, $IsRequired)
{
#Set the Error Action
$ErrorActionPreference = "Stop"
Try{
#Get the List
$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
#Check if List with specific name exists
if($List -ne $null)
{
if(!$List.Fields.ContainsField($FieldName))
{
#Add columns to the List
$List.Fields.Add($FieldName,$FieldType,$IsRequired)
#Update the List
$List.Update()
#Update the default view to include the new column
$View = $List.DefaultView # OR $List.Views["All Items"]
$View.ViewFields.Add($FieldName)
$View.Update()
write-host "New Column '$FieldName' Added to the List!" -ForegroundColor Green
}
else
{
write-host "Field '$FieldName' Already Exists in the List" -ForegroundColor Red
}
}
else
{
write-host "List '$ListName' doesn't exists!" -ForegroundColor Red
}
}
catch {
Write-Host $_.Exception.Message -ForegroundColor Red
}
finally {
#Reset the Error Action to Default
$ErrorActionPreference = "Continue"
}
}
#Parameters
$SiteURL="https://intranet.crescent.com/"
$ListName = "Customer Directory"
#Add 'Name' - Single Line of Text Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Text
$FieldName="Name"
$IsRequired = $False
#Call the function to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired
#Add Phone Number - Number Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Number
$FieldName="Phone Number"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired
#Date of Joing - Date Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::DateTime
$FieldName="Date of Join"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired
#Read more: https://www.sharepointdiary.com/2015/12/how-to-add-column-to-sharepoint-list-using-powershell.html#ixzz7MK52gBGO