There is a requirement in the project, and a list is required to link nine other lists. It takes a lot of time to debug the project. I also found some tips in eventhandler. Most of the content below is reprinted, I have my own experiences. I forgot the specific source. If the author sees it, please forgive me.
1. Get the value entered by the user in the itemadding event or itemupdating event
We usually encounter a business logic that allows users to add records only when the value entered by the user meets the requirements. Otherwise, this operation is canceled. Or when the user adds a record, obtain the value entered by the user and perform other operations. When encountering these services, it is necessary to obtain the user value in the itemadding event, then we will naturally think of a property properties. listitem, this is the record item being added, but it is exactly the event before the addition, properties. the value of listitem is null and cannot be obtained. Even in the itemupdating event, properties. although listitem has a value, it is not the latest value entered by the user, but the original value (old value). It should be noted that properties. beforeproperties cannot be set in updating.
To get a new value when the record item is added or modified, we can directly use the following statement
Properties. afterproperties ["title"]. tostring ();
If the field is a Chinese character name, internalname is required.
Using (spweb web = (properties. openweb () as spweb)
{
Splist list = web. Lists [properties. listtitle] As splist;
Snametitle = List. Fields ["department"]. internalname;
Orgname = properties. afterproperties [snametitle]. tostring ();
}
2.Modify the value of this record item in the itemadding event or itemupdating event.
Sometimes, when designing the list, we will add some columns hidden from users to record information related to the record. We can only rewrite the value of this column through a program, how can we modify the value of this column when adding a new record item or modifying a record item?
Naturally, we will write the following program
Public override void itemupdating (spitemeventproperties properties)
{
Splistitem item = properties. listitem;
Item ["title"] = "this is the value I specified ";
Item. Update ();
}
However, I'm sorry, there is only one end to this section-an error! The system reports an error message "Save conflict. Let's take a closer look at the Code. The Code modifies this record item again when the record item is being modified. Similar to SQL, moss also locks the record item before entering the itemupdating event to prevent the record item from being modified again before the modification is completed, therefore, writing in this way is doomed to failure! This is especially true for itemadding events. As mentioned above, in itemadding events, properties. the value of listitem is null. This record item has not yet been generated, so it is impossible and should not be passed through properties. listitem to modify the value of a column, because properties. listitem indicates the old value of a record item (the old value before addition is not null)
So what attributes are used for modification? Moss will certainly provide us with a way to do this! Let's take a look at properties. the afterproperties attribute can be easily understood literally. It is a new value recorded, but unfortunately, this attribute is read-only and cannot be directly modified to achieve our goal. Next, let's look at the properties. afterproperties also has a property changedproperties. For more information, see properties. afterproperties. changedproperties literally means that this property saves the changed value of this field (the same is true for SDK, gets properties that have changed in the collection ), it is easy to understand that Moss will automatically compare the user's modified record values. At the beginning, I also understood this, But it is strange that no matter how I modify the record value, during debugging, the Count attribute of this property value is always 0, not as I started to think!
At that time, I checked a lot of information on the Internet, just to explain why Ms provided a hashtable attribute such as changedproperties, and there was always no value in it. I suddenly realized it when I decompiled the eventhandler event program in the MS official site template! We should understand the hashtable of changedproperties in this way. It is a set used to modify the column value for us. I think the SDK English should be changed to "gets properties thatWill beChanged in the collection, because in the itemadding event or itemupdating event, record items are not actually generated or modified. listitem represents the original value, afterproperties indicates the value to be changed for this record item. changedproperties in the Group provides an opportunity to modify it again. Therefore, to modify this record item in the itemadding event or itemupdating event, it is worth writing in this way.
Using (spweb web = (properties. openweb () as spweb)
{
Splist list = web. Lists [properties. listtitle] As splist; // get the current modified list
Snametitle = List. Fields ["Organization Code"]. internalname; // obtain the field name.
String test01 = pitem ["Organization Code"]. tostring ();
Properties. afterproperties. changedproperties. Add (snametitle, pitem ["Organization Code"]); // assign the modified value to the item of the current eventhandler
}
Note:
During itemupdating, itemadding, and itemdeleting programming, there are two different actions. The event first locks the current listitem, that is, you want to modify the token, and then store the modified value to be submitted in afterproperties, in this case, you can use changeproperties to modify the value to be submitted. beforeproperties is strange and cannot be obtained at any time. It is basically useless.
Disableeventfiring () and enableeventfiring () are a set of necessary methods, especially in the updating and updated events, but also depends on the specific situation, disableeventfiring ()
Int value1 = convert. toint32 (properties. afterproperties [field_internalname]. tostring ());
Int value2 = convert. toint32 (properties. beforeproperties [field_internalname]. tostring ());
Properties. afterproperties. changedproperties [field_internalname] = intinterval2;
Determine the first two, and then use the third. in addition, if the methods described in the first two points report an exception (this occurs when itemdeleting is written), you need to obtain the values in the current database. You can do this:
Try
{
String swhere = sylist. Fields ["ID card number"]. internalname;
String svalue = item ["ID card number"]. tostring ();
Syqry = new spquery ();
Syqry. query = "<where> <EQ> <fieldref name = '" + swhere + "'/> <value type = 'text'>" + svalue + "</value> </EQ> </where> ";
Result = sylist. getitems (syqry );
Return result;
}
Catch (exception ex)
{
Log. Error (ex. tostring ());
Throw ex;
}
List event debugging is a kind of activity, especially when there are many associated lists, it is a good habit to make a good data relationship diagram, and then you need a good error capture mechanism, use log4net for multiple purposes (see my previous article). In the production environment, you can easily find the cause of errors. Otherwise, you can cry.