Good News: WSS Event Receiver ItemAdding & ItemUpdating can work!

Recently I try to write the Event Receiver of WSS v3, focus on the SPItemEventReceiver class's ItemUpdating and ItemAdding method. In this way, I can implement some validating rule for WSS v3's list. For example, to insure one field in a list can't contain duplicate value!

First, I found a artical in the MSDN.
WSS 3.0 Preview - Discover Significant Developer Improvements In SharePoint Services
http://msdn.microsoft.com/msdnmag/issues/06/07/WSS30Preview/default.aspx
There is a good example code, see Fig.10 of that article.

Also this article become chapter 1 of one book name "7 development projects for Microsoft Office SharePoint Server 2007 ans Windows SharePoint Services".
http://download.microsoft.com/download/0/2/f/02f0f661-88e1-43c2-b523-88d2e9e6802f/7%20Development%20Projects%20with%20the%202007%20Microsoft%20Office%20System%20and%20Windows%20SharePoint%20Services%202007.pdf

These two source had the almost the same example code. But when I try that code, I found that the code get the field value store in list instead of the value user just keyin!
After google this problem, I found someone also had this same problem.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1224024&SiteID=1

And, so lucky, there were answer:
http://www.sharepoint-tips.com/2006/09/synchronous-add-list-event-itemadding.html
And some another relative discuss blog:
http://blah.winsmarts.com//post.aspx?postid=68

But, no code list. Just some tips. And the most important tip is:
The properties.AfterProperties can get the field value user just keyin. Some replies still say they can't get it work! Only one reply say the AfterProperties can work in the form of foreach iteration. This give me a idea: AfterProperties is a DictionaryEntry (like HashTable). So, it can't be index! Only foreach iteration can be used to find the match value.

string code = "";
foreach (DictionaryEntry de in properties.AfterProperties)
{
if (de.Key.Equals("code")) code = de.Value.ToString();
}

Ok! Now, I can get the filed value user just keyin. Using this coding style, ItemAdding and ItemUpdating method can check the field value user just keyin for validation.

留言

這個網誌中的熱門文章

D-BUS學習筆記

關於藍牙裝置找尋(inquiry, scan)兩三事

Cisco Switch學習筆記: EtherChannel