0

I need powershell script to create a custom list named 'test' with Title, URL and an boolean field in the rootweb of every site collection.

1

Try this. I have tested it

    Add-PSSnapin Microsoft.Sharepoint.Powershell
    [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({ 
    $webApplicationURL = "http://xyz:4444/"
    $webApp = Get-SPWebApplication $webApplicationURL

    if($webApp -ne $null)
    {
      Write-Host "Web Application : " + $webApp.Name

       foreach($siteColl in $webApp.Sites)
        { 
         if($siteColl -ne $null)
          {


             Write-Host "Site Collection : " + $siteColl.Url
             $subWeb = $siteColl.RootWeb;
              write-host $subWeb
                if($subWeb -ne $null)
                  {



                   $spTemplate = $subWeb.ListTemplates["Custom List"]
                    $spListCollection=$subWeb.Lists
                     write-host $spListCollection

                    $spListCollection.Add("test","test",$spTemplate)
                    $path =  $subWeb.url.trim()

                    $spList = $subWeb.GetList("$path/Lists/test")
                    $spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
                    $spList.Fields.Add("Title",$spFieldType,$false)

                    $spList.Fields.Add("URL","URL",$false)

                     $spFieldType = [Microsoft.SharePoint.SPFieldType]::Boolean
                    $spList.Fields.Add("Boolean",$spFieldType,$false)


                    $Views = $spList.Views["All Items"]
                    $Views.ViewFields.Add("Title")
                    $Views.ViewFields.Add("URL")
                    $Views.ViewFields.Add("Boolean")
                    $views.Update()



                     $subWeb.Dispose()
                  }
               else
                  {
                    Echo $subWeb "does not exist"
                  }

                $siteColl.Dispose()

          }
     } 
     }


 else
      {       Echo $webApplicationURL "does not exist, check the WebApplication name"
      }Remove-PsSnapin Microsoft.SharePoint.PowerShell
Echo Finish
 }); 
1

Try this code. I have not tested it

$url = "http://yoursitecollection.com"
 try
 {
     $Site=Get-SPSite $url -ErrorAction SilentlyContinue
     $spWebApp = $Site.WebApplication
       
     foreach($spSite in $spWebApp.Sites)
     {
        $spWeb = $spSite.RootWeb
        $spTemplate = $spWeb.ListTemplates["Custom List"]
        $spListCollection=$spWeb.Lists

        #adding the new list to the list collection
        $spListCollection.Add("test","test",$spTemplate)


        #get the list to the list object 
        $spList = $spWeb.Lists.TryGetList("test")

        if($spList -ne $null)
        {
           #adding the field type(url) to the list 
           $spList.Fields.Add("WebLink","URL",$false)

           $spFieldType = [Microsoft.SharePoint.SPFieldType]::Boolean
           $spList.Fields.Add("BooleanColumnName",$spFieldType,$false)
           $spList.Update()
        }
        else
        {
            write-host -f yellow "test does not exist in the site"
        }
    }
 }
 catch
 {
     write-host "Unable to Extract Sitecollection List..." -foregroundcolor red
     break
 }
  • can we check if the list with name 'test' already exists? – variable Apr 8 '14 at 5:10
  • Yes. With TryGetList method. I put in my code – Alvmad Apr 8 '14 at 8:06
0

Here is the PowerShell code. Note that I have not tested it.

$contentWebAppServices = (Get-SPFarm).services |
? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

foreach($webApp in $contentWebAppServices.WebApplications)
{
    Write-Host "Web Application  : " $webApp.name    
    foreach($siteColl in $webApp.Sites)
    {
        // Create a list
        $ListName = "List Name"
        $ListDescription = "List Description"       
        $SPWeb = Get-SPWeb -Identity $siteColl.Url
        $ListTemplate = $SPWeb.ListTemplates["Custom List"]
        $listId = $SPWeb.Lists.Add($ListName, $ListDescription, $listTemplate)

        // Add field
        $spList = $web.Lists[$listId]
        $spFieldType = [Microsoft.SharePoint.SPFieldType]::Boolean
        $spList.Fields.Add("ColumnName",$spFieldType,$false)
        $spList.Update()
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.