Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am working on Sharepoint list form with 10 fields. The first time when I add the new item I fill in 8 values and id is generated. Next time I edit the form I fill in the one of the two remaining values and I want to generate an email based on the value just filled in.

If I were to use event receiver (for the email generation) which one it would be, will it be item added or item updated?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

ItemAdding/ItemAdded is only triggered once, when the item is created. I guess for your problem it would be the ItemUpdating event:

public override void ItemUpdating(SPItemEventProperties properties)
{
  string value1 = Convert.ToString(properties.AfterProperties["Value1FieldInternalName"] ?? properties.ListItem["Value1FieldInternalName"]);
  string value2 = Convert.ToString(properties.AfterProperties["Value2FieldInternalName"] ?? properties.ListItem["Value2FieldInternalName"]);

  properties.AfterProperties["EmailFieldInternalName"] = BuildEmailAddress(value1, value2);
}

BuildEmailAddress is a method implemented by you...

share|improve this answer
    
IMHO this code will fail if there is change on item and Value1 or Value2 are not changed/filled because AfterProperties will be empty. Convert.ToString is also not needed. In case BuildEmailAddress throws exception item will not be updated. Disclaimer: I didn't test it so maybe I am wrong and this example in general is ok. –  Vedran Rasol Jan 25 '12 at 13:58
    
@VedranRasol: Generally: My code should demonstrate how it can be done as an example, but does not claim to be perfect. 1. properties.AfterProperties["Value1FieldInternalName"] returns an object (so Convert.ToString is reasonable)! 2. Exception handling does always matter, but to keep it short and clear in an example you can omit it! –  Matthias Jan 25 '12 at 15:31
    
And that is exactly why I added disclaimer in my comment :) +1 for updating your answer and like I already stated - it is a valid answer. –  Vedran Rasol Jan 25 '12 at 16:00
    
No problem ;-) Thanks! –  Matthias Jan 25 '12 at 16:05
1  
@spStacker: Too bad you didn't include this in your original question. Yes, if that is condition then ItemUpdating is valid solution! I can even provide some sample code but that would be unfair since Matthias answer is now much better then mine. –  Vedran Rasol Jan 25 '12 at 20:08
show 2 more comments

There is a matter of asynchronous vs. synchronous execution of the event handling code. This post shows it more detail - Managing ItemUpdating and ItemUpdated Events

share|improve this answer
add comment

I would use ItemUpdated method (since I used it in past for similar scenarios)

Using ItemUpdating for this scenario can lead to unwanted results eg. if your code fails item will not be updated. On other hand if email is crucial for your process maybe this can be desired behavior.

On ItemUpdated you should check are conditions meet and decide do you want to send email or not. If mail sending fails your item is 'safe'.

Since ItemUpdating/ed fires on every item change you need to decide do you want to send email on each change when condition is met or do you want to send email only once. For 'once only scenario' you can implement additional flag field in your item (boolean) and set it to true when email is send. You need to include this field in original condition.

Keep in mind that if you want to change item on ItemUpdating/ed you need to make sure that your receiver doesn't fire again and goes in 'infinitive loop'. This can be done by setting SPEventReceiverBase.EventFiringEnabled Property. Also when updating flag field you can use SPListItem.SystemUpdate Method to avoid visible item changes.

share|improve this answer
add comment

You would use SPItemEventReceiver.ItemUpdated.

However, you need to test the scenarios thoroughly as events fire a bit different depending on how the item is added, or if there is a attachment involved. See here.

Also take into account what should happen if user decides to modify item that already contains some values in the one/two remaining fields - should email be sent or not.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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