views:

16

answers:

1

Hi,

I have created a custom list. I have a field called manager Email. Whenever a list items gets added to this field manager should get an email. This workflow should be automatic.

Any directions please

+2  A: 

"Whenever a list items gets added" sounds like an Event Receiver to me rather than a workflow:

public class MyEventReceiver : SPItemEventReceiver 
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        using (SPWeb web = properties.OpenWeb())
        {
            SPListItem item = properties.ListItem;
            SPUtility.SendEmail(web, true, true, item["ManagerEmail"].ToString(), "Subject", "Body");
        }
    }
}

EDIT:

You attach this code to a list through a Feature. If you have created a custom list template, you can use the Receivers Element. But if the custom list was created through the UI, you will need to use a Feature Receiver:

public class MyFeatureReceiver : SPFeatureReceiver {

    public override void FeatureActivated(SPFeatureReceiverProperties properties) {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
            Type type = typeof(MyEventReceiver);
            SPList list = web.Lists["My Custom List"];
            list.EventReceivers.Add(SPEventReceiverType.ItemAdded, type.Assembly.FullName, type.FullName);
            list.Update();
        }
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
    }
    public override void FeatureInstalled(SPFeatureReceiverProperties properties) {
    }
    public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {
    }
}

For more information:

Rich Bennema
the items are added from the user interface which I have created a form in designer with the validation. So I am not sure where do I write this peice of code?
Hari Gillala
You attach the Event Receiver to the list with a Feature. See my edit for more information.
Rich Bennema
Thank you. Much useful
Hari Gillala

related questions