SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
SPWebApplication webApp = SPContext.Current.Site.WebApplication;

        using (SPSite site = webApp.Sites.Add("/sites/site3", "SharePoint", null, 1033, null, "administrator", "Administrator", ""))
            SPWeb rootWeb = site.RootWeb;

            SPWebTemplateCollection webTemplates = site.RootWeb.GetAvailableWebTemplates(1033);
            SPWebTemplate webTemplate = (from SPWebTemplate t
                                         in webTemplates
                                         where t.Title.Equals("NameOfTemplate")
                                         select t).FirstOrDefault();
            if (webTemplate != null)

ApplyWebTemplate() is raising an SPException. The only further details (including from the InnerException property) I have is that it says:

"Cannot complete this action. Please try again. 0x80004005" with the stacktrace clearly leading me back to ApplyWebTemplate().

This template that I'm applying did call on a custom content type/page layout I had - so I removed all that to ensure that there were no 'missing file' issues.

Can anybody shed any light on where this exception could be coming from?

Thanks a lot.

share|improve this question
the issue is either with your code or with the template itself. Sooo, to isolate which it is, can you apply the web template via the browser just to see if it works? – Derek Gusoff Sep 19 '12 at 18:50
Thanks a lot Derek. Just attmempted Site Actions > New Site and chosen my custom site template, it didn't work. For the same reason. Any ideas? – JᴀʏMᴇᴇ Sep 20 '12 at 8:02
really hard to say what given what I know. I would suggest pulling everything out of the template, then adding items back one at a time to find the offending element. – Derek Gusoff Sep 20 '12 at 23:13

Use the overload that takes the SPWebTemplate object instead of the string, like this:

share|improve this answer
Thanks a lot for your suggestion but I'm getting the same result. – JᴀʏMᴇᴇ Sep 20 '12 at 8:00
up vote 0 down vote accepted

Sorted! Mostly down to the fact I found this article:

Refer to Daniel Scott's comment at the bottom - the onet.xml file needed a element somewhere in there, even if it was empty.

share|improve this answer

Your Answer


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.