I've got this eventreceiver that fires after an item has been added to a list.
The eventreceiver creates a subsite from a template I created myself.
The only problem is, every first time I try to add a listitem (so actually create a subsite), I have to wait +40 secs and the subsite wont be created.
But the next +100 times I try to add the subsite after my first attemt, every site has been created succesfully.
In other words, in this situation my customers have to create a "bad" subsite first before they can actually create a working subsite.
I'm using SharePoint online. Does anybody know how I can fix this problem.
Creating a site is a long running operation and therefore its a bad practice to do it an ItemAdding event which is synchronous by default. Make it on the ItemAdded Event and then it will be created, but no visual feedback will be given to the user.
Normally for this, we create a custom action on the list item, that custom actions open a simple aspx page with a long running operation and there you can show visual feedback to the user that the site has been created.
Doing anything even moderately intensive in sandbox code (like creating a subsite) is asking for trouble, sooner or later. You may find that creating a fake list item and broken site will become a "deployment step" in this scenario.
If your environment is on 2013, I'd like to suggest an alternate approach: Create an auto-hosted App with a web service that creates the subsite. Then, instead of an event receiver, put a workflow on that list that calls the web service.