SharePoint 2010 PowerShell 系列 之 Create List and Field

时间:2022-02-27 08:55:08

学习目录

本篇才是重点,包含了基本的字段类型创建,具体情况看代码了,代码比文字更容易理解

# Check to ensure Microsoft.SharePoint.PowerShell is loaded
$Snapin = get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if($Snapin -eq $null){
    Write-host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
#SPListTemplateType
$DocTemp = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$ContactsTemp = [Microsoft.SharePoint.SPListTemplateType]::Contacts
$CustomGridTemp = [Microsoft.SharePoint.SPListTemplateType]::CustomGrid 
$GenericListTemp = [Microsoft.SharePoint.SPListTemplateType]::GenericList

#SPFieldType
$TextFieldType = [Microsoft.SharePoint.SPFieldType]::Text 
$NoteFieldType = [Microsoft.SharePoint.SPFieldType]::Note
$ChoiceFieldType = [Microsoft.SharePoint.SPFieldType]::Choice
$NumberFieldType = [Microsoft.SharePoint.SPFieldType]::Number

#SPAddFieldOptions
$DefaultViewFieldOptions = [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView

# Get Web
$WebUrl = "http://win-kpmmoq5srnd:201"
$Web = Get-SPWeb $WebUrl

# Add List
Function AddList($web,$listName,$listDescription,$template){
    Write-Host "Starting Creat List" $listName "please waiting......" -foregroundcolor Yellow
    Write-Host ".............................."
    $web.Lists.Add($listName,$listDescription,$template)
    Write-host "List" $listName "already is created"  -foregroundcolor Green
}

# Delete List
Function DeleteList($web,$listName){
    $list = $web.Lists.TryGetList($listName)
    if($list -ne $null){
        Write-Host "List" $listName "already exists,deleting......" -foregroundcolor Yellow
        Write-Host "..........................."
        $list.Delete()
        Write-host "List" $listName "already is deleted" -foregroundcolor Green
    }
}

# List OnQuickLaunch
Function OnQuickLaunchList($list,$bool){
    $list.OnQuickLaunch = $bool
    $list.Update()
}

#Add Fields
Function AddField($list,$xml,$bool,$fieldOptions){
    $list.Fields.AddFieldAsXml($xml,$bool,$fieldOptions)
}

Function AddList_Contact($web){
    $listName = "PowerShell List"
    $listDescription = "PowerShell"
    DeleteList $web $listName
    AddList $web $listName $listDescription $ContactsTemp    
    $list = $web.Lists.TryGetList($listName);
    OnQuickLaunchList $list True
}
Function AddList_CustomList($web){
    $listName = "Test"
    $listDescription = "Test"
    DeleteList $web $listName
    AddList $web $listName $listDescription $GenericListTemp
    $list = $web.Lists.TryGetList($listName)
    OnQuickLaunchList $list True
    AddListField_CustomList $web $list $listName
}
Function AddListField_CustomList($web,$list,$listName){
    $textXml="<Field Type='Text'  DisplayName='你好' Required = 'FALSE' MaxLength='255' StaticName='Hello' FromBaseType='TRUE' Name='Hello'/>"
    AddField $list $textXml $false $DefaultViewFieldOptions
    
    $noteXml="<Field Type='Note' DisplayName='Multi' Required = 'FALSE' RichText='TRUE' RichTextMode='Compatible'/>"
    AddField $list $noteXml $FALSE $DefaultViewFieldOptions
    
    $noteXml1="<Field Type='Note' DisplayName='Multi1' Required = 'FALSE' RichText='TRUE' RichTextMode='FullHtml'/>"
    AddField $list $noteXml1 $FALSE $DefaultViewFieldOptions
    
    $noteXml2="<Field Type='Note' DisplayName='Multi2' Required = 'FALSE' RichText='TRUE'/>"
    AddField $list $noteXml2 $FALSE $DefaultViewFieldOptions
    
    $noteXml3="<Field Type='Note' DisplayName='Multi3' Required = 'FALSE' RichText='FALSE'/>"
    AddField $list $noteXml3 $FALSE $DefaultViewFieldOptions
    
    $choiceXml = "<Field Type='Choice' DisplayName='Choice' Required = 'FALSE' Format='Dropdown'>
                    <Default>First</Default>
                    <CHOICES>
                        <CHOICE>First</CHOICE>
                        <CHOICE>Second</CHOICE>
                    </CHOICES>
                 </Field>"
    AddField $list $choiceXml $FALSE $DefaultViewFieldOptions
    
    $choiceRadioButtonXml = "<Field Type='Choice' DisplayName='ChoiceRadioButton' Required = 'FALSE' Format='RadioButtons'>
                    <Default>First</Default>
                    <CHOICES>
                        <CHOICE>First</CHOICE>
                        <CHOICE>Second</CHOICE>
                    </CHOICES>
                 </Field>"
    AddField $list $choiceRadioButtonXml $FALSE $DefaultViewFieldOptions
    
    $choiceCheckXml = "<Field Type='MultiChoice' DisplayName='Checked' Required = 'FALSE'>
                    <Default>First</Default>
                    <CHOICES>
                        <CHOICE>First</CHOICE>
                        <CHOICE>Second</CHOICE>
                    </CHOICES>
                 </Field>"
    AddField $list $choiceCheckXml $false $DefaultViewFieldOptions
    
    $numberXml="<Field Type='Number' DisplayName='Number' Required = 'TRUE' Max='100' Min='1' Decimals='2'/>"
    AddField $list $numberXml $FALSE $DefaultViewFieldOptions
    
    $currencyXml="<Field Type='Currency' DisplayName='Currency' Required = 'TRUE' Decimals='1' LCID='1033' />"
    AddField $list $currencyXml $FALSE $DefaultViewFieldOptions
    
    $DateXml="<Field Type='DateTime' DisplayName='DateOnly' Required = 'FALSE' Format='DateOnly'/>"
    AddField $list $DateXml $FALSE $DefaultViewFieldOptions
    
    $DateTimeXml="<Field Type='DateTime' DisplayName='DateTime' Required = 'FALSE' Format='DateTime'/>"
    AddField $list $DateTimeXml $FALSE $DefaultViewFieldOptions
    
    $DateTimeISO8601Xml="<Field Type='DateTime' DisplayName='ISO8601' Required = 'FALSE' Format='ISO8601'/>"
    AddField $list $DateTimeISO8601Xml $FALSE $DefaultViewFieldOptions
    
    $BooleanXml="<Field Type='Boolean' DisplayName='Boolean' Required = 'FALSE'>
                    <Default>1</Default>
                </Field>"
    AddField $list $BooleanXml $FALSE $DefaultViewFieldOptions
    
    $BooleanDefaultXml="<Field Type='Boolean' DisplayName='BooleanDefault' Required = 'FALSE' Default = '2'/>"
    AddField $list $BooleanDefaultXml $FALSE $DefaultViewFieldOptions
    
    $userXml="<Field Type='User' DisplayName='User' Required = 'FALSE' UserSelectionMode='1' />"
    AddField $list $userXml $FALSE $DefaultViewFieldOptions
    
    $userMultipleXml="<Field Type='UserMulti' DisplayName='UserMulti' Required = 'FALSE' Mult='TRUE' />"
    AddField $list $userMultipleXml $FALSE $DefaultViewFieldOptions
    
    $urlXml="<Field Type='URL' DisplayName='URL' Required = 'FALSE' Format='Hyperlink'/>"
    AddField $list $urlXml $FALSE $DefaultViewFieldOptions
    
    $imgXml="<Field Type='URL' DisplayName='IMG' Required = 'FALSE' Format='Image'/>"
    AddField $list $imgXml $FALSE $DefaultViewFieldOptions
    
    $CalcXml="<Field Type='Calculated' DisplayName='Calc' Required = 'FALSE' Format='Image' Name='Internal_Calc' ResultType='Number' ReadOnly='TRUE'>
                <Formula>=Number*100</Formula>
                <FieldRefs>
                    <FieldRef Name='Number'/>
                </FieldRefs>
             </Field>"
    AddField $list $CalcXml $FALSE $DefaultViewFieldOptions   
    
    $CalcXml="<Field Type='Calculated' DisplayName='Calc1' Required = 'FALSE' Format='Image' Name='Internal_Calc1' ResultType='Currency' ReadOnly='TRUE' LCID='1033'>
                <Formula>=Number*Currency</Formula>
                <FieldRefs>
                    <FieldRef Name='Number'/>
                    <FieldRef Name='Currency'/>
                </FieldRefs>
             </Field>"
    AddField $list $CalcXml $FALSE $DefaultViewFieldOptions 
    
       
    $list.Update()
    #$list.Fields.Add("Test",$TextFieldType,$false)
    #$list.Fields.Add("Test_2",$TextFieldType,$false)
    #$list.Fields.Add("Test_Note",$NoteFieldType,$false)
    #$testChoice = New-Object System.Collections.Specialized.StringCollection
    #$testChoice.Add("First")
    #$testChoice.Add("Second")
    #$testChoice.Add("Third")
    #$list.Fields.Add("Test_Choice",$ChoiceFieldType,$false,$false,$testChoice)
    #$list.Fields.Add("Test_Number",$NumberFieldType,$false)
    
    #$viewUrl = "/Lists/"+$listName+"/AllItems.aspx"
    #Write-Host $viewUrl
    #$allView = $web.GetViewFromUrl("/Lists/Test/AllItems.aspx")
    
    #$fieldChoice = $list.Fields["Test_Choice"]
    #$allView.ViewFields.Add($fieldChoice)
    #$allView.Update()
}

if($Web -ne $null){
    AddList_Contact $Web
    AddList_CustomList $Web
    write-host 
    $Web.Dispose()
}
else{
    Write-Host $WebUrl " is not existing"
}

参考文章