When you create a sub site with the same context of SPSite, you need to be smart enough to choose your code.
using (SPSite siteCollection = new SPSite(parentSiteURL))
{
SPWeb parentWeb = siteCollection.OpenWeb();//OPen the parent web
//Start code to get all available web templates
:
:
//End code
parentWeb.AllowUnsafeUpdates = true;
SPWeb AssignWeb = parentWeb.Webs.Add(siteURLRequested, siteTitle, "", Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate, false, false);
This code would give Access denied exception. Reason is when you are opening the web, site opening context does not hold good. Even if you put creation of subsite in another RunWithElevatedPrivileges, it wont work as new web would be created with another context. In order to resolve it replace parentWeb.Webs.Add with the following code
SPWeb AssignWeb = siteCollection.AllWebs.Add(siteURLRequested, siteTitle, "", Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate, false, false);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment