Question

I am new to Sharepoint. I have an EventReceiver hooked to an ItemUpdated event and I want to write a text in a field. When I upload a file the event triggers ok, it goes through the code on debugging, seems to update but my property does not receive the text it's supposed to. However after I hit refresh on the page, I can see the updated value.

Here's my code

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);

        string folderPath = string.Empty;
        SPListItem item = properties.ListItem;
        if (item.File.ParentFolder != null)
        {
            folderPath = item.File.ParentFolder.ServerRelativeUrl;
        }

        AssignPropertyToField("Folder Name", item, folderPath);
    }        

    private void AssignPropertyToField(string fieldName, SPListItem item, string folderPath)
    {
        item[fieldName] = folderPath;

        this.EventFiringEnabled = false;
        item.SystemUpdate();
        this.EventFiringEnabled = true;
    }

Thank you in advance for suggestions,

Regards,

Was it helpful?

Solution

If possible, try ItemUpdating instead of ItemUpdated.

Since ItemUpdated is asynchronous, you should not count on it being called before the page is refreshed. With ItemUpdating, note that the list item has not yet been saved so you do not need to call SystemUpdate.

public override void ItemUpdating(SPItemEventProperties properties)
{
    string folderPath = string.Empty;
    SPListItem item = properties.ListItem;
    if (item.File.ParentFolder != null)
    {
        folderPath = item.File.ParentFolder.ServerRelativeUrl;
    }
    properties.AfterProperties["FolderNameInternalName"] = folderPath;
}        

In your case, the question is going to be whether you will be able to retrieve the updated parent folder information within the ItemUpdating event. My sample code above will take the previously existing folder information. This code will give you the wrong URL if the file is being moved to a different folder.

OTHER TIPS

You can call item.Update() instead of item.SystemUpdate()

Note that this way the ItemUpdated event handler will be called twice, so you need to make sure to only do the update if item[fieldName] is different from folderPath in AssignPropertyToField, to avoid infinite loop.

What you can do, is to define in the elements.xml in the definition of the ItemUpdated receiver that it should be run synchronous. See this http://msdn.microsoft.com/en-us/library/ff512765.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top