0
votes

I am trying to update a Sharepoint 2010 record using ByPassLock method in Powershell.
I am using below code

  [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
  [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.RecordsManagement.RecordsRepository.Records")
  [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")
  $modDate = {
      param(
          [Microsoft.SharePoint.SPListItem]$a
      )
      $a["Title"] = "NewTitle"
      $a.SystemUpdate($true)
  }
  $web = Get-SPWeb "http://mySiteCollection/"
  $item = $web.Lists["Dokument"].GetItemById(10)
  [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::BypassLocks($item,$modDate)

After execution of the script i get below error

Exception calling "BypassLocks" with "2" argument(s): "Attempted to perform an unauthorized operation."
+ [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::BypassLocks <<<< ($item,$modDate)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException



I running this script as administrator and have site collection administrator access on the site. I removed item update part but still it is not working.
Can Someone help me with this

1

1 Answers

0
votes

It worked when I executed under token of the System Account for the site collection.

    #Get Systrem User ID
    $sysId = $web.Site.SystemAccount.ID
    $sysuser = $web.AllUsers[$sysId]
    $token = $sysuser.Token

    #Create New Site Object under Token of System Account for the Site Collection
    $newSiteObject = New-Object Microsoft.SharePoint.SPSite($web.Url, $token)
    $newWebObject = $newSiteObject .OpenWeb()
    $item = $newWebObject.GetListItem($fileurl)        
    [Microsoft.Office.RecordsManagement.RecordsRepository.Records]::BypassLocks($item, $byPassMethodName)