PowerShell——修改list columnDefaultValue属性

Files:

Deploy.bat

View Code
1 Powershell.exe -Command Set-ExecutionPolicy "Bypass"
2 Powershell.exe -Command "& {%~dp0InstallMain_listcolumn.ps1}"
3 Pause

InstallMain.ps1

View Code
 1 #********************************************************************
 2 #    Create Site List Instance
 3 #      
 4 #********************************************************************
 5 
 6 
 7 # Log the Power shell window text
 8 $0 = $MyInvocation.MyCommand.Definition                 # Get complete file path of file 
 9 $dp0 = [System.IO.Path]::GetDirectoryName($0)           # Get current Directory file path 
10 $bits = Get-Item $dp0 | Split-Path -Parent              # Get current Drive   (eg:  D:\)
11 
12 ## check to ensure Microsoft.SharePoint.PowerShell is loaded if not using the SharePoint Management Shell 
13 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
14 if ($snapin -eq $null)  
15 {    
16     Write-Host "Loading SharePoint Powershell Snapin..."    
17     Add-PSSnapin "Microsoft.SharePoint.Powershell" 
18     Write-Host "SharePoint Powershell Snapin Loaded"   
19 } 
20 
21 
22 #Step0 : Assign values for Variable
23 $scriptFolderPath = $dp0
24 $path = "$scriptFolderPath\"
25 #$configurationFilePath = "$path\CreateListInstance.xml"
26 $configFile = "$path\UpdateListColumn.xml"
27 
28 #Step1 : Create Site Collection Application
29 #$command = "& '$scriptFolderPath\CreateListInstance.ps1' -InputFile '$configurationFilePath'"
30 $command = "& '$scriptFolderPath\UpdateListColumn.ps1' -InputFile '$configFile'"
31 
32 Invoke-Expression $command

 UpdateListColumn.ps1

View Code
 1 function ChangeListColumn([xml]$solutionsConfig)
 2 {
 3     ForEach($Web in $solutionsConfig.SiteCollection.Webs.Web)
 4     {
 5         $webpath=$solutionsConfig.SiteCollection.url+$Web.url
 6         Write-Host -foregroundcolor white $webpath
 7         $targetWeb=Get-SPWeb $webpath
 8 
 9         $list=$Web.List
10 
11         $targetlist=$targetWeb.Lists[$list.name]
12         
13         Write-Host -foregroundcolor white $list.name
14 
15         $targetfield=$list.property
16         $targetvalue=$list.value
17 
18         Write-Host -foregroundcolor white $targetlist.Fields[$targetfield].DefaultValue
19         $field=$targetlist.Fields[$targetfield]
20         $field.DefaultValue=$targetvalue
21 
22         Write-Host -foregroundcolor white $targetlist.Fields[$targetfield].DefaultValue
23         $field.update()
24         $targetlist.Update()
25         $targetWeb.Update()
26         write-host -foregroundcolor white '==========================='
27         write-host -foregroundcolor white ''
28     }
29 }
30 
31 if([string]::IsNullOrEmpty($configFile)){return}
32 [xml]$solutionsConfig=Get-Content $configFile
33 if($solutionsConfig -eq $null){return}
34 
35 Write-Host -foregroundcolor white "Begin update ListColumn."
36 Write-Host ""
37 ChangeListColumn $solutionsConfig
38 Write-Host -foregroundcolor white "End update ListColumn."

Attention:

$field.update()不能少,否则不会修改数据库.同时要结合list使用,否则此method不能调用

UpdateListColumn.xml

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <SiteCollection url="http://xde7109sp033:9999">
 3   <Webs>
 4     <Web name="English" url="/en-us">
 5       <List name="Pages" property="Display Priority" value="Medium"/>
 6     </Web>
 7     <Web name="News" url="/en-us/News">
 8       <List name="Pages" property="Display Priority" value="Medium"/>
 9     </Web>
10     <Web name="Company" url="/en-us/company">
11       <List name="Pages" property="Display Priority" value="Medium"/>
12     </Web>
13     <Web name="Company" url="/en-us/Misc">
14       <List name="Pages" property="Display Priority" value="Medium"/>
15     </Web>
16   </Webs>
17 </SiteCollection>
原文地址:https://www.cnblogs.com/daishuguang/p/3024026.html