views:

288

answers:

4

I have a library and I want to run some code when an item is added to populate some additonal columns. Is there any way I can have this code run automcailly. Where would I go about adding it. Also would it be possible to have a screen to be able to edit the options of the code. Just some pointing in the right dirrection would be great.

Would this be a case of creating a workflow?

A: 

What you can do is to Create an Event Handler and place your code there. You can find an Example in This list of Videos.

What EventHandler gives you is a place to hook up your code and do your stuff. You can also decide about when your code runs (Sync or Async) that is Before an Item is added or after. This is an excellent article to get t know about the EventHandlers.

Kusek
A: 

Both, Workflows or EventReceivers (Event Handler) could be attached to lists or libraries and would do the job. Which one you should use, depends on what you're trying to archive.

Workflows are a little bit more complicated but more powerful and are mainly used to handle some long running processes which involve user interactions. The state of a workflow is controlled and also stored by SharePoint. So a workflow could be halted within a certain state an wait for a user action. When the user has then done his job the worklfow will go on.

In contrast to that a EventReceivers is just a piece of code that will be run before or after a certain event occurs/occurred on the list or library it is attached to. If a EventReceiver is triggered a user could not interact with it. The EventReceiver has to rely on the data that were available when the event was triggered.

Flo
+3  A: 

Workflows are not the way to go about it. You should be creating an item event receiver. The reason I say this is because it sounds like you have a need for some code to just run after you add an item. Since you don't need to maintain state then workflows are not the proper solution to this problem. Here's what you do: create a new class in Visual Studio and have it inherit from SPItemEventReceiver. Override the ItemAdded() method and put your logic in there. Ex:

public class MyItemEventReceiver : SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        // do your stuff
    }
}

This code will be called after an item is added. If you need to have code run before the item is added then you'll be overriding the ItemAdding() method. The way my company deploys event receivers is a bit different but this is the 'by the book' method using Elements.xml:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt;
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>My Event Receiver</Name>
      <Type>ItemAdding</Type>
      <SequenceNumber>1000</SequenceNumber>
      <Assembly>AssemblyName, Version=1.0.0.0, culture=neutral, PublicKeyToken=[token]</Assembly>
      <Class>Namespace.Class</Class>
      <Data></Data>
      <Filter></Filter>
    </Receiver>
  </Receivers>
</Elements>

You mentioned having a screen to edit options of the code. It sounds like you're verging on another can of worms so I can't speak directly to that. However, if you want to pass in various options that differ from deployment to deployment then just throw those into the <Data> tags above. Then you can access your options from the properties.ReceiverData property within your code. Also, please note that the <Filter> tags do nothing - they were not implemented by the WSS 3 team. Hope this helps.

Repo Man
A: 

The nice things about workflows is that the user can see a trace of what is going on in the History of the workflow.

I blogged about Event Receivers from start to finish: http://koenvosters.wordpress.com/2009/07/31/howto-create-an-event-handler-for-sharepointmoss-2007/

KoenVosters