`
bcyy
  • 浏览: 1823463 次
文章分类
社区版块
存档分类
最新评论

SharePoint 2010 PowerShell base function abut list

 
阅读更多

#*************************************************************************************************************************************
# 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
#***************************************************************************************************************************************

# 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()
}
}

}

# 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
# 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()
}
}
}

# 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()
}
}
}

# Delete the item by the Title
# Example : DeleteItemByTitle "http://localhost" "MyNewList" "007"
function DeleteItemByTitle($spWeb,$spList,$spTitle){
$spWeb=Get-SPWeb -Identity $spWeb
try{
$spList=$spWeb.Lists[$spList]

# Initial the query object
$spQuery=New-Object Microsoft.SharePoint.SPQuery

# Query the item via CAML
$camlQuery='<Where><Eq><FieldRef Name="Title"/><Value Type="Text">$spTitle</Value></Eq></Where>'
$spQuery.Query=$camlQuery
$spQuery.RowLimit=100

# Execute the CAML query statement
$spListItem=$spList.GetItems($spQuery)

# Find the right item in circle
$spListItem|ForEach-Object {
$_.Delete()
}
}
catch{
write-host "(ERROR : "$_.Exception.Message")"
throw
}
finally{
if($spWeb -eq $null){
write-host "The web site is not existing"
}
else{
$spWeb.Dispose()
}
}
}

# 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()
}
}
}

# 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!"
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics