Thursday, 26 April 2012

Edit Page Layout from SharePoint page

ListPages.aspx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListMasterPages.aspx.cs" Inherits="TestMasterPageList.Layouts.TestMasterPageList.ListMasterPages" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
PageHead
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <asp:Table ID="Table1" runat="server" Width="100%">
    <asp:TableRow><asp:TableCell Width="20%">
       
    <asp:GridView ID="gvMLPages" runat="server" OnRowDataBound="gvMLPages_RowDataBound">
    </asp:GridView>

    </asp:TableCell>
    
    <asp:TableCell  Width="80%">
       
    <asp:TextBox ID="tbxPageSource" runat="server" Rows="30" TextMode="MultiLine" Width="100%" BackColor="#FEE2C7" Font-Names="Lucida Console" ForeColor="#0033CC"></asp:TextBox>
    <br />
     <asp:Label ID="labelInfo" runat="server" Text="Label"></asp:Label>
     <br />
        <asp:Button ID="btnCheckOut" runat="server" Text="Check Out" OnClick="btnCheckOut_Click" />

        <asp:Button ID="btnUndoCheckout" runat="server" Text="Undo Check Out" OnClick="btnUndoCheckout_Click" />
        <asp:Button ID="btnSaveAndCheckIn" runat="server" Text="Save,CheckIn,Publish" OnClick="btnSaveAndCheckInPublish_Click" />
    <br />
        <asp:Label ID="lblCheckOut" runat="server" Text="Please check out before editing."></asp:Label>
    </asp:TableCell>
    
    </asp:TableRow>
    </asp:Table>

    
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
List Master Pages & Layouts
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
PageTitleInTitleArea List Master Pages & Layouts
</asp:Content>


ListPages.aspx.cs
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Net;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;

namespace TestMasterPageList.Layouts.TestMasterPageList
{
    public partial class ListPages : LayoutsPageBase
    { 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string layoutpagename = Request.QueryString["layoutpage"];

                gvMLPages.DataSource = getLayoutPages();
                gvMLPages.DataBind();

                givePageLayout(layoutpagename); 
            }
        }

        protected void gvMLPages_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                TableCellCollection cells = e.Row.Cells;

                foreach (TableCell cell in cells)
                {
                    cell.Text = Server.HtmlDecode(cell.Text);
                }

            }
            if (e.Row.RowType == DataControlRowType.Header)
            { 
                    e.Row.Cells[0].Text = "Page Layouts";
            }
        }
        protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            lblCheckOut.Text = "Checked out " + Request.QueryString["layoutpage"];
            tbxPageSource.Enabled = true;
            string layopage = Request.QueryString["layoutpage"];
            if (layopage == null || layopage.Length == 0)
            {
                lblCheckOut.Text = "No layout page defined.";
                return;
            }
            SPWeb web = SPContext.Current.Web;
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); 

            PageLayout pagelayout = null;

            foreach (PageLayout p in pubWeb.GetAvailablePageLayouts())
            {
                if (p.Name.Equals(layopage))
                {
                    pagelayout = p;
                    
                }
            }
            SPFile aspxpage = pagelayout.ListItem.File;
            aspxpage.CheckOut();
            Response.Redirect(Request.RawUrl); 
        }


        protected void btnUndoCheckout_Click(object sender, EventArgs e)
        {
            string layopage = Request.QueryString["layoutpage"];
            tbxPageSource.Enabled = false;
            if (layopage == null || layopage.Length == 0)
            {
                lblCheckOut.Text = "No layout page defined.";
                return;
            }
            lblCheckOut.Text = "Checked out UNDO for " + Request.QueryString["layoutpage"];

            SPWeb web = SPContext.Current.Web;
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
 

            PageLayout pagelayout = null;

            foreach (PageLayout p in pubWeb.GetAvailablePageLayouts())
            {
                if (p.Name.Equals(layopage))
                {
                    pagelayout = p;

                }
            }
            SPFile aspxpage = pagelayout.ListItem.File;
            aspxpage.UndoCheckOut();
            Response.Redirect(Request.RawUrl); 
        }

        protected void btnSaveAndCheckInPublish_Click(object sender, EventArgs e)
        {
            string layopage = Request.QueryString["layoutpage"];
            tbxPageSource.Enabled = false;
            if (layopage == null || layopage.Length == 0)
            {
                lblCheckOut.Text = "No layout page defined.";
                return;
            }

            SPWeb web = SPContext.Current.Web;
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); 

            PageLayout pagelayout = null;

            foreach (PageLayout p in pubWeb.GetAvailablePageLayouts())
            {
                if (p.Name.Equals(layopage))
                {
                    pagelayout = p;

                }
            }
            SPFile aspxpage = pagelayout.ListItem.File;

            try
            {
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[] byteArrayFileContentsAfter = enc.GetBytes(tbxPageSource.Text);
                aspxpage.SaveBinary(byteArrayFileContentsAfter);


                aspxpage.CheckIn("page layout changed checkin ");
                aspxpage.Publish("page layout changed publish");
                aspxpage.Approve("page layout changed approved");
            }
            catch (Exception ex)
            {
                labelInfo.Text = "Error save, checkin, publish: " + ex.ToString();
            }
             

            Response.Redirect(Request.RawUrl);
        }


        private List getLayoutPages()
        {
            List webpages = new List(); 
            SPWeb web = SPContext.Current.Web;
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
              

            foreach (PageLayout p in pubWeb.GetAvailablePageLayouts())
            {
                webpages.Add("<a href=' " + returnRaw(Request.RawUrl) + "?layoutpage=" + p.Name + "'>" + p.Name + "</a>");
            }
            return webpages;
        }
         

        private string givePageLayout(string layoutpagename)
        {
            if (layoutpagename == null || layoutpagename.Equals(""))
                layoutpagename = "pagelayoutcustom.aspx";
            SPWeb web = SPContext.Current.Web;
            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); 

            PageLayout pagelayout = null;

            foreach (PageLayout p in pubWeb.GetAvailablePageLayouts())
            {
                if (p.Name.Equals(layoutpagename))
                {
                    pagelayout = p;
                    break;
                }
            }

            labelInfo.Text = pagelayout.Title;
            SPFile aspxpage = pagelayout.ListItem.File;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(aspxpage.OpenBinaryStream()))
            {
                tbxPageSource.Text = reader.ReadToEnd();
            }
            string raw = Request.RawUrl;
            int lengthraw = raw.Length - (raw.Length - raw.IndexOf("?layout"));
            labelInfo.Text = layoutpagename + " [checkout status: " + aspxpage.CheckOutType.ToString() + "]" ;

            if (aspxpage.CheckOutType.Equals(SPFile.SPCheckOutType.Online))
                btnCheckOut.Enabled = false;
            if (aspxpage.CheckOutType.Equals(SPFile.SPCheckOutType.None))
            {
                tbxPageSource.Enabled = false;
                btnSaveAndCheckIn.Enabled = false;
                btnUndoCheckout.Enabled = false;
            }
            return "";
        }

        private string returnRaw(string url)
        {
            if (url.IndexOf("?layout") < 0)
                return url;
             
            return url.Substring(0, url.IndexOf("?layout"));
        
        }
    }
}

No comments:

Post a Comment