Take the 2-minute tour ×
SPWebApplication webApp = SPContext.Current.Site.WebApplication;

        using (SPSite site = webApp.Sites.Add("/sites/site3", "SharePoint", null, 1033, null, "administrator", "Administrator", "admin@admin.com"))
            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? –  DeeMac 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

2 Answers 2

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. –  DeeMac 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


