question

gogi100 avatar image
0 Votes"
gogi100 asked YiLu-MSFT commented

powershell for updating columns values in document library

i have a the powershell script where i want that update columns of documents in folders based on column values of folders in whom those files exists. my code is but he is wrong, where is error

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
  ####
  #### Update sharepoint field
  ###
     
 #Get the List, update fields
 $spSite = Get-SPSite -Identity http://test-net.dri.local
 $website = $spSite.OpenWeb()
 $list = $website.Lists["Лична акта"]
 $items = $list.Items
    
 foreach($folder in $list.Folders)
 {
 $imeiprezime=$folder["Име и презиме"]
 $korisnickoime=$folder["Корисничко име"]
 $eposta=$folder["Е-пошта"]
 foreach($item in $folder.LisItems)
 {
     if($item["Име и презиме"] -eq "")
     {
         $item["Име и презиме"] = $imeiprezime
         $item.Update()
     }
     if($item["Корисничко име"] -eq "")
     {
         $item["Корисничко име"] = $korisnickoime
         $item.Update()
     }
     if($item["Е-пошта"] -eq "")
     {
         $item["Е-пошта"] = $eposta
         $item.Update()
     }
    
 }
 }
 $website.Dispose()
office-sharepoint-server-developmentsharepoint-devoffice-sharepoint-server-customization
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

i tryed with next code

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
  ####
  #### Update sharepoint field
  ###
     
 #Get the List, update fields
 $spSite = Get-SPSite -Identity http://test-net.dri.local
 $website = $spSite.OpenWeb()
 $list = $website.Lists["Лична акта"]
 $items = $list.items
    
 foreach($folder in $list.Folders)
 {
     $imeiprezime=$folder["Име и презиме"]
     $korisnickoime=$folder["Корисничко име"]
     $eposta=$folder["Е-пошта"]
     $query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
     $query.Folder = $folder.Folder
     $folderItems = $list.GetItems($query)
     foreach($item in $folderItems)
     {
     if($item["Име и презиме"] -eq "")
         {
             $item["Име и презиме"] = $imeiprezime
               
         }
         if($item["Корисничко име"] -eq "")
         {
             $item["Корисничко име"] = $korisnickoime
              
         }
         if($item["Е-пошта"] -eq "")
         {
             $item["Е-пошта"] = $eposta
                
         }
         $item.Update()
    
     }
 }
 $website.Dispose()


but variable $item cannot update columns. how customize my code

0 Votes 0 ·

1 Answer

YiLu-MSFT avatar image
0 Votes"
YiLu-MSFT answered YiLu-MSFT commented

Hi @gogi100
You could try below code:

 Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
  $Web = Get-SPWeb "http://test-net.dri.local"
    
  $list = $web.Lists["luyidoc"]
  $items = $list.Items
        
  foreach($folder in $list.Folders)
  {
  $imeiprezime=$folder["luyi"]
  $eposta=$folder["luyi2"]
     $query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
     $query.Folder = $folder.Folder
     $folderItems = $list.GetItems($query)
     foreach($item in $folderItems)
  {
       if($item["luyi"] -eq $null)
     { 
          $item["luyi"] = $imeiprezime
          $item.Update()
      }
   if($item["luyi2"] -eq $null)
     { 
          $item["luyi2"] = $eposta
          $item.Update()
      }
   } 
 }

Here you need to change the parameter as your own need.


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @gogi100
I am checking to see if the problem has been resolved.
If you have any questions or progress, you can contact me in time.

0 Votes 0 ·

Hi @gogi100
Have you tried the script I provided in the answer? Did it work for you? Please feel free to reply.

0 Votes 0 ·