#*************************************************************************************************************************************
# File name : AllFunctionToList.ps1
# Created by : v-trdong 2012/10/30
# This script is for uploading the custom list tempalte, and create a list by the custom list tempalte,then operate the list.
# the paramters are follow:
# $spSite: The website url you want to operate, example http://localhost
# $spweb : The web url you want to upload the file, example http://localhost
# $spList: The list you want to operate
# $file : The file path that relative to this script, example MyTemplate.stp
# $folder : The upolad folder path which related to the web, example _catalogs/It
# $dir : The path of the current script file
# $source : The source url you want to download file
# $destination : The path in disk you want to put
# Note: If there is a existing file name, the old file will be overwrited,please make sure the file need to be uploaded in the same folder of this script
#***************************************************************************************************************************************
1. Upload the list template
# Import the Microsoft.SharePoint.PowerShell
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# Upload the list template
# Example: UpLoadTemplate "http://localhost" "MyTemplate.stp" "template.stp"
function UpLoadTemplate($spSite,$path,$Name){
# Get the root web, only the root web contains list templates
$spSite=Get-SPSite -Identity $spSite
$spWeb=$spSite.RootWeb
try{
# Get the site page of the template sitepage
$spFolder = $spWeb.GetFolder("_catalogs/It")
$spFileCollection = $spFolder.Files
# Get the path of the file you want to upload
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
$dir=Split-Path $Invocation.MyCommand.Path
$path = $dir+"\"+$path
# Check the path
If(Test-Path "$path"){
$file=Get-ChildItem $path
# Add the file
$spFileCollection.Add("/_catalogs/lt/"+$Name,$file.OpenRead(),$true)
}
else{
# If the path is not right, throw the exception
write-host "The path is wrong"
throw
}
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}
2. Create a list by the custom list template
# Create a list by the custom list template
# Example: CreateCustomList "http://localhost" "MyTemplate" "MyNewList" "This is my list"
function CreateCustomList($spSite,$spTemplate,$spListName,$spDescription){
$spSite=Get-SPSite $spSite
$spWeb=$spSite.RootWeb
try{
# Get all the custom template
$listTemplates = $spSite.GetCustomListTemplates($spWeb);
# Create the list via the template name
$spWeb.Lists.Add($spListName, $spDescription, $listTemplates[$spTemplate])
$list=$spWeb.Lists[$spListName]
# Make the list view in OnQuickLaunch
$list.OnQuickLaunch="True"
$list.Update()
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}
Add the view of column that you want to view
3. Add the view of column that you want to view
# Add the view of column that you want to view
# Example: AddColumnView "http://localhost" "http://localhost/Lists/MyNewList/AllItems.aspx" "MyNewList" "Address"
function AddColumnView($spWeb,$spView,$spList,$columnName){
$spWeb=Get-SPWeb -Identity $spWeb
try{
$spList=$spWeb.Lists[$spList]
# Get the view list by the url
$spView=$spWeb.GetViewFromUrl($spView)
# Get the field by column name ,then add it
$spField=$spList.Fields[$columnName]
$spView.ViewFields.Add($spField)
$spView.Update()
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}
4. Delete column view
# Delete column view
# Example : AddColumnView "http://localhost" "http://localhost/Lists/MyNewList/AllItems.aspx" "MyNewList" "Address"
function AddColumnView($spWeb,$spView,$spList,$columnName){
$spWeb=Get-SPWeb -Identity $spWeb
try{
$spList=$spWeb.Lists[$spList]
# Get the View if the list by the view url
$spView=$spWeb.GetViewFromUrl($spView)
# Get the field should be deleted
$spField=$spList.Fields[$columnName]
$spView.ViewFields.Delete($spField)
$spView.Update()
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}
5. Delete column by its title
# Delete Column with its title
# Example : DeleteColumn "http://localhost" "MyNewList" "Address"
function DeleteColumn($spWeb,$spListName,$title){
$spWeb=Get-SPWeb -Identity $spWeb
try{
$spList=$spWeb.Lists[$spListName]
$column=$spList.Fields[$title]
# Show the column and make the column be read, the operate
$column.Hidden=$false
$column.ReadOnlyField=$false
$column.Update()
$spList.Fields.Delete($column)
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}
6. Delete the item by the Title
# Delete Column with its title
# Delete Column with its title
# Example : DeleteColumn "http://localhost" "MyNewList" "Address"
function DeleteColumn($spWeb,$spListName,$title){
$spWeb=Get-SPWeb -Identity $spWeb
try{
$spList=$spWeb.Lists[$spListName]
$column=$spList.Fields[$title]
# Show the column and make the column be read, the operate
$column.Hidden=$false
$column.ReadOnlyField=$false
$column.Update()
$spList.Fields.Delete($column)
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}
7. Download the file by the link
# Download the file by the link
# Example : DownLoadFile "http://localhost/DocumentUpload/1.txt " "C:\Users\v-trdong\Desktop\ListTemplate\1.txt"
function DownLoadFile($source,$destination){
try{
# The source url you want to download
$source = $source
# The destination path in disk you want to put
$destination = $destination
$wc = New-Object System.Net.WebClient
# Call the function to download
$wc.DownloadFile($source, $destination)
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
write-host "DownLoad success!"
}
}