7

I have a simple list with an event receiver for the following events:

  • ItemAdded
  • ItemUpdating
  • ItemDeleting

I confirmed that the event receiver fires when adding/updating/deleting an item from the SharePoint user interface.

However, it doesn't fire when I create the list item from a PowerShell script. The item is added to the list but the event receiver doesn't fire. Any thoughts on why this is?

Here's the relevant snippet of the PowerShell script:

# read offices from Data-Offices.xml
$officeData = [xml](Get-Content .\data\Data-Offices.xml)

# iterate through items and create them in Office list
foreach ($office in $officeData.offices.office)
{   
    $newItem = $list.items.Add();
    $newItem["Title"] = $office.title;
    $newItem.Update()
    Write-Host "Added Office: " $office.title
}

Wondering if there's a permissions issue here ... Tried running the script as an administrator but that didn't resolve the issue.

Thank you

3
  • What permissions are you running your cmd-let with? Make sure it has the privileges to execute the code in question (eg. full control of web application). Also, I notice sometimes closing the shell and re-open works (not sure about your situation). Commented Nov 9, 2011 at 18:45
  • 3
    Figured it out ... The PowerShell script is running in a different context, and it's not able to find a web.config setting that our SharePoint application uses (appSettings for the ConfigStore project). Commented Nov 9, 2011 at 19:55
  • I have the same problem you desrcibed. How did you solve it ? Thank you
    – user7177
    Commented Mar 1, 2012 at 15:09

1 Answer 1

1

Precede your code block with something similar to the following to obtain the same context for the event handler to fire -

$fooWebURL = "http://fooWebSite"; 
$fooWeb = Get-SPWeb -Identity $fooWebURL; 
$fooFolder = $fooWeb.GetFolder("Documents"); 
$fooFileCollection = $fooFolder.Files;

then merge & iterate thru files upload..

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.