4

How can I call DisableEventFiring using PowerShell?

C# code like this :

Type typeInfo = typeof(SPEventReceiverBase); 
MethodInfo disable = typeInfo.GetMethod("DisableEventFiring", BindingFlags.Instance | BindingFlags.NonPublic);
SPEventReceiverBase receiver = new SPEventReceiverBase();
 disable.Invoke(receiver, null);

I need do something like this using PS

3 Answers 3

4

You Shouldn't.

DisableEventFiring is only meant to be used inside EventReceivers. The main purpose is to prevent looping when they update items.

If you really REALLY want then it's almost identical to you c# code:

$receiver = New-Object "Microsoft.SharePoint.SPEventReceiverBase"
$type = $receiver.GetType()
[System.Reflection.BindingFlags]$flags = [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic
$method = $type.GetMethod("DisableEventFiring", $flags)
$method.Invoke($receiver, $null)
0
4

Here's how I did it but in most situations it is best not to disable event firing outside the event handlier/receiver:

$web = Get-SPWeb -Identity http://yoursharepointweb #FILL IN WITH YOUR WEB URL
$list = $web.Lists["YOUR LIST"] #THE LIST NAME FOR YOUR LIST
$item = $list.GetItemById(YOUR NUMERIC LIST ITEM ID) #THE ITEM ID
$assembly = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$type = $assembly.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); #SET EVENT FIRING DISABLED. 
$item["YOUR COLUMN NAME"]= "YOUR VALUE"
$item.SystemUpdate($false)
$prop.SetValue($null, $false, $null); #SET EVENT FIRING ENABLED.

I use the above code in a situation where a workflow task is assigned to a "blank" user i.e. sometimes a user has no permissions on a SharePoint site but they are assigned a task by the initiator of the workflow. In this case the task is assigned to blank and the email alert is sent out but the Assigned To field on the task item remains blank.

2
  • How is this linked to the list or item?
    – PeterX
    Commented Jun 18, 2015 at 1:19
  • @PeterX I've tested it and looks like it's a setting for the process that PowerShell is run within because it doesn't affect events triggered from the UI at the same time. Commented Oct 28, 2016 at 14:52
0

You can call this function only from inside an event receiver but not via powershell.

If you want to prevent a event receiver from being fired, you can use a power shell script to remove it. http://blogs.msdn.com/b/sharepoint_bar__grill/archive/2011/12/12/10151733.aspx

Another option is to SharePoint Manager 2010 to remove the Event receiver. http://spm.codeplex.com

0

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.