1

I have associated a custom content type to a document library and this content type as multiple fields.

The FieldXYZ termset from which this field is derived has 2 values ('Success' , 'Failed').

In my PowerShell script, I am updating a field FieldXYZ value for all documents in the root directory of the library.

The PowerShell code for updating the field is as follows:

    $lists = $web.lists
foreach ($list in $lists) 
{
 if ($list.BaseType -eq [Microsoft.SharePoint.SPBaseType]::DocumentLibrary) 
 {
    write-host "Document Lib : " , $list.title
        SetFieldValuebyGUID($list.RootFolder)   
        SetFieldValuebyVal($list.RootFolder)    
 }
}




Function SetFieldValuebyGUID($rootfolder)
    { 
      foreach($docfile in $rootfolder.Files)
      { 
        write-host $docfile.Properties["FieldXYZ"]
            $docfile.CheckOut()
        $docfile.Properties["FieldXYZ"] = n5f52e7f-bb83-4c01-xxxxxxxxx
        $docfile.Update();
        $docfile.CheckIn('updated property GUID')
            write-host $docfile.Properties["FieldXYZ"]
         }
       }


Function SetFieldValuebyVal($rootfolder)
    { 
      foreach($docfile in $rootfolder.Files)
      { 
        write-host $docfile.Properties["FieldXYZ"]
            $docfile.CheckOut()
        $docfile.Properties["FieldXYZ"] = "Success"
        $docfile.Update();
        $docfile.CheckIn('updated property GUID')
            write-host $docfile.Properties["FieldXYZ"]
         }
       }

The script is not updating the values of the field.

Any ideas on how to resolve the issue?

1 Answer 1

1

There are two things you should change in your script:

  1. You are using $file.Properties which are not exactly SPListItem metadata. You should use $file.Item["fieldname"] = ... To change document metadata.

  2. As it seems you are trying to set a taxonomy field that is not so straightforward. You should look,at this blog post on how to set taxonomy field using powersell http://blogs.msdn.com/b/kaevans/archive/2012/02/15/updating-sharepoint-managed-metadata-columns-with-powershell.aspx

5
  • I have tried it by docfile.item [field name ] = value and docfile.item.update () , that was not updating the value.
    – Daniel
    Commented Jul 29, 2015 at 10:37
  • Is it taxonomy field? If yes you should try set according to sample specified in the link.
    – ECM4D
    Commented Jul 29, 2015 at 12:14
  • Yes, it was a metadata field (part of custom contenttype) The field is also tried to termset. I will try the termset option
    – Daniel
    Commented Jul 29, 2015 at 19:48
  • One thing I dont understand is, if I am using foreach file in folder, it does not update the data for parameter, when I use the foreach on listitems, update function will work. Is there anyway to update items in a specific folder? foreach ($file in folderparam.files) { $file.Item["parameter"] = GUID $file.Item.Update() } ================ foreach ($item in $list.items) { $item["parametr"] = GUID $item.Update() }
    – Daniel
    Commented Aug 2, 2015 at 4:45
  • In thtis case, I would suggest, to make a CAML query on your list to get ListItems from specific folder and then make foreach through ListItemCollection. So make caml query on specific folder you can set SPQuery.Folder property, like showed in the anwer of this post sharepoint.stackexchange.com/questions/59216/…
    – ECM4D
    Commented Aug 2, 2015 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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