0

I can't find anything on google on this. I've created an item as follows:

$item = $supportPackList.Items.Add()
$item["Title"] = $supportPackName
$item["Job No (Nav)"] = "NOT MAPPED TO NAV"
$item["SupportPackTemplateLookup"] = $supportPackTemplateId
$item["ClientLookup"] = $clientTemplateId
$item["Description (Nav)"] = "NOT MAPPED TO NAV"
$item.Update()

This works. I can see it through the UI and also change properties/save it again. Now, after creating the item, this works:

$suppPackList.GetItemById(22).Title

I am then trying to update it through powershell. However, this simple line;

$suppPackList.GetItemById(22).Update()

Or even

$suppPackList.GetItemById(22).SystemUpdate()

Gives me:

Exception calling "SystemUpdate" with "0" argument(s): 
"System.ArgumentException: Item does not exist. It may have been deleted by 
another user.
   at Microsoft.SharePoint.SPList.GetItemById(String strId, Int32 id, String 
strRootFolder, Boolean cacheRowsetAndId, String strViewFields, Boolean 
bDatesInUtc, Boolean bExpandQuery)
   at Microsoft.SharePoint.SPList.GetItemById(String strId, Int32 id, String 
strRootFolder, Boolean cacheRowsetAndId, String strViewFields, Boolean 
bDatesInUtc)
   at Microsoft.SharePoint.SPList.GetItemById(String strId, Int3"
At line:1 char:1
+ $suppPackList.GetItemById(22).SystemUpdate()

Of course it exists, you just gave me it's Title! Note that updating the item from the Edit Form in the UI by clicking Save works. I have some event receiver code. Here's the gist of it, but technically, if I can do something through the UI I should be able to do it via power shell right?

public override void ItemUpdating(SPItemEventProperties properties)
{
    using (DisabledEventsScope scope = new DisabledEventsScope())
    {


        SPL_SupportPack supportPack = new SPL_SupportPack(properties.AfterProperties);

        try
        {

            base.ItemUpdating(properties);
            UpdateExistingSPSupportPack(supportPack, properties.Web);
        }
        catch (Exception e)
        {
            //SPUtility.TransferToErrorPage(e.ToString());
            properties.ErrorMessage = e.ToString();
            properties.Status = SPEventReceiverStatus.CancelWithError;
        }
    }
}

One more thing, I have my debugger attached to all w3wp.exe processes. Updating through UI triggers the ItemUpdating(), but through power shell doesn't (for both .Update() and .SystemUpdate().

I've been tearing my hair over this, I must be missing something very basic. Any ideas?

4

2 Answers 2

1

Found the problem thanks to Paul's suggestion of attaching to PowerShell instead of w3wp (as I thought the code still runs in the IIS worker process even though PowerShell is initiating the call).

The problem was; SPItemEventProperties.AfterProperties is empty after the update, but only from powershell, causing my event receiver to complain. Weird.

2
  • Very interesting finding... Maybe this is because AfterProperties are empty if they are not changed? Sep 17, 2015 at 17:15
  • That's what I thought. But they are filled up even if unchanged from a normal OOTB SharePoint form!
    – David
    Sep 17, 2015 at 19:22
0

Just a note: use $supportPackList.AddItem() instead of $supportPackList.Items.Add().

On topic, try:

$item = $suppPackList.GetItemById(22)
$item.Update()

instead of

$suppPackList.GetItemById(22).Update()

As you CAN NOT update an item in SPItemCollection directly. You need to reference it first.

Regarding debugger, you need to attach to powershell.exe process instead of w3wp.

4
  • 1
    In retrospect, $suppPackList.GetItemById(22).Update() obviously shouldn't do anything but it's just demonstrating a problem. Assigning the resulting object to a variable and then calling Update() on it shouldn't be any different than calling it directly, no?
    – David
    Sep 17, 2015 at 14:49
  • It's not just demonstrating a problem - it might be the problem. Getting a value from an item and updating the item itself within the collection are two totaly different things. Sep 17, 2015 at 15:16
  • ??? Functionally, $item = $suppPackList.GetItemById(22); $item.Update(); and $suppPackList.GetItemById(22).Update() are exactly the same.
    – David
    Sep 17, 2015 at 15:54
  • 1
    This is exactly where you are wrong, mate :) You can NOT directly update a SharePoint list item in a SPListItemCollection. Basically you need to reference the item you are going to update as an SPListItem and update that for it to work. Every day brings some kind of news! Sep 17, 2015 at 16:05

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.