2

I have an event receiver that sets a value of a field using properties.AfterProperties within ItemAdding.

This works fine when adding a folder (with a custom content type) within the browser (the content type might not be set at that point but the value is in the field), but when adding a folder via code (list.RootFolder.SubFolders.Add("some folder name") or via PowerShell, or even Client Object Model code), the event receiver fires but the value isn't being saved into the field.

Anyone else see this? I should mention it's within a Document Library.

SharePoint build number is: 14.0.7015.1000

Reproduction steps: Team Site with Shared Documents, custom column added called "TestColumn".

Event Receiver Code to reproduce:

/// <summary>
/// List Item Events
/// </summary>
public class TestReceiver : SPItemEventReceiver
{
    /// <summary>
    /// An item is being added.
    /// </summary>
    public override void ItemAdding(SPItemEventProperties properties)
    {
        base.ItemAdding(properties);

        if (properties.List.Title == "Shared Documents")
        {
            properties.AfterProperties["TestColumn"] = "Foo";
        }
    }        
}

PowerShell to reproduce:

$w = get-spweb http://sitecoll/
$w.Lists["Shared Documents"].RootFolder.SubFolders.Add("TestFolder").Update()
3

With thanks to Max Melcher, I found this workaround for creating Folders that writes AfterProperties to the item:

$w = get-spweb http://redacted
$list = $w.Lists["Shared Documents"]
$f= $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "test 2")
$f.Update()
  • 1
    glad it worked out. – Max Melcher Apr 20 '15 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.