SharePoint 2010 PowerShell base function abut list
2012-10-29 16:56
375 查看
#*************************************************************************************************************************************
# 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
#***************************************************************************************************************************************
# 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!" } }
相关文章推荐
- SharePoint 2010 PowerShell 系列 之 Create List and Field --Lookup
- SharePoint 2010 PowerShell 系列 之 Create List and Field
- sharepoint 2010 library或者list里关于列的添加、修改、删除、排序
- Adding Custom Actions to the List Item Menu in SharePoint 2010 Using SharePoint Designer 2010[Sharepoint中添加自定义菜单]
- SharePoint 2010开发工具图解系列:PowerShell脚本
- 通过PowerShell卸载所有的SharePoint 2010 解决方案
- PowerShell 在 SharePoint 2010 自动化部署中的应用(2)-编译打包
- 从SharePoint 2010网站上导出list的方法
- SharePoint 2010 页面引用 Reporting Services 展现 List 报表
- 使用SharePoint 2010的PowerShell完成Object Model编程才能完成的功能的一个实例
- SharePoint 2010 PowerShell(2)使用PowerShell管理网站
- 通过PowerShell卸载所有的SharePoint 2010 解决方案
- PowerShell在SharePoint 2010自动化部署中的应用(1)--代码获取
- SharePoint 2010 PowerShell(4)使用PowerShell管理文档库
- 从SharePoint 2010网站上导出list的方法
- SharePoint【Ribbon系列】-- 10.Reference :List definitions Type and BaseType
- SharePoint 2010 PowerShell(2)使用PowerShell管理网站
- 用PowerShell 启用Sharepoint 2010 Session
- PowerShell 在 SharePoint 2010 自动化部署中的应用(2)-编译打包
- SharePoint列表的模板类型中的BaseType参数和ListTemplate参数