Code for creating subsites in Sandbox

using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.XPath;

namespace CreateSiteHierarchy.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public partial class VisualWebPart1 : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
        // the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public VisualWebPart1()
        {
        }

        private string _newLine = "<br/>";
        private string Message = "";

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected XPathNavigator ReadXML()
        {
            SPFile aXmlFile = GetXmlFile();

            byte[] bytes = aXmlFile.OpenBinary();
            Stream aMemStream = new MemoryStream(bytes);
            XPathDocument aXPathDOc = new XPathDocument(aMemStream);

            XPathNavigator aXPAthDOc = aXPathDOc.CreateNavigator();

            return aXPAthDOc;          
        }

        private SPFile GetXmlFile()
        {
            SPFolder aSiteColDocsFolder = SPContext.Current.Site.RootWeb.Folders["SiteCollectionDocuments"];

            // get second xml file
            SPFile afile = aSiteColDocsFolder.Files[1];

            return afile;
        }

        protected void btnCreateSiteHierachy_Click(object sender, EventArgs e)
        {
            try
            {
                XPathNavigator aXMLNav = ReadXML();

                CreateHierarchy(aXMLNav, SPContext.Current.Site.RootWeb);

            }
            catch (Exception ex)
            {
                Message += "Exception in button click method: " + ex.Message;
            }
            finally
            {
                lbMessages.Text = _newLine + _newLine + Message;
            }
        }

        private void CreateHierarchy(XPathNavigator nav, SPWeb aParentWeb)
        {
            XPathNodeIterator nodes = nav.Select("web");
            while (nodes.MoveNext())
            {
                //A - read the attribute
                string url = nodes.Current.GetAttribute("url", string.Empty);
                string title = nodes.Current.GetAttribute("title", string.Empty);
                string Description = nodes.Current.GetAttribute("description", string.Empty);
                string template = nodes.Current.GetAttribute("template", string.Empty);
                //B - do something with the data

                SPWeb aWeb = getWeb(aParentWeb, title);

                if (aWeb == null)
                {
                    // web with this title doesnt exist yet so it shoudl be safe to create it.
                    aWeb = CreateWeb(url, title, Description, template, aParentWeb);
                }
                else
                {
                    // web already exists so continue with the childnodes of the current node in the xml
                }
                //C - recurse
                CreateHierarchy(nodes.Current, aWeb);
            }
        }

        private SPWeb getWeb(SPWeb aParentWeb, string aTitle)
        {
            SPWeb aWeb = null;

            foreach (SPWeb bWeb in aParentWeb.Webs)
            {
                if (bWeb.Title == aTitle)
                {
                    aWeb = bWeb;
                    break;
                }
            }

            return aWeb;
        }

        private SPWeb CreateWeb(string aUrl, string aTitle, string aDescription, string aTemplate, SPWeb aParentWeb)
        {
            SPWeb aNewWeb = null;

            try
            {
                string aTemplateName = GetTemplate(aTemplate);
                aNewWeb = aParentWeb.Webs.Add(aUrl, aTitle, aDescription, 1033, aTemplateName, false, false);              
            }
            catch (Exception ex)
            {
                Message += "Error creating web: " + ex.Message;
            }

            return aNewWeb;
        }

        private static String GetTemplate(string solutionName)
        {
            string templateName = null;
            SPWebTemplateCollection coll = SPContext.Current.Site.GetWebTemplates(1033);

            foreach (SPWebTemplate template in coll)
            {
                if (template.Title.Equals(solutionName, StringComparison.CurrentCultureIgnoreCase))
                {
                    templateName = template.Name;
                }
            }

            return templateName;
        }
    }
}

No comments:

Post a Comment