Options

How to create a trigger in SharePoint off of a List Item?

N2ITN2IT Inactive Imported Users Posts: 7,483 ■■■■■■■■■■
I was messing with the workflow in 2010 and created a three-state workflow and but noticed a few items grayed out and incomplete. I was able to check start this workflow when a new item is created, however when item is changed was grayed out. I went through the wizard and set it to choose the list field assigned to all the time. Anytime this changes it should trigger an email from Outlook. Obviously I am not getting to far with this piece. I was told I could create a trigger in SharePoint without using the workflow option on the field itself. Not sure if this is true or not. Any assistance would be greatly appreciated.

Comments

  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    This is called an Event Receiver. There are two basic types: -ing and -ed. Such as ItemAdded and ItemDeleting. ItemAdded fires after the item has been added to the list. Item adding fires before the item has actually been created.

    You must not change an item during the -ing events. Those are really just for data verification and sending notifications. You can change an item during the ItemAdded and ItemUpdated events. But you must disable event firing while you are operating on the item or it will cause an infinite loop. There are a number of tutorials on how to create even receivers and to be honest probably 80% of the things you need to do in "workflow" should actually be accomplished using an event receiver. It has much less overhead. If you need any specific guidance, email me and I can help you with the details. This is probably the single most useful thing you can learn in SharePoint development without needing to know a lot about development itself.
  • Options
    N2ITN2IT Inactive Imported Users Posts: 7,483 ■■■■■■■■■■
    Robert thanks for chiming in. Do you know of any good resource material to help walk a beginner through some of this? Thanks again.
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Download a trial copy of VisualStudio 2012, and go through this tutorial. You will need a development environment for this. I don't suggest that you add a trial copy of VS2012 to a production server, of course.

    Creating SharePoint 2010 Event Receivers in Visual Studio 2010

    There are two ways to associate an event receiver with a list. One is via an XML file that is automatically created when you add an item event receiver to your VisualStudio project. IMO, this method is somewhat prone to problems caused by minor issues in the configuration that are difficult to troubleshoot when you are just starting out in SharePoint custom development.

    Before you do anything below, take a look at this blog post on how to programmatically associate a an event receiver with a list.
    Add an event receiver to a specific list programmatically | Bram Nuyts

    So rather than use the Elements.xml that is created when you add the List Item Event Receiver just delete it from your project. Add an on Activated Feature Event Receiver. This will allow us more control over adding our list item event receiver to the list of our choice.

    Here is a good answer on the SharePoint StackExchange on how to check if a field's value has changed.
    How can I detect whether a specific column changed in an SPItemEventReceiver.ItemUpdated event (SP 2010)? - SharePoint Stack Exchange

    Here is an example of how to send an email in an event receiver. The StringDictionary class is just an array of name/value pairs.

    [C#] Using SPUtility and StringDictionary to Send Email in ShareP - Pastebin.com

    If you get this deployed to your development SharePoint site and it's not working, the first thing to do is check the logs in the 14 directory. If your feature activated activated fine and it seems that the event receiver is just not firing, use PowerShell to ensure that it is associated with the list.
    $web = Get-SPWeb "http://etc";
    $list = $web.Lists["ListName"];
    $list.EventReceivers | [COLOR=#000000][FONT=inherit] | select Name, Assembly, Type[/FONT][/COLOR]
    

    The code above is from memory, so you might need to tweak it.
  • Options
    N2ITN2IT Inactive Imported Users Posts: 7,483 ■■■■■■■■■■
    Robert what about having end users set up alerts from the My Settings tab and point the alerts at the list/column?
  • Options
    tstrip007tstrip007 Member Posts: 308 ■■■■□□□□□□
    Thats what I do, I have the "site owner" setup the alerts that way for whoever and whatever.
  • Options
    N2ITN2IT Inactive Imported Users Posts: 7,483 ■■■■■■■■■■
    Can an admin do this for the other users without them having to set it up for themselves?
  • Options
    tstrip007tstrip007 Member Posts: 308 ■■■■□□□□□□
    Yes. Im using MOSS 07. Once you click "Alert Me", you can just remove your name and search your AD to add a user/users.
  • Options
    N2ITN2IT Inactive Imported Users Posts: 7,483 ■■■■■■■■■■
    Makes sense thanks again for helping out.
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    I am pretty sure that alerts do not fire based on the changes in a specific column, though. That is the only issue that you might encounter. If your requirement is that you send an email whenever "column x" changes, then you need to either do a custom alert or an event receiver.
  • Options
    N2ITN2IT Inactive Imported Users Posts: 7,483 ■■■■■■■■■■
    Thanks for the potential risk we may face Robert. I'll make sure to explore all the options, heck now we are having trouble getting exchange intergrated into SharePoint, it's not recognizing all the email addresses. I think the AD accounts were made with the email fields being populated since we use WorkSpace for out email provider.
Sign In or Register to comment.