Tuesday, November 1, 2011

File or arguments not valid for site template or how to create site from web template

This short code snippet creates a new SharePoint site from site template:

using (SPSite site = new SPSite("http://spserver/"))
            {
                SPWeb web = site.OpenWeb();
                SPWebTemplateCollection Templates = site.GetCustomWebTemplates(web.Language);
                SPWebTemplate siteTemplate = Templates["mySiteTemplate.stp"];


                //Checks whether site requested is exist or not
                if (web.Webs["test"].Exists)
                {
                    web.Webs.Delete("test");
                }


                //Creates a new site
                web.Webs.Add("test", "This is test site", "", Convert.ToUInt32(web.Language), siteTemplate, true, false);
            }


If you would receive this error message: File or arguments not valid for site template "someTemplate"
You can check two things to fix this error:
The first one is to use correct function to get web templates from your site. If you would use GetWebTemplates(unit LCID) function you will get default site templates and if you need to use site template which you were created you should use GetCustomWebTemplates(unit LCID).
The second is to use correct site template name. Check this link to find out how to get the proper site template name.

Hope it helps ;)

No comments:

Post a Comment