ItemAdded event does not fire

I run the powershell script for adding list item. I've executed the script with site collection admin right and other user (distribute) but ItemAdded event doesn't get fired. 

Please let me know what I am missing. 
Windows 2008 R2 / Sharepoint 2010
Many thanks

$web = Get-SPWeb $WebUrl
$list = $web.Lists[$ListName]
$newItem = $list.Items.Add() 

$assem = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$type = $assem.GetType("Microsoft.SharePoint.SPEventManager");
$prop = $type.GetProperty([string]"EventFiringDisabled",[System.Reflection.BindingFlags] ([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static));
$prop.SetValue($null, $false, $null);

$newItem["Field1"] = "Field1"
$newItem["Field1"] = "Field2"
$newItem["Field1"] = "Field3"
$newItem["Editor"]=$web.EnsureUser("i:0#.f|adprovider|user1")
$newItem.Update()
 I determined that object properties in itemAdded function is null if I execute by powershell. What should I do?

public override void ItemAdded(SPItemEventProperties properties)




  • Edited by diepkv Thursday, September 10, 2015 3:08 AM
September 10th, 2015 2:05am

Hi,

According to your description, my understanding is that you want to trigger ItemAdded event receiver when adding new item using PowerShell Command.

I suggest you can create 3 field in the list and then you can set the 2 field value using PowerShell and then set another filed value in the ItemAdded event receiver to test if the event receiver has been triggered.

In my environment, I have 3 fields named "Custom_Column1","Custom_Column2","Custom_Column3", "Custom_Column1" and "Custom_Column2" fields are set by PowerShell and "Custom_Column3" is set by event receiver.

Here is a code snippet for your reference:

PowerShell Command add new item:

$weburl = "http://sp2013sps/sites/test2"
$listname = "List2"
$a = "Text1"
$b = "Text2"
$c = "Text3"


Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue  
$web = Get-SPWeb -Identity $webUrl
$list = $web.Lists[$listname]
$newItem = $list.items.add()
$newitem["Title"] = $a
$newitem["Custom_Column1"] = $b
$newitem["Custom_Column2"] = $c
$newitem.update()

Event Receiver code :

 public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SPListItem item = properties.ListItem;
            item["Custom_Column3"] = "CustomText";
            item.Update();
        }

Thanks

Best Regards

Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 3:02am

Thank you Jerry Zy for your response

Last time I determined that ItemAdded event is fired but object properties in itemAdded function always null if I execute by powershell. 

So, I cannot do as your guide due to that reason.

September 11th, 2015 10:37am

Hi,

I suggest you can create a new event receiver and tried with my code demo above to test if the event receiver can be executed correctly.

Thanks

Best Regards

Free Windows Admin Tool Kit Click here and download it now
September 13th, 2015 9:49pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics