views:

1105

answers:

4

Hi there.

I have written an event receiver that is activated on the 'ItemAdded' event of a 'Pages' list. The code runs fine, but my issue is this - during the call to my Sub ItemAdded, I want to change the value of a field belonging to the current list item (in this case an aspx page).

The idea is that I can configure the 'Title' field to be another value, which my event receiver configures, and by the time the user sees the page in edit mode, the new title will have been saved for the page. Here is my current attempt:

Public Overrides Sub ItemAdded(ByVal properties As Microsoft.SharePoint.SPItemEventProperties)

 Dim newItem As SPListItem = properties.ListItem
 Dim currentSiteTitle As String = properties.OpenWeb().Title

 UpdateItemTitle(newItem, currentSiteTitle)
 newItem.Update()
 'newItem.SystemUpdate()

End Sub

As you can see, I've tried both Update() and SystemUpdate(), but in each case, when the user tried to check in the page, they get a message that the page has been modified externally. Plus, when viewing the page, the title field value has not changed.

Is what I'm trying to do at all possible, or is there a better way?

Cheers. Jas.

+2  A: 

ItemAdd**ed** Name says it all. It a Asynchronous Event that happens after the Items has been added, so is the issue with your calse. I suggest you to hook the ItemAdding event unless you have reason not to do so.

Refer the Link for Details on Asynchronous & Synchronous

Kusek
Do note that in the ItemAdding event the listItem is not available. Use the AfterProperties[] in stead.
Paul-Jan
A: 

The best way to accomplish this is use the ItemAdd**ing** event. This will allow you to change the Title value before it is saved to the database. Trying to change them in ItemAdded is possible but will cause the headaches you are experiencing.

Have you tried ItemAdding?

Alex Angas
To both Alex and kusek, apologies for the delay in replying, but I've been moved onto another project for now. I would like to revisit this question later since I have not tired the ItemAdded event yet.
Jason Evans
A: 

changing the properties.afterproperties will get around the save conflict. note that the afterproperties is a hashtable, but you can access it so:

properties.AfterProperties["Title"] = "My New Title";

Chris Bain
I've been on another project for while, so I've not had the chance to check out your answer. I will get round to it, as I'd like to mark an answer for this question, just to keep things clean.
Jason Evans
A: 

Now following on from the above, I found that when I was working with a discussion group the only place I could change any fields in the list item was in the ItemUpdating method, where I could assign the new value into the properties.AfterProperties hash corresponding to the item name as mentioned previously.

Unfortunately this didn't seem to automatically be run when a new reply was added to the discussion ( maybe it is in other list related scenarios ) but if I put code into the ItemAdded method ( ItemAdding was not being triggered either ) I found that it was being run but that I couldn't change the item from there, so I ended up with something like this in itemAdded:

  public override void ItemAdded(SPItemEventProperties properties)
    {
        SPListItem item = properties.ListItem;
        item.Update();
    }

The outcome of this is that the field is updated but not before it is shown so that when the user is directed to the output page the list will look as it did before but if you change view or look at the details for the reply you will find that it has actually changed as it was supposed to.

This is good enough for me as I'm expecting most of my replies to arrive by email.

glenatron