Code source works fine from console application but not from SharePoint interface

Hi;
Please can you help
me to understand why my code below works fine from console application (VS 2010) and not working from interface of my SharePoint collection site :
From console application : the subsite is created and I can to open without any problem
From Sharepoint GUI : the subsite is created but impossible
to open subsite : error 404 page not found ?
The subsite creation is very long (2 minutes) and in concole application When I try to open the subsite just after its creation I have page not found and I must waits (several refresh) more time to open the subsite correctly.
Can be I must to display a PoPup to show the progress creation ?
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace CreateSubSiteClients.EventReceiver1
    public class EventReceiver1 : SPItemEventReceiver
        public override void ItemAdded(SPItemEventProperties properties)
                 base.ItemAdded(properties);
                 try
                     SPWeb web = properties.OpenWeb();
                     if (properties.List.Title == "REFCLIENTS")
                         SPListItem curItem = properties.ListItem;
                         string name = properties.ListItem["SITE"].ToString();
                         curItem["SiteUrl"] = web.Url + "/" + name;
                         SPWeb rootWeb = web.Site.RootWeb;
                         SPWebTemplateCollection webTemplates = rootWeb.GetAvailableWebTemplates(1036);
                         SPWebTemplate webTemplate = null;
                         String webTemplateName = "ModeleSiteCourrier";
                         String webTemplateSearchName = "";
                         for (int i = 0; i < webTemplates.Count; i++)
                             webTemplateSearchName = webTemplates[i].Name.ToString();
                             if (webTemplateSearchName.Contains(webTemplateName))
                                 webTemplate = webTemplates[webTemplateSearchName];
                                 break;
                       SPWeb newSite = web.Webs.Add(name, name, name, Convert.ToUInt16(1036), webTemplate, false, false);
                        newSite.Navigation.UseShared = true;
                        newSite.Update();
                        newSite.Close();
                        Console.WriteLine("Le site suivant a été crée", name);
                 catch (Exception ex)
                     properties.Status = SPEventReceiverStatus.CancelWithError;
                     properties.ErrorMessage = ex.Message.ToString();
                     properties.Cancel = true;
Regards

Hi,
To create subsite using event receiver in SharePoint Empty Project, you can refer to:
Sharepoint 2010 event handler to create subsites
To display a PoPup to show the progress creation, you can use:  MessageBox.Show("Hello, world.");
More information:
MessageBox.Show Method (String)
Best Regards,
Linda Li
Linda Li
TechNet Community Support

Similar Messages

Maybe you are looking for