Create/Delete list via powershell

來源:互聯網
上載者:User
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)           }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.