Moss 2007 Problem to create an eventhandler Adding Item

Hi,
I am working at the moment on sharepoint 2007 and visual studio 2005. I created a collection site.
then i created a custom list named Liste. I added a colunn called Sortie.
I added this webpart on the front page. Then I coded an event handler. then I enregistered it. And I noticed a thing, whenever i try to access to the current SPListItem i cant , so in the sane time i cant acces toOrders["Sortie"]. I checked the value of properties.ListItemId, the latter is equal to 0.
Please Could you help me, How can i access to the Itemlist I am creating.
Thank a lot for your help.
PS My code below :
publicclassListeClasse :SPItemEventReceiver
{
publicoverridevoid ItemAdding(SPItemEventProperties properties)
{
SPWeb web = properties.OpenWeb();
Guid ListeId = properties.ListId;
int ListeItemId = properties.ListItemId;
SPList MaListe = web.Lists[ListeId];
SPListItem Orders = web.Lists[ListeId].GetItemById(ListeItemId);
string t = Orders["Sortie"].ToString();
String msg ="Kiki, T es un Arioule !!!! ";
properties.ErrorMessage = msg + t ;
properties.Cancel =true;
}
}
}
staticvoid Main(string[] args)
{
SPSite site =newSPSite("http://ibbsvr07t:23002/sites/Toki");
SPWeb web = site.OpenWeb();
SPList Liste = web.Lists["Liste"];
string assemblyName ="Liste, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6b1377561a8780dc";
string className ="Liste.ListeClasse";
Liste.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
}
[3843 byte] By [arioule] at [2008-1-1]
# 1

If you read my article on this (http://spstips.blogspot.com/2006/09/synchronous-add-list-event-itemadding.html) you will find that Microsoft did this by design. In ItemAdding you dont get access to the item at all. Please join me in telling them to change it!

Ishai Sagi
www.spstips.blogspot.com

IshaiSagi at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 2
You can use properties.AfterProperties to see the values.
FrankChiang at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 3

Frank,

No, you can't. Most list types return "null" for "AfterProperties" in the "ItemAdding" event (read about this in my blog - microsoft acknowledged it is by design)

IshaiSagi at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 4

Ishai,

Please try this:

public class clsBDC : SPItemEventReceiver

{

public override void ItemAdding(SPItemEventProperties properties)

{

SPItemEventDataCollection b = properties.AfterProperties;

properties.ErrorMessage = b["Title"].ToString();

properties.Cancel = true;

}

}

This works for me. MSDN says about AfterProperties: Gets a hash table of properties consisting of string/value pairs that correspond to fields in the SPItem object after the event occurred.

Frank

FrankChiang at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 5
Well I hope that works, Or esle you can traverse through the template container controls and detect the selected value for specific drop downs or text entered in the textbox
rpoojary at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 6
Unless this is a Document Library, the BeforeProperties and AfterProperties will be empty. This is by design.
joshmadagan at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 7
I have done extensive work (or at least I'll pretend like I have) with EventReceivers. The ItemAdding event receiver has valid AfterProperties for regular lists and libraries alike. The problem you will run into is the AfterProperties hashtable is not equivalent to the SPListItem itself. For example, Moderation Status cannot be set as part of the AfterProperties, since ModerationStatus (or whatever the technical column name is) is not listed, and therefore null. Most likely, though, AfterProperties (as done in one of the code examples above) will serve your purpose.
CurtisRuppe at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 8

Frank,

I am trying to get the curreom ErrorMessage to display but the only message that show is the generic "The data source control failed to execute the insert command." message on the generica Sharepoint error page.

how did you get it to show up correctly?

GregorioRojas at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 9

Hi,

I accessed the values of various list fields by "properties.AfterProperties["<field name>"]". We have to take care that here we have to give field's "Internal Name" e.g. if a field name is "Display Name" then its internal name would be "Display_x0020_Name". This internal name you can see in query string after clicking the field's name in list setting page i.e. edit the column.

But I am running into different problem that after setting properties.Cancel = true; and properties.ErrorMessage = "some message";, I get WSS standard error page (when user clicks "OK" button on NewForms.aspx page) showing the error message what I set but after that user is not able to continue his work. On each action e.g. going back to list form by clicking "Back" button of browser again shows "Unexpected error" page. Then the only solution is that user has to open a new browser and continue his work. Its annoying.

Please help.

Thanks.

Ashish

AshishGupta at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...
# 10

Thanks Frank.

I was having the same issue , this post solved my problem.

SagarGandewar at 2007-9-12 > top of Msdn Tech,SharePoint Products and Technologies,SharePoint - Development and Programming...

SharePoint Products and Technologies

Site Classified