Add items to sharepoint list

17/12/2012 11:38

Add items to simple sharepoint list with 'Title' column only.


# ListItemExists
# GetListItemByTitle
# GetPageByTitle
# AddItemsToSimpleList

function ListItemExists
{
    param ([object]$sourceList, [string]$value)

    try
    {        
    #prepare lookup value-getting Id of 'Section' from another list
    $spQuery = new-object Microsoft.SharePoint.SPQuery
    $camlQuery = "<Where> 
     <Eq>   
     <FieldRef Name='Title' /> 
           <Value Type='Text'>"+$value+"</Value>
           </Eq>
           </Where>"
           $spQuery.Query = $camlQuery
           $spQuery.RowLimit = 1
      
    $spListItemCollection = $sourceList.GetItems($spQuery)
    if($spListItemCollection.Count -ne 0)
        {
         write-host "Item '" $value "' is already present in the list " $sourceList.Title
         return $true
         }
     else
     {
      write-host "Item '" $value "' is not in the list " $sourceList.Title
        return $false
       
     }
    }
    catch [Exception]
    {
        write-error $_
        return $false
    }           
 }
 
 
 function GetListItemByTitle
{
    param ([object]$sourceList, [string]$title)

    try
    {        
    #prepare lookup value-getting Id of 'Section' from another list
    $spQuery = new-object Microsoft.SharePoint.SPQuery
    $camlQuery = "<Where> 
     <Eq>   
     <FieldRef Name='Title' /> 
           <Value Type='Text'>"+$title+"</Value>
           </Eq>
           </Where>"
           $spQuery.Query = $camlQuery
           $spQuery.RowLimit = 1
      
    $spListItemCollection = $sourceList.GetItems($spQuery)
    if($spListItemCollection.Count -ne 0)
        {
         #write-host "Item '" $title "' is already present in the list " $sourceList.Title
         return $spListItemCollection[0]
         }
     else
     {
      #write-host "Item '" $title "' is not in the list " $sourceList.Title
        return $null
       
     }
    }
    catch [Exception]
    {
        write-error $_
        return $null
    }           
 }
 
  function GetPageByTitle
{
    param ([Microsoft.SharePoint.Publishing.PublishingWeb]$pweb, [string]$title)

    try
    {
   
     
        #convert title to name
       
        #regex to find illegal characters in fileName
        $pattern = "[{0}]" -f ([Regex]::Escape([String][System.IO.Path]::GetInvalidFileNameChars()))
        $name=[Regex]::Replace($title, $pattern, '-') + ".aspx"
       
       
        #Get Publishing Page
        $page=$pWeb.GetPublishingPage("Paginas/" + $name)       
        if($page -ne $null)
        {
          #write-host "Page '" $name "' is already present in the 'Pages' library."
         
        }
        else
        {
          #write-host "Page '" $name "' is not in the 'Pages' library."
  
        }
        return $page 
    }
    catch [Exception]
    {
        write-error $_
        return $null
    }           
 }
 
  function GetPageByName
{
    param ([Microsoft.SharePoint.Publishing.PublishingWeb]$pweb, [string]$name)

    try
    {
   
        $name = $name + ".aspx"
       
       
        #Get Publishing Page
        $page=$pWeb.GetPublishingPage("Paginas/" + $name)       
        if($page -ne $null)
        {
          #write-host "Page '" $name "' is already present in the 'Pages' library."
         
        }
        else
        {
          #write-host "Page '" $name "' is not in the 'Pages' library."
  
        }
        return $page 
    }
    catch [Exception]
    {
        write-error $_
        return $null
    }           
 }

Function AddItemsToSimpleList
{
    param ([string]$siteUrl, [string]$listName, [array]$values)
   
   
    #get List object
    try
    {
     $spWeb = Get-SPWeb -Identity $siteUrl
        $spWeb.AllowUnsafeUpdates=$true
     $spList = $spWeb.Lists[$listName]
     if ($spList -ne $null)
     {
            write-host "Creating sample values in " $listName " list..."
      $i=0
      foreach ($element in $values)
      {
                if(-not(ListItemExists -sourceList $spList -value $element))
                {
           $spListItem = $spList.AddItem()
           $spListItem["Title"] = $element
           $spListItem.Update()
           $i++
                }
      }
      write-host $i "items of list " $listName " were updated."
     }
        else
        {
      write-host "List " $listName " doesnt exist at " $siteUrl
     }
    }
    catch [Exception]
    {
     write-error $_
    }
    finally
    {
        $spWeb.Dispose()
    }
          
   
}