Thursday, May 9, 2013

Sharepoint 2010 disable event firing using Powershell script



Here is a code:


#Add SharePoint PowerShell SnapIn if not already added
 if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$web = Get-SPWeb "http://mysharepoint.com/mypage/"


$list = $web.Lists.TryGetList("<ListName>")

$item = $list.Items.GetItemById("<ItemID>")

   try
   {

Write-Host "Deleting" $item.Title;
$myAss = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
    $type = $myAss.GetType("Microsoft.SharePoint.SPEventManager");
    $prop = $type.GetProperty([string]"EventFiringDisabled",[System.Reflection.BindingFlags] ([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static));

$prop.SetValue($null, $true, $null);
$prop.GetValue($null,$null)
$item.Title;
     $item.Delete()
    Write-Host  $item.Title "was deleted";

$prop.SetValue($null, $false, $null);
$prop.GetValue($null,$null);
$prop.GetValue(
}
catch
{
     
}

No comments:

Post a Comment