Create/Delete list via powershell

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)
           }
    }



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值