dinsdag 29 april 2008

More about events

As I had problems with the way events worked, I wanted to know more about the way they worked. below is bit of information about events that is good to know:

Two different kinds:
There are two different kinds of events: Synchronous and Asynchronous events.

* Synchronous events
- End with -ing (ItemAdding, ItemUpdating)
- Block the code as long the events arent finished
- Can (thus) make use of Session vars (HttpContext.Current is available)
- Can be used to prevent the event being finished. For example, with the ItemUpdating event you can verify the content being updated. Is the update not valid, the SPItemEventProperties.Cancel can be set to true and the SPItemEventProperties.ErrorMessage can be filled with an error message. This error message will not be displayed, but is written to the logfile. An error page will appear.
- dont contain the SPListItem in it's SPItemEventProperties (well, in the case of ItemAdding, that is, I didnt test it for the other events), so when you want to make changes to any of it's field you need to make use of the SPItemEventProperties.AfterProperties (see message below).

* Asynchronous events
- End with -ed (ItemAdded, ItemUpdated)
- Dont block the code.
- Can't (thus) make use of the Session (HttpContext.Current is null)
- Can't be reverted; When these events are fired, the item is already added, updated or deleted.
- contains the SPListItem. Changes can be made if you want to. This fires the (for example) ItemUpdating and ItemUpdated event. This can be turned off by using this.DisableEventFiring();

When creating a new list, events like FieldAdded and FieldAdding are not fired!

vrijdag 25 april 2008

modify SPListItem fields with vars from the Session in the itemAdding or itemAdded events

Yesterday, a colleague of me and I tried to alter a field of a SPListItem right after the moment that the item has been created. We stored a var in the sessionState and tried to read that one, so we could use that variable to alter the newly created SPListItem.

We found out that this was not as easy as we thought.

When using the itemAdded event, the httpContext is not available, so it's not possible to get the vars out of the HttpContext.Current.Session

When using the itemAdding event, the HttpContext is available, but the properties.ListItem is null then. This is expected behaviour, because the SPListItem is only filled with data from the database and in the itemAdding event, no write actions to the database have been executed yet.

In this case, the properties.AfterProperties can be used to modify a field on creation.

below is an example that shows whether the HttpContext and properties.ListItem are null or not shows how to modify properties when an item is added.



donderdag 17 april 2008

Crawl through all links

As I am still working on a migration project, the customer asked me if it was possible to check all urls in content editor webparts that I migrated. As i serialized all webparts on a 2003 to xml (for importing them to 2007), it was very easy for me to locate all content editor webparts, scan it's content and report the external, non-existant urls in it and change/report the internal, non-existant url's.

Reflection...

Today I got the question from a collegue if it was possible to instantiate a class, based on a result from a computation, without making use of an if-then-else, case or any other (logical) construction.


We found the answer in using reflection and attributes.
first of all, we created a class "ClassType":



After that, we created some classes, with each its own attribute:


after that, the attributes of a class in a certain assembly can be read (a little bit of pseudo code ;)

dinsdag 1 april 2008

Import user accounts

As you were able to read in my post before, I had a problem with disabled users. I decided that i didnt want any disabled users in my shared service provider, but how could i manage that? Luckily, there is a Microsoft KB article which helped me with not importing disabled accounts. By making smart use of the ADSearch, only enabled accounts are retrieved:

Adding users to a site

Today I was busy on migrating some data using the Tzunami tool, until I got the following error:

SPS2003HandlerException: Principal 'xxx\xxx' cannot be resolved. I was sure about it that the user existed in the SSP and but to be sure, I checked it a second time. And yes: it existed. So i tried to add that user manually to a site, but the user couldnt be added. I stumbled on that problem for about an hour, until i accidentally checked something in the AD:



Account disabled... SIGH!