Using the event receiver to update the Title field

I wrote an event listener that fires in the "ItemAdded" event in the "Pages" list. The code works fine, but my problem is that while calling my Sub ItemAdded, I want to change the value of the field that belongs to the current list item (in this case, an aspx page).

The idea is that I can configure the "Title" field as another value that is configured by my event receiver, and by the time the user sees the page in edit mode, a new title will be saved for this 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 tried both Update () and SystemUpdate (), but in each case, when the user tried to check the page, they get a message that the page was changed from the outside. In addition, when viewing a page, the value of the header field has not changed.

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

Greetings. Jac.

+4
source share
4 answers

change of properties. By default, conflict properties will be resolved. note that afterproperties is a hash table, but you can access it like this:

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

+3
source

ItemAdd ed The name says it all. This is an asynchronous event that occurs after adding items, as well as a problem with your calse. I suggest you hook up the ItemAdding event if you have no reason not to.

Refer Link for details on asynchronous and synchronous

+2
source

The best way to achieve this is to use the ItemAdd ing event. This will allow you to change the Title value before storing it in the database. Trying to change them in ItemAdded is possible, but will lead to headaches that you experience.

Have you tried ItemAdding?

0
source

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

Unfortunately, this did not seem to automatically start when a new answer was added to the discussion (perhaps this applies to other scripts related to the list), but if I put the code in the ItemAdded method (ItemAdding also did not start) I found that it starts but I couldn’t change the item from there, so I got something like this in itemAdded:

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

The result of this is that the field is updated, but not before it is shown, so when the user is redirected to the output page, the list will look like he did before, but if you change the view or view the details for the answer , you will find that it has really changed, as expected.

This is good enough for me as I expect most of my responses will come by email.

0
source

All Articles