Create/Delete list via powershell

来源:互联网 发布:帝国cms邀请码 编辑:程序博客网 时间:2024/06/15 22:00

1. Create list by custom list template

    #***************************************************************************************************************
    #  Create a list by the custom list template  

    #  $spSiteUrl: The site url

    #   $spTemplate: The name of list template

    #   $spListName: The name of the list      

    #   $spDescription: Th edescription about the new list   

    #  $quickLaunchView: Show the list in quicklaunch true or false                                    
    #  Example: CreateCustomList http://localhost  "MyTemplate" "MyNewList2" "This is my list" "true"
    #***************************************************************************************************************

function CreateCustomList($spSiteUrl, $spTemplate, $spListName, $spDescription, $quickLaunchView)    {        $spSite = Get-SPSite $spSiteUrl.trim()        $spWeb = $spSite.RootWeb                $spTemplate = $spTemplate.trim()        $spListName = $spListName.trim()        $quickLaunchView = $quickLaunchView.trim()                # Check the name is null or not        if($spTemplate -eq $null -or $spListName -eq $null -or $quickLaunchView -eq $null)        {           write-host "The parameter is null"           return         }                try{             #  Get all the custom template     $listTemplates = $spSite.GetCustomListTemplates($spWeb);                              # check the list name is right or not               if(!($spListName -match "\w+"))              {                write-host "The list name is wrong"                return               }                            #  Check list is exist or not              foreach($list in $spWeb.Lists)              {                    if($list.Tostring() -eq $spListName)                        {                            write-host "The list named $list is existing"                            return                        }              }                            # check the template name is right or not               if(!($spTemplate -match "\w+"))              {                write-host "The template name is wrong"                return               }                            # Check the $quickLaunchView is right or not              if($quickLaunchView -ne "true" -and $quickLaunchView -ne "false")              {                write-host "The $quickLaunchView is not right choice"                return               }                            # check the template name is existing or not               foreach($template in  $listTemplates)              {                if($template.name -eq $spTemplate)                {                           #  Create the list via the template name            $spWeb.Lists.Add($spListName, $spDescription, $listTemplates[$spTemplate])                   $list = $spWeb.Lists[$spListName]                             #  Make the list view in OnQuickLaunch                  if($quickLaunchView -eq "true")                  {              $list.OnQuickLaunch = "True"                  $list.Update()                  }                  if($quickLaunchView -eq "false")                  {              $list.OnQuickLaunch = "False"                  $list.Update()                  }                  write-host "Create the list success"                  return                }             }             write-host "The template named $spTemplate is not existing"             return            }      catch{                write-host "(ERROR : "$_.Exception.Message")"                throw           }    finally{               $spWeb.Dispose()               $spSite.Dispose()           }     }

 

2. Delete the list by name

    #***************************************************************************************************************
    #  Delete the  list by name

    #  $spSiteUrl: The site url

    #   $spTemplate: The name of list template

    #   $spListName: The name of the list     

    #   $spDescription: Th edescription about the new list  

    #  $quickLaunchView: Show the list in quicklaunch true or false                                   

    #  Example : DeleteListByName http://localhost "MyNewList2" 
    #***************************************************************************************************************

function DeleteListByName($siteUrl, $spListName)    {        $spSite = Get-SPSite -identity $siteUrl.trim()         $spWeb = $spSite.RootWeb        try{              $spListName = $spListName.trim()               # check the list name is right or not               if(!($spListName -match "\w+"))              {                write-host "The list name is wrong"                return               }                            #  Check list is exist or not              foreach($list in $spWeb.Lists)              {                  if($list.Tostring() -eq $spListName)                  {                     $list.Delete()                     write-host "Delete the list named $spListName success"                     return                 }              }              write-host "The list name $spListName is not existing"              return            }      catch{                write-host "(ERROR : "$_.Exception.Message")"                throw           }    finally{               DisposeWebObject($spWeb)           }    }



 

原创粉丝点击