Code for creating pages in Sandbox from XML file

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

namespace CreatePages.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()
        {
        }

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

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

        protected void btnCreatePage_Click(object sender, EventArgs e)
        {
            SPFile aXmlFile= GetXmlFile();

            XmlDocument aXMLDoc = new XmlDocument();

            byte[] bytes = aXmlFile.OpenBinary();
            Stream aMemStream = new MemoryStream(bytes);
            aXMLDoc.Load(aMemStream);

            XmlNode aPageNameNode = aXMLDoc.SelectSingleNode("Pages/Page/Name");
            XmlNode aTitleNode = aXMLDoc.SelectSingleNode("Pages/Page/Title");          
            XmlNode aPageLayoutNode = aXMLDoc.SelectSingleNode("Pages/Page/PageLayout");
            XmlNode aPageContentNode = aXMLDoc.SelectSingleNode("Pages/Page/PageContent");

            string aPageName = aPageNameNode.InnerText;
            string aPageTitle = aTitleNode.InnerText;
            string aPageLayout = SPContext.Current.Site.Url + aPageLayoutNode.InnerText;          
            string aPageContent = aPageContentNode.InnerText;

            SPList aPagesList = SPContext.Current.Site.RootWeb.Lists["Pages"];
         
            PublishingPageCreator.AddPublishingPage(aPageName, aPageTitle, aPageLayout, aPagesList.RootFolder, aPageContent);
        }

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

            SPFile afile = aSiteColDocsFolder.Files[0];

            return afile;
        }

        internal static class CompiledTemplateStrings
        {
            internal static string templateRedirectionPageMarkup = @"<%@ Page Inherits='Microsoft.SharePoint.Publishing.TemplateRedirectionPage, Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c' %><%@ Reference VirtualPath='~TemplatePageUrl' %> <%@ Reference VirtualPath='~masterurl/custom.master' %>";
        }

        internal class PublishingPageCreator
        {
            private static byte[] pageAsBytes;
            private static readonly UTF8Encoding PageEncoder = new UTF8Encoding();
            internal static byte[] GeneratePageStreamAsBytes()
            {
                if (pageAsBytes == null)
                {
                    byte[] bytes = PageEncoder.GetBytes(CompiledTemplateStrings.templateRedirectionPageMarkup);
                    pageAsBytes = new byte[bytes.Length + 3];
                    pageAsBytes[0] = 0xef;
                    pageAsBytes[1] = 0xbb;
                    pageAsBytes[2] = 0xbf;
                    bytes.CopyTo(pageAsBytes, 3);
                }
                return pageAsBytes;
            }


            internal static SPListItem AddPublishingPage(string name, string title, string layout, SPFolder folder, string aPageContent)
            {
                SPFileCollection files = folder.Files;
                byte[] file = GeneratePageStreamAsBytes();
                SPFile newFile = files.Add(name, file);
                SPListItem item = newFile.Item;
                item["ContentType"] = "HRSContentPage";
                item["ContentTypeId"] = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3905";
                item["PublishingPageLayout"] = layout;
                item["PublishingPageContent"] = aPageContent;
                item["Title"] = title;
                item.Update();
                newFile.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                return item;
            }
        }
    }
}

No comments:

Post a Comment