Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
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)
            {
                site.RootWeb.ApplyWebTemplate(webTemplate.Name);
            }
        }

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
1  
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
add comment

2 Answers

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

site.RootWeb.ApplyWebTemplate(webTemplate); 
share|improve this answer
    
Thanks a lot for your suggestion but I'm getting the same result. –  DeeMac Sep 20 '12 at 8:00
add comment
up vote 0 down vote accepted

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

http://blog.brianfarnhill.com/2009/04/cannot-complete-this-action-when-trying-to-create-a-document-library

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