Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have an SPListEventReceiver that handles the ListAdded event. If checks that the list is a Document Library, and then adds some fields to the library:

public class ListEventReceiver : SPListEventReceiver
{
    public override void ListAdded(SPListEventProperties properties)
    {
        if (properties.List.BaseTemplate == SPListTemplateType.DocumentLibrary)
        {
            // Add fields here
        }
        base.ListAdded(properties);
    }
}

When a list is created using SharePoint's web UI, the event works perfectly. However, when the list is created by the code below, the event is not fired, and the fields are not added:

SPWeb spWeb = ...;
SPListTemplate listTemplate = ...; //Find relevant DocLib template
Guid newListId = spWeb.Lists.Add(title, description, listTemplate);

EDIT: I do not have the option of changing the EventReceiver - it was created by a third party, and I have no control over it.

share|improve this question
    
Not sure it will help you but move base.ListAdded(properties); before your custom code. Can you debug and verify that your event is fired and condition is met? –  Vedran Rasol Feb 7 '12 at 21:12
    
What kind of library do you try to create? Some document libraries have different SPListTemplateType (not only DocumentLibrary which has code 101, but other codes). –  Kai Feb 8 '12 at 7:25
    
@Kai - just the regular DocLib, with 101 code –  SaguiItay Feb 8 '12 at 12:15
add comment

3 Answers

ListAdded event runs asynchronously (whereas ListAdding is synchronous). The properties may not be available for updating. This post describes an alternative using ListAdding - Working with ListAdding and ListDeleting Events.

I had a similar Event handling situation with ItemAdded/ItemAdding in a Document library where it worked fine under UI but not for automated upload (using PowerShell). There is a matter of at which point in the event handling the properties should or can be updated. This post explains the various Events and their variations ('-ing', '-ed', etc) - not all properties are available during the synch/asynch event handling - Managing ItemUpdating and ItemUpdated Events

More on getting ListAdded (-ed event handling) to run synchronously - SharePoint 2010 Event Receivers can be Synchronous or Asynchronous

UPDATE: Looking at the update to the question above ("EDIT: I do not have the option of changing the EventReceiver - it was created by a third party, and I have no control over it."). The solutions may be limited as I presume, you do not have access to the event handler code. You may try this change - you can specify the 'ListAdded' event handler as synchronous in its Elements.xml file (within the Receiver tag) - Synchronous.

Here's another link to consider should you regain the handler code - Binding asynchronous event handlers as synchronous

share|improve this answer
    
Does it really matter since in question it is stated that if list is created using UI everything works fine? –  Vedran Rasol Feb 7 '12 at 21:14
1  
also test if you can specify the 'ListAdded' event handler as synchronous in its Elements.xml file thus (within the Receiver tag) - <Synchronization>Synchronous</Synchronization> –  Supriyo SB Chatterjee Feb 7 '12 at 21:29
    
Vedran - please see my edited response above. Thanks. SBC. –  Supriyo SB Chatterjee Feb 7 '12 at 21:32
    
You missed my point. If everything works fine when adding list via UI (event fires, fields are added) then IMHO sync is not an issue. Or am I wrong? –  Vedran Rasol Feb 7 '12 at 21:39
    
Vedran - as mentioned above: the UI procedure worked for a manual upload but not during automated upload. Asynchronous events by its nature are non-deterministic. –  Supriyo SB Chatterjee Feb 7 '12 at 21:47
show 1 more comment

Not certain, but do you have to fire an spWeb.Update() after the add?

And should make sure you have using for your SPWeb object, not modifying directly. eg:

 using(SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL"))
{
    ...
}
share|improve this answer
add comment

Are you using SPContext.Current to get your SPWeb? Be sure you're using properties.Web or such in an event receiver. If the code isn't being run on the UI thread, SPContext.Current would be null.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.