Tuesday, September 20, 2011

Create a List Using Powershell script

Below script loops through all site collections in the web application and it creates a list.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
$WebAppUrl = "servername"
$rootsite = new-object Microsoft.SharePoint.SPSite($WebAppUrl)
$spWebApp = $rootSite.WebApplication
$oRootweb = $rootSite.OpenWeb()

$ErrorLog = ""
$i = 0
foreach($site in $spWebApp.Sites)
{
        $RootSCWeb = $site.OpenWeb()           
        $siteURL = $RootSCWeb.Url
        $ErrorLog = $ErrorLog + $siteURL + "`n`n"
        $fileUrl=$null
            $IsTrue =$false
        Write-Host $RootSCWeb.Url
  
            if (($RootSCWeb -ne $null)  -and ($siteURL -ne $WebAppUrl))
           {
                        #if($siteURL  -eq  "http://w2msmossmgn02:33980/sites/P000736")
                        #{
                      $blogUrl = $siteURL + "/TeamBlog"
                      $blogSite = New-Object Microsoft.SharePoint.SPSite($blogUrl)
                      $blogWeb = $blogSite.OpenWeb() 
                             
                              if ($blogWeb -ne $null)
                              {
                                    $PhotosList =  $blogWeb.Lists.TryGetList("Photos")
                                    if( $PhotosList -ne $null)
                                    {
                                          Write-Host  $PhotosList.Title
                                          #delete if list exist
                                          $PhotosList.Delete()
                                          $SPTemplate = $blogWeb.ListTemplates["OPS Project Pictures"]
                                          $blogWeb.Lists.Add("Photos","Share a picture with the team by adding it to this picture library",$SPTemplate)
                                    }
                              }
                        $RootSCWeb.Close()
                              break
                  #}
             }
       
        $site.Close()
        $site.Dispose()
           
}

    if($ErrorLog.Length -gt 0)
    {
        $ErrorLog =  " Exceptions `n`n" + $ErrorLog
       [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($oRootweb, $true, $true, "mailid", "SharePoint Site Update Patch - Log", $ErrorLog)   
    }
     

No comments:

Post a Comment

Followers