3

I have saved $item.ModerationInformation.Status information in HashTable $DeletedItemsIDs and recycled list items. After That I restored items from recycle bin and assign saved ModerationStatus information back to items, but items restored with "Pending" status. Whats wrong with this Powershell code? I can see the correct saved Moderation status info in variable $deleteditemId.Value

    // Saving Modeartion status info
  $DeletedItemsIDs = @{}

    foreach($item in $ListItems)
    {
       $DeletedItemsIDs.Add($item.ID,$item.ModerationInformation.Status)
       $DeletedItem = $item.recycle()       
       $DeletedItems.add($DeletedItem)
    }


// I do some stuff here and then restore as 

ForEach ($deletedGuid in $DeletedItems)
{
 $site_To_AddCT.RecycleBin.Restore($deletedGuid)
 $Recount++
}
    //Here assigning back moderation status info after restoring


      ForEach ($deleteditemId in $DeletedItemsIDs.GetEnumerator())
      {

       $Pageitem = $PagesLibrary.GetItemById($deleteditemId.Key)
       $Pageitem.File.CheckOut()
       $Pageitem["ContentTypeId"] = $Global:ctToAdd.Id
       $Pageitem.File.CheckIn("CheckIn automatically")
       $Pageitem.File.Publish("Published automatically")
       $Pageitem.ModerationInformation.Status = $deleteditemId.Value // whats wrong here
       $Pageitem.Update()
     } 
0

1 Answer 1

2

Add $Pageitem.Update() (or .SystemUpdate()) after assigning ModerationInformation.Status:

$Pageitem.Update();

You need to update the item after changing the metadata as well as after setting the Moderation Status. Your code should look like this:

$Pageitem = $PagesLibrary.GetItemById($deleteditemId.Key)
$Pageitem.File.CheckOut()
$Pageitem["ContentTypeId"] = $Global:ctToAdd.Id
$Pageitem.File.CheckIn("CheckIn automatically")
$Pageitem.File.Publish("Published automatically")
$Pageitem.Update(); //I would use SystemUpdate(false); this won't increase the file version.
$Pageitem = $PagesLibrary.GetItemById($deleteditemId.Key)
$Pageitem.ModerationInformation.Status = $deleteditemId.Value
$Pageitem.Update(); //Maybe SystemUpdate(false) here as well, depending on the requirements
7
  • I have added $Pageitem.Update(), but then i got this exception: Exception calling "Update" with "0" argument(s): "You cannot change moderation status and set other item properties at that same time." At C:\Users\administrator\Desktop\Test.ps1:127 char:37
    – SPBeginer
    Mar 1, 2016 at 11:18
  • After changing the metadata do an .Update() as well, then change the moderation information and do an .Update() again. Post the rest of your code please. Mar 1, 2016 at 11:32
  • code updated now, please have a look
    – SPBeginer
    Mar 1, 2016 at 11:38
  • Updated my answer. Mar 1, 2016 at 12:03
  • Thats work, but now I got another exception at first .Update(). Exception calling "Update" with "0" argument(s): "The file "somefile.aspx" is not checked out. You must first check out this document before making changes." ... But i have checkedout as you see in code
    – SPBeginer
    Mar 1, 2016 at 12:11

Your Answer

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

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