Wednesday, July 10, 2013

Copy 'Name' field as is to the 'Title' Field SharePoint 2010 Document library

When you run below script it will update the 'name' field as is to the 'Title' for all existing files in the document library.
dont forget to change your list name

Auto Fill Title of Document in Document Library in SharePoint 2010



$URL = "http://spdev5:8002/sites/library"
$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
$fieldname="Title"
     
      foreach($list in $web.Lists)
      {
      if($list.BaseType -eq "DocumentLibrary")
        {
            $listname = $list.title
            if($list.title -eq "your list name") # update with your list name. if u remove this condition then it will apply to all libraires. 
            {
                  Write-Host $list.title
                  $msg = ("{0},{1}" -f "List:", "=========================== $listname ======================")
               $msg | out-file -FilePath $logfile  -append
           foreach($item in $list.items)
             {
                  $item[$fieldname] = $item["Name"]
                  $item.SystemUpdate();
                 
                  Write-Host $count". FileName:"   $item.url
                  $msg = ("{0},{1}" -f $count, "URL:" + $item.url)
                  $msg | out-file -FilePath $logfile  -append
                  $count++
              }
                  # $list.update()
         }
      }
   }
$web.Dispose()
$site.Dispose()

No comments:

Post a Comment

Followers