Tuesday, July 16, 2013

Batch Update SharePoint List Items using Powershell

$URL = "http://spdev5:8002/"
$site = New-Object Microsoft.SharePoint.SPSite($URL)
$web = $site.openweb("")


#logging (it will create a .csv file where u r running this script)
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "CopyName2Title"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "S.No,Message"
$header | out-file -FilePath $logfile

$count=1



# New-Object
#$strPost =New-Object  System.Text.StringBuilder
$methodBuilder=New-Object  System.Text.StringBuilder

$list = $web.Lists["Current Studies Test"]
$items= $list.items
$guid = $list.ID
$strGuid = $guid.ToString()
$title="surya"
 $time = Get-Date -Format g

Write-Host  "Start Time " $time

#Write-Host "count" $items.count   
$strPost = "<?xml version=""1.0"" encoding=""UTF-8""?><ows:Batch OnError=""Return"">"
   foreach($item in $items)
   {
         $strPost += "<Method>"
        $strPost +=  "<SetList Scope=""Request"">" + $strGuid +"</SetList>"
      $strPost += "<SetVar Name=""ID"">"+ $item.ID +"</SetVar>"
      $strPost += "<SetVar Name=""Cmd"">Save</SetVar>"
        $strPost += "<SetVar Name=""owsfileref"">" + $item.File.ServerRelativeUrl + "</SetVar>"
      $strPost += "<SetVar Name=""urn:schemas-microsoft-com:office:office#Title"">" + $title +"</SetVar>"
        $strPost += "</Method>"
     
      $msg = ("{0},{1}" -f $count, "URL:" + $item.url)
      $msg | out-file -FilePath $logfile  -append
    
       $count++
       Write-Host       $count

     #if($count -eq 2000)
       #{
           
             $strPost += "</ows:Batch>"
     
              #Write-Host $strPost
             $strProcessBatch = $web.ProcessBatchData($strPost)
           
             Write-Host  "Updated at:" $time
            $msg = ("{0},{1}" -f $count, "Updated:" + $time)
            $msg | out-file -FilePath $logfile  -append
  
           
      # }


  }
 
 $web.Dispose()
$site.Dispose()

You can see related posts as well with c# and powershell

1 comment:

  1. there is error for this post, please verify it first, then follow it.

    ReplyDelete

Followers