In this post I have come with SharePoint 2010 List and PowerShell scripts. Here we discussed the various things, which is possible in SharePoint List using PowerShell.
1. Create a list
Syntax:
$spAssigment = Start-SPAssignment #Use to dispose SPWeb safely
$spWeb = Get-SPWeb ServerURL -AssignmentCollection $spAssignment #Get SPWeb Instance
$spWeb.ListTemplates | Select Name, Description #Display List of Templates Available
$spTemplate = $spWeb.ListTemplates["Custom List"] #Create SPTemplate instance of Type Custom List
$spWeb.Lists.Add("List Title", "Description", $spTemplate) #Add list to site
Example:
$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://chesgh201tw1v:1000 -AssignmentCollection $spAssignment
$SPTemplate = $SPWeb.ListTemplates["Custom List"]
$SPWeb.Lists.Add("Custom List","Description",$SPTemplate)
Stop-SPAssignment $SPAssignment
2. Display list on Quick Launch
Syntax:
$spList = $spWeb.Lists["List Title"] #Get list instance
$spList.OnQuickLaunch = $True #Set true to display in Quick Launch
$spList.Update() #Update list to reflect changes in site
Example:
$SPList = $SPWeb.Lists["Custom List"]
if ($SPList -ne $NULL){
$SPList.OnQuickLaunch = $True
$SPList.Update()
}
3. Add Field to the list
Syntax:
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text #Get Field type to create
$spList.Fields.Add("My Column", $spFieldType, $false) #Add new field to list
Example:
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Boolean
$spList.Fields.Add("Yes/No", $spFieldType, $false)
4. Add Field to Default View
Syntax:
$Views = $List.Views["All Items"] #Get the view
$Views.ViewFields.Add("MyColumn"); #Add field to the view
Example:
$Views = $List.Views["All Items"]
$Views.ViewFields.Add("Yes/No");
5. Create View and add field
Syntax:
$spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
$spViewFields = New-Object System.Collections.Specialized.StringCollection #Create string collection object
$spViewFields.Add("Title") #Add columns
$spViewFields.Add("My Column")
$spViewName = "My View" #Set view name
$spListView = $spList.Views.Add($spViewName, $spViewFields, $spViewQuery, 100, $True, $False, “HTML”, $False) #Add view to list
$spListView.DefaultView = $True #Set view as default
$spListView.Update() #Update view to reflect changes in site
Example:
$spViewFields.Add("Title”)
$spViewFields.Add("Yes/No")
$spListView = $spList.Views.Add($spViewName, $spViewFields, $spViewQuery, 100, $True, $False, “HTML”, $True) (Last Boolean value to set the view as default view)
$spListView.Update()
6. Delete List
Approach 1
$spList.Delete()
Approach 2
Get-SPWeb "SiteURL" | % { $_.Lists.Delete([System.Guid]$_.Lists["CustomList"].ID) }
Below I have added a complete script, where you can find all the above listed points. Which I have done.
try
{
#Get Site
$SPsite = Get-SPSite("http://chesgh201tw1v:1001") # $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1001");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Create Custom List
$OpenWeb.Lists.Add("MyCustomListPS","Custom List craetion from Powershell",$OpenWeb.ListTemplates["Custom List"])
#Open the List
$List = $OpenWeb.Lists.TryGetList("MyCustomListPS")
if($List -ne $Null)
{
#Shows on Quicklaunch
$List.OnQuicklaunch = $True
$List.Update()
write-host “Defining values for choice: “ -foregroundcolor Green
#Define Choice Field
$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("First Choice")
$Choices.Add("Second Choice")
$Choices.Add("Third Choice")
write-host “start Adding Fields to list: “ -foregroundcolor Green
#Add Yes/No Field to list
$List.Fields.Add("Yes/No", "Boolean", $FALSE)
#Add Choice Field to list
# $List.Fields.Add("Choice", [Microsoft.SharePoint.SPFieldType]::Choice,$FALSE,$FALSE,$Choices)
$List.Fields.Add("Choice","Choice",$FALSE,$FALSE,$Choices)
#Add Currency Field to list
$List.Fields.Add("Currency", "Currency", $FALSE)
#Add DateTime Field to list
$List.Fields.Add("DateTime","DateTime",$false)
$List.Fields[“DateTime”].Description = “My DateTime Field”
#Add Integer Field to list
$List.Fields.Add("Integer", "Integer", $FALSE)
# Get List Item from list Lookup (MyParentList)
$LookupList = $OpenWeb.Lists["MyParentList"]
$LookupListID = $LookupList.ID
# Add Field Lookup to list
$List.Fields.AddLookup("Lookup",$LookupListID,$FALSE)
#Add Multi Choice Field to list
$List.Fields.Add("MultiChoice","MultiChoice",$FALSE,$FALSE,$Choices)
#Add Note Field to list
$List.Fields.Add("Note", "Note", $FALSE)
#Add Number Field to list
$List.Fields.Add("Number", "Number", $FALSE)
#Add TextField to list
$List.Fields.Add("Text", "Text", $FALSE)
#Add UrlHyperField Field to list
$List.Fields.Add("Url","URL",$false)
$List.Fields[“Url”].Description = “My UrlHyperField Field”
# Add User Field to list
$List.Fields.Add("User", "User", $FALSE)
#Update view
write-host “Updating View: “ -foregroundcolor Green
$Views = $List.Views["All Items"]
$Views.ViewFields.Add("Yes/No")
$Views.ViewFields.Add("Choice")
$Views.ViewFields.Add("Currency")
$Views.ViewFields.Add("DateTime")
$Views.ViewFields.Add("Integer")
$Views.ViewFields.Add("Lookup")
$Views.ViewFields.Add("MultiChoice")
$Views.ViewFields.Add("Note")
$Views.ViewFields.Add("Number")
$Views.ViewFields.Add("Text")
$Views.ViewFields.Add("Url")
$Views.ViewFields.Add("User")
$Views.Update()
#Create view and add field
$spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
$spViewFields = New-Object System.Collections.Specialized.StringCollection #Create string collection object
$spViewFields.Add("Title") #Add columns
$spViewFields.Add("Yes/No") #Add columns
$spViewFields.Add("Choice") #Add columns
$spViewFields.Add("Currency") #Add columns
$spViewFields.Add("DateTime") #Add columns
$spViewFields.Add("Integer") #Add columns
$spViewFields.Add("Lookup") #Add columns
$spViewFields.Add("MultiChoice") #Add columns
$spViewFields.Add("Note") #Add columns
$spViewFields.Add("Number") #Add columns
$spViewFields.Add("Text") #Add columns
$spViewFields.Add("Url") #Add columns
$spViewFields.Add("User") #Add columns
$spViewName = "My View" #Set view name
$spListView = $List.Views.Add($spViewName, $spViewFields, $spViewQuery, 50, $True, $False, “HTML”, $False) #Add view to list
$spListView.DefaultView = $True #Set view as default
$spListView.Update() #Update view to reflect changes in site
}
$OpenWeb.Dispose();
$SPSite.Dispose()
write-host “Opeartion Completed Successfully: “ -foregroundcolor Green
}
catch [System.SystemException]
{
write-host “Execution stopped due to: “+$_.Message -foregroundcolor Red
}
Let me discuss all other Fields related actions and List item related actions in my fore coming posts.
Wait for that ……………..
Happy Programming ……………………………………
1. Create a list
Syntax:
$spAssigment = Start-SPAssignment #Use to dispose SPWeb safely
$spWeb = Get-SPWeb ServerURL -AssignmentCollection $spAssignment #Get SPWeb Instance
$spWeb.ListTemplates | Select Name, Description #Display List of Templates Available
$spTemplate = $spWeb.ListTemplates["Custom List"] #Create SPTemplate instance of Type Custom List
$spWeb.Lists.Add("List Title", "Description", $spTemplate) #Add list to site
Example:
$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://chesgh201tw1v:1000 -AssignmentCollection $spAssignment
$SPTemplate = $SPWeb.ListTemplates["Custom List"]
$SPWeb.Lists.Add("Custom List","Description",$SPTemplate)
Stop-SPAssignment $SPAssignment
2. Display list on Quick Launch
Syntax:
$spList = $spWeb.Lists["List Title"] #Get list instance
$spList.OnQuickLaunch = $True #Set true to display in Quick Launch
$spList.Update() #Update list to reflect changes in site
Example:
$SPList = $SPWeb.Lists["Custom List"]
if ($SPList -ne $NULL){
$SPList.OnQuickLaunch = $True
$SPList.Update()
}
3. Add Field to the list
Syntax:
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text #Get Field type to create
$spList.Fields.Add("My Column", $spFieldType, $false) #Add new field to list
Example:
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Boolean
$spList.Fields.Add("Yes/No", $spFieldType, $false)
4. Add Field to Default View
Syntax:
$Views = $List.Views["All Items"] #Get the view
$Views.ViewFields.Add("MyColumn"); #Add field to the view
Example:
$Views = $List.Views["All Items"]
$Views.ViewFields.Add("Yes/No");
5. Create View and add field
Syntax:
$spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
$spViewFields = New-Object System.Collections.Specialized.StringCollection #Create string collection object
$spViewFields.Add("Title") #Add columns
$spViewFields.Add("My Column")
$spViewName = "My View" #Set view name
$spListView = $spList.Views.Add($spViewName, $spViewFields, $spViewQuery, 100, $True, $False, “HTML”, $False) #Add view to list
$spListView.DefaultView = $True #Set view as default
$spListView.Update() #Update view to reflect changes in site
Example:
$spViewFields.Add("Title”)
$spViewFields.Add("Yes/No")
$spListView = $spList.Views.Add($spViewName, $spViewFields, $spViewQuery, 100, $True, $False, “HTML”, $True) (Last Boolean value to set the view as default view)
$spListView.Update()
6. Delete List
Approach 1
$spList.Delete()
Approach 2
Get-SPWeb "SiteURL" | % { $_.Lists.Delete([System.Guid]$_.Lists["CustomList"].ID) }
Below I have added a complete script, where you can find all the above listed points. Which I have done.
try
{
#Get Site
$SPsite = Get-SPSite("http://chesgh201tw1v:1001") # $SPSite = New-Object Microsoft.SharePoint.SPSite("http://chesgh201tw1v:1001");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Create Custom List
$OpenWeb.Lists.Add("MyCustomListPS","Custom List craetion from Powershell",$OpenWeb.ListTemplates["Custom List"])
#Open the List
$List = $OpenWeb.Lists.TryGetList("MyCustomListPS")
if($List -ne $Null)
{
#Shows on Quicklaunch
$List.OnQuicklaunch = $True
$List.Update()
write-host “Defining values for choice: “ -foregroundcolor Green
#Define Choice Field
$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("First Choice")
$Choices.Add("Second Choice")
$Choices.Add("Third Choice")
write-host “start Adding Fields to list: “ -foregroundcolor Green
#Add Yes/No Field to list
$List.Fields.Add("Yes/No", "Boolean", $FALSE)
#Add Choice Field to list
# $List.Fields.Add("Choice", [Microsoft.SharePoint.SPFieldType]::Choice,$FALSE,$FALSE,$Choices)
$List.Fields.Add("Choice","Choice",$FALSE,$FALSE,$Choices)
#Add Currency Field to list
$List.Fields.Add("Currency", "Currency", $FALSE)
#Add DateTime Field to list
$List.Fields.Add("DateTime","DateTime",$false)
$List.Fields[“DateTime”].Description = “My DateTime Field”
#Add Integer Field to list
$List.Fields.Add("Integer", "Integer", $FALSE)
# Get List Item from list Lookup (MyParentList)
$LookupList = $OpenWeb.Lists["MyParentList"]
$LookupListID = $LookupList.ID
# Add Field Lookup to list
$List.Fields.AddLookup("Lookup",$LookupListID,$FALSE)
#Add Multi Choice Field to list
$List.Fields.Add("MultiChoice","MultiChoice",$FALSE,$FALSE,$Choices)
#Add Note Field to list
$List.Fields.Add("Note", "Note", $FALSE)
#Add Number Field to list
$List.Fields.Add("Number", "Number", $FALSE)
#Add TextField to list
$List.Fields.Add("Text", "Text", $FALSE)
#Add UrlHyperField Field to list
$List.Fields.Add("Url","URL",$false)
$List.Fields[“Url”].Description = “My UrlHyperField Field”
# Add User Field to list
$List.Fields.Add("User", "User", $FALSE)
#Update view
write-host “Updating View: “ -foregroundcolor Green
$Views = $List.Views["All Items"]
$Views.ViewFields.Add("Yes/No")
$Views.ViewFields.Add("Choice")
$Views.ViewFields.Add("Currency")
$Views.ViewFields.Add("DateTime")
$Views.ViewFields.Add("Integer")
$Views.ViewFields.Add("Lookup")
$Views.ViewFields.Add("MultiChoice")
$Views.ViewFields.Add("Note")
$Views.ViewFields.Add("Number")
$Views.ViewFields.Add("Text")
$Views.ViewFields.Add("Url")
$Views.ViewFields.Add("User")
$Views.Update()
#Create view and add field
$spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
$spViewFields = New-Object System.Collections.Specialized.StringCollection #Create string collection object
$spViewFields.Add("Title") #Add columns
$spViewFields.Add("Yes/No") #Add columns
$spViewFields.Add("Choice") #Add columns
$spViewFields.Add("Currency") #Add columns
$spViewFields.Add("DateTime") #Add columns
$spViewFields.Add("Integer") #Add columns
$spViewFields.Add("Lookup") #Add columns
$spViewFields.Add("MultiChoice") #Add columns
$spViewFields.Add("Note") #Add columns
$spViewFields.Add("Number") #Add columns
$spViewFields.Add("Text") #Add columns
$spViewFields.Add("Url") #Add columns
$spViewFields.Add("User") #Add columns
$spViewName = "My View" #Set view name
$spListView = $List.Views.Add($spViewName, $spViewFields, $spViewQuery, 50, $True, $False, “HTML”, $False) #Add view to list
$spListView.DefaultView = $True #Set view as default
$spListView.Update() #Update view to reflect changes in site
}
$OpenWeb.Dispose();
$SPSite.Dispose()
write-host “Opeartion Completed Successfully: “ -foregroundcolor Green
}
catch [System.SystemException]
{
write-host “Execution stopped due to: “+$_.Message -foregroundcolor Red
}
Let me discuss all other Fields related actions and List item related actions in my fore coming posts.
Wait for that ……………..
Happy Programming ……………………………………
No comments:
Post a Comment