Wednesday, June 27, 2012

custom.aspx page

===============================================

  1. Create a new file customspcf.txt
  2. Copy the below text into it.
  3. then save and rename the customspcf.txt to customspcf.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#" DynamicMasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase"       %>


<script runat="server">
string L_GeneralError_Text = "An error has occurred.";
private void Page_Load(Object sender, EventArgs e)
{
    try
    {
        SPWeb spWeb = SPControl.GetContextWeb(Context);
        SPSite spServer = SPControl.GetContextSite(Context);
         // Make sure that the user is authenticated
        SPUtility.EnsureSessionCredentials(SPSessionCredentialsFlags.RequireAuthentication);
        // Create the select drop down control for the available Document Libraries only when the page is first loaded
        if (!Page.IsPostBack)
        {
            SPListCollection spLists = spWeb.Lists;           
            spLists.IncludeRootFolder = true;
            int iIndex = 0;
            bool bDocLibAvailable = false;
            for (int i = 0; i < spLists.Count; i++)
            {
                SPList spList = spLists[i];
                SPDocumentLibrary spDocLib = spList as SPDocumentLibrary;
                if ((!spList.Hidden) && (spList.BaseType == SPBaseType.DocumentLibrary) && spDocLib != null && !spDocLib.IsCatalog && (spList.BaseTemplate != SPListTemplateType.PictureLibrary))
                {
                    bool bHasPermission = false;
                    bool oldState = spServer.CatchAccessDeniedException;
                    try
                    {
                        spServer.CatchAccessDeniedException = false;
                        bHasPermission = spList.DoesUserHavePermissions(SPBasePermissions.AddListItems);
                    }
                    catch (UnauthorizedAccessException)
                    {
                    }
                    finally
                    {
                        spServer.CatchAccessDeniedException = oldState;
                    }
                    if (bHasPermission)
                    {
                        bDocLibAvailable = true;           
                        string value = SPEncode.HtmlEncode(spList.ID.ToString("B").ToUpper());
                        // ListItem encodes the text already, so no HTML encode here is needed
                        string text = spList.Title;
                        ListItem li = new ListItem(text, value);
                        if(iIndex == 0)
                        {
                            li.Selected = true;
                        }
                        onetidDocLibIDSelect.Items.Add(li);
                        iIndex++;
                    }
                }
            }
            // If no document library is available, disable the select drop down, and renders a link to create a document library
            if (iIndex == 0)
            {
                string L_NoneAvailable_Text = "None Available";
                onetidDocLibIDSelect.Disabled = true;
                ListItem li = new ListItem(L_NoneAvailable_Text);
                onetidDocLibIDSelect.Items.Add(li);
                string L_CreateDocLib1_Text = "Create a new ";
                string L_CreateDocLib2_Text = "Document Library";
                onetidCreateDocLibLabel.Text = L_CreateDocLib1_Text;
                onetidCreateDocLibLink.HRef = "new.aspx?ListTemplate=101&ListBaseType=1";
                onetidCreateDocLibLink.InnerText = L_CreateDocLib2_Text;
                btnCreate.Enabled = false;
            }
        }
        else // If the form posts back then we assume users press to enter button to create Web Part Pages
        {
            CreateWebPartPage();
        }
    }
    catch
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
}

private void SubmitBtn_Click(Object sender, EventArgs e)
{
    try
    {
        CreateWebPartPage();
    }
    catch
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
}
private void CreateWebPartPage()
{
    SPWeb spWeb = SPControl.GetContextWeb(Context);
    string templateName = Request.Form["WebPartPageTemplate"];
    // Validate the source file name
    if (templateName == null)
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    templateName = templateName.Trim();  // get rid of white spaces
    if (templateName.Length == 0)
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    templateName = templateName + ".aspx";
    if (templateName.Length > SPUtility.MaxLeafNameLength)
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    // Avoid characters like "..\..\" in the path
    if (templateName != System.IO.Path.GetFileName(templateName))
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    // Validate target file name
    string fileName = Request.Form["Title"];
    if (fileName == null)
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    fileName = fileName.Trim();  // get rid of white spaces
    if (fileName.Length == 0)
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    // Avoid characters like "..\..\" in the path
    if (fileName != System.IO.Path.GetFileName(fileName))
    {
        Context.Server.Transfer("error.aspx?ErrorText=" + SPEncode.UrlEncode(L_GeneralError_Text));
        return;
    }
    // Prepare the source file, assuming the Web Part Page templates live in
    // <Installation Path>\Template\Layouts\CustomWebPartTemplates
    string sourceFilePath = SPUtility.GetGenericSetupPath("Template\\") +
        "Layouts\\CustomWebPartTemplates\\";
   
    //string sourceFilePath = SPUtility.GetGenericSetupPath("Template\\") +
    //    spWeb.Language.ToString() + "\\" + spWeb.WebTemplate +
    //    "\\doctemp\\smartpgs\\";

    sourceFilePath = sourceFilePath + "mywpptd" + templateName;
    Response.Write(sourceFilePath);
    System.IO.StreamReader sr = new System.IO.StreamReader(sourceFilePath);
    //Response.Write("Ok");
    string content = sr.ReadToEnd();
    // Assuming the Web Part Page template has a Title Bar Web Part with the title place holder "_TitlePlaceHolder_"
    content = content.Replace("_TitlePlaceHolder_", fileName);
    // Save the target file into the database
    fileName += ".aspx";
   
    string doclibID = onetidDocLibIDSelect.Value;
    Guid guidDocLib = new Guid(doclibID);
    SPList doclib = spWeb.Lists[guidDocLib];
    string folderPath = doclib.RootFolder.Url;
    string targetFilePath = spWeb.Url + "/" + folderPath + "/" + fileName;
    // Convert the string into UTF8 encoded bytes
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    System.Byte[] contentBytes = encoding.GetBytes(content);
    System.Byte[] bytes = new System.Byte[contentBytes.Length + 3];
    // Adding UTF8 byte order mark
    bytes[0] = 0xEF;
    bytes[1] = 0xBB;
    bytes[2] = 0xBF;
    contentBytes.CopyTo(bytes, 3);
    SPFileCollection fileCollection = spWeb.Files;
    if (OverwriteCheckBox.Checked)
    {
        SPFile spFile = spWeb.GetFile(targetFilePath);
        if (spFile != null && spFile.Exists)
        {
            SPFolder spFolder = spWeb.GetFolder(folderPath);
            spFolder.Files.Delete(targetFilePath);
        }
    }
    fileCollection.Add(targetFilePath, bytes);
    // Redirect to the newly created Web Part Page, with the toolpane opened in the Add Web Parts view
    Response.Redirect(targetFilePath + "?PageView=Shared&DisplayMode=Design&InitialTabId=Ribbon.WebPartPage&VisibilityContext=WSSWebPartPage");
}
</script>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<% SPSite spServer = SPControl.GetContextSite(Context); SPWeb spWeb = SPControl.GetContextWeb(Context); %>
<head>
    <meta name="GENERATOR" content="Microsoft SharePoint"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="Expires" content="0"/>
   
    <title id="onetidTitle"><SharePoint:EncodedLiteral ID="EncodedLiteral1" runat="server" text="<%$Resources:wss,pagetitle_sharepoint%>" EncodeMethod='HtmlEncode'/></title>
<SharePoint:CssLink ID="CssLink1" runat="server"/>
    <SharePoint:Theme ID="Theme1" runat="server"/>
<SharePoint:ScriptLink ID="ScriptLink1" name="init.js" language="javascript" runat="server" />
<SharePoint:ScriptLink ID="ScriptLink2" name="core.js" language="javascript" runat="server" />
<SharePoint:CustomJSUrl ID="CustomJSUrl1" runat="server" />
<link type="text/xml" rel='alternate' href="_vti_bin/spdisco.aspx" />
</head>
<script type="text/javascript">
// <![CDATA[
var strImagePath = "../Images/MyWebPartLayoutImages/";
function DoValidateAndSubmit()
{
    var form = document.frmWebPage;
    form["Title"].value = TrimSpaces(form["Title"].value);
    if (form["Title"].value.length < 1)
    {
        var L_alert1_Text = "You must specify a non-blank value for Name.";
        window.alert(L_alert1_Text);
        form["Title"].focus();
        return false;
    }
    if (IndexOfIllegalCharInUrlLeafName(form["Title"].value) >= 0)
    {
        var L_IllegalChar_Text = "The file name contains invalid characters. Type another file name using valid characters.";
        window.alert(L_IllegalChar_Text);
        return false;
    }
    var index = document.frmWebPage.onetidDocLibIDSelect.selectedIndex;
    var ListValue = document.frmWebPage.onetidDocLibIDSelect.options[index].value;
    if (ListValue == "")
    {
    var L_NoDocumentLibary_Text = "No document library is selected for the save location.";
        alert(L_NoDocumentLibary_Text);
        return false;
    }
    return true;
}
function DoTemplateOptionChange()
{ULSEhF:;
    var frmWebPage = document.forms.<%SPHttpUtility.NoEncode(Form.ClientID,Response.Output);%>;
    var index = frmWebPage.WebPartPageTemplate.selectedIndex;
    frmWebPage.PreviewImage.src = strImagePath + "mywpptd" + frmWebPage.WebPartPageTemplate.options[index].value + ".png";
    frmWebPage.PreviewImage.alt = frmWebPage.WebPartPageTemplate.options[index].text;
}

// ]]>
</script>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
  <TABLE class="ms-main" cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
    <!-- Banner -->
<%
string alternateHeader = SPControl.GetContextWeb(Context).AlternateHeader;
if (alternateHeader == null || alternateHeader == "")
{
%>
<TR>
  <TD COLSPAN=3 WIDTH=100%>
  <!--Top bar-->
 
  </TD>
</TR>
<%
}
else
{
    Server.Execute(alternateHeader);
}
%>
   
<TR>
<TD valign=top height=100% > </TD>
    <!-- Page overview -->
    <td><IMG SRC="/_layouts/images/blank.gif" width=10 height=1 alt=""></td>
    <td style="padding-top: 2px" valign="top" width="100%"> <table cellpadding=2 cellspacing=0><tr><td><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></td></tr></table>
      <TABLE  border="0" cellpadding="0" cellspacing="0" width="100%" id="diidPageOverview">
        <TR>
          <TD valign="top" colspan="3" style="padding-bottom: 10px">
            <TABLE cellpadding="0" border="0" id="Table1">
              <TR>
                <TD class="ms-descriptiontext" id="align01">
                    A Web Part Page is a collection of Web Parts that combines list data, timely information, or useful graphics into a dynamic Web page. The layout and content of a Web Part Page can be set for all users and optionally personalized by each user.  <a href="javascript:HelpWindowKey('WPPTour')">Take the Web Part Page tour!</a>
                </TD>
              </TR>
            </TABLE>
          </TD>
        </TR>
    <!-- New form UI -->
        <TR>
          <TD>
<FORM id="frmWebPage" onsubmit="return DoValidateAndSubmit();">
    <!-- Name -->
             <TABLE  border="0" width="100%" cellspacing="0" cellpadding="0" id="Table2">
                   <TR><TD class="ms-sectionline" height="1" colspan="4"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   <TR>
                     <TD nowrap rowspan="3"></TD>
                     <TD class="ms-descriptiontext" rowspan="3" valign="top"  id="align02">
                       <TABLE border="0" cellpadding="1" cellspacing="0" id="Table3">
                         <TR><TD class="ms-sectionheader" height="28" valign="top" id="200">Name</TD></TR>
                         <TR>
                           <TD id="onetidNameDescription" class="ms-descriptiontext">
                                 Type a file name for your Web Part Page.  The file name appears in headings and links throughout the site.
                           </TD>
                         </TR>
                       </TABLE>
                       <IMG SRC="/_layouts/images/blank.gif" width=275 height=1 alt="">
                     </TD>
                     <TD height="3" colspan="2" class="ms-authoringcontrols"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   </TR>
                   <TR>
                     <TD class="ms-authoringcontrols" width="10">&nbsp;</TD>
                     <TD class="ms-authoringcontrols" id="400">
                       Name:<BR>
                       <TABLE border="0" cellspacing="1">
                         <TR>
                           <TD>&nbsp;</TD>
                           <TD>
                             <INPUT id="onetidListTitle" type="Text" title="Name" name="Title" maxLength="123"><SPAN class="ms-authoringcontrols">.aspx</SPAN>
                           </TD>
                         </TR>
                         <TR>
                           <TD>&nbsp;</TD>
                           <TD class="ms-authoringcontrols">
                             <asp:CheckBox id="OverwriteCheckBox" title="Overwrite" runat="server"/>
                             Overwrite if file already exists?
                           </TD>
                         </TR>
                       </TABLE>
                     </TD>
                   </TR>
                   <TR><TD class="ms-authoringcontrols" colspan="2" height="6"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   <TR><TD colspan="2">&nbsp;</TD><TD class="ms-authoringcontrols" colspan="2" height="21">&nbsp;</TD></TR>
    <!-- Layout -->
                   <TR><TD class="ms-sectionline" height="1" colspan="4"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   <TR>
                     <TD nowrap rowspan="3"></TD>
                     <TD class="ms-descriptiontext" rowspan="3" valign="top"  id="align03">
                       <TABLE border="0" cellpadding="1" cellspacing="0" id="Table5">
                         <TR><TD class="ms-sectionheader" height="28" valign="top" id="500">Layout</TD></TR>
                         <TR>
                           <TD id="onetidLayoutDescription" class="ms-descriptiontext">
                                 Select a layout template to arrange Web Parts in zones on the page. Multiple Web Parts can be added to each zone. Specific zones allow Web Parts to be stacked in a horizontal or vertical direction, which is illustrated by differently colored Web Parts. If you do not add a Web Part to a zone, the zone collapses (unless it has a fixed width) and the other zones expand to fill unused space when you browse the Web Part Page.
                           </TD>
                         </TR>
                        <TR><TD class="ms-descriptiontext" height="20"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                        <TR><TD align="center" class="ms-descriptiontext" height="6"><img src="/_layouts/MyApplication/Images/MyWebPartLayoutImages/mywpptd1.png" alt="Enterprise Layout – 33 / 66 Split" id="onetidPreviewImage" name="PreviewImage"/></TD></TR>
                        <TR><TD class="ms-descriptiontext" height="6"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                       </TABLE>
                     </TD>
                     <TD height="3" colspan="2" class="ms-authoringcontrols"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   </TR>
                   <TR>
                     <TD class="ms-authoringcontrols" width="10">&nbsp;</TD>
                     <TD class="ms-authoringcontrols" id="700" valign="top">Choose a Layout Template:<FONT size="3">&nbsp;</FONT><BR>
                       <TABLE border="0" cellspacing="1" id="Table6">
                         <TR>
                           <TD>&nbsp;</TD>
                           <TD>
                             <!-- Assuming the templates are named mywpptdN.aspx and the image files are named as mywpptdN.gif, where N is from 9 to 12 -->
                             <!-- 0 to 8 are used by the default web part page layouts provided by SharePoint 2010. So we start from 9 -->
                             <SELECT id="onetidWebPartPageTemplate" name="WebPartPageTemplate" size="5" onchange="DoTemplateOptionChange()">
                               <OPTION value="1" selected="true">Enterprise Layout – 33 / 66 Split</OPTION>
                               <OPTION value="2">Enterprise Layout – 66 / 33 Split</OPTION>
                               <OPTION value="3">Enterprise Layout – 100% Span</OPTION>
                               <OPTION value="4">Enterprise Layout – 33 / 66 / 33 / 33 Split</OPTION>
                               <OPTION value="5">Enterprise Layout – 33 / 33 / 33 Split</OPTION>
                             </SELECT>
                           </TD>
                           <TD>&nbsp;</TD>
                         </TR>
                       </TABLE>
                     </TD>
                   </TR>
                   <TR><TD class="ms-authoringcontrols" colspan="2" height="6"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                  <TR><TD colspan="2">&nbsp;</TD><TD class="ms-authoringcontrols" colspan="2" height="21">&nbsp;</TD></TR>
    <!-- Save Location -->
                   <TR><TD class="ms-sectionline" height="1" colspan="4"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   <TR>
                     <TD nowrap rowspan="3"></TD>
                     <TD class="ms-descriptiontext" rowspan="3" valign="top"  id="align04">
                       <TABLE border="0" cellpadding="1" cellspacing="0" id="Table7">
                         <TR><TD class="ms-sectionheader" height="28" valign="top" id="800">Save Location</TD></TR>
                         <TR>
                           <TD id="onetidSaveLocationDescription" class="ms-descriptiontext">
                                 Select the document library where you want the Web Part Page to be saved.
                           </TD>
                         </TR>
                       </TABLE>
                     </TD>
                     <TD height="3" colspan="2" class="ms-authoringcontrols"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   </TR>
                   <TR>
                     <TD class="ms-authoringcontrols" width="10">&nbsp;</TD>
                     <TD class="ms-authoringcontrols" id="900" valign="top"><label for="onetidDocLibIDSelect">Document Library</label>:<FONT size="3">&nbsp;</FONT><BR>
                       <TABLE border="0" cellspacing="1" id="Table8">
                         <TR>
                           <TD>&nbsp;</TD>
                           <TD>
                             <SELECT id="onetidDocLibIDSelect" runat="server"/>
                           </TD>
                           <TD>&nbsp;</TD>
                         </TR>
                         <TR>
                           <TD>&nbsp;</TD>
                           <TD class="ms-authoringcontrols">
                             <asp:Label id="onetidCreateDocLibLabel" runat="server"/><a id="onetidCreateDocLibLink"  runat="server"/>
                           </TD>
                           <TD>&nbsp;</TD>
                         </TR>
                       </TABLE>
                     </TD>
                   </TR>
                   <TR><TD class="ms-authoringcontrols" colspan="2" height="6"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></TD></TR>
                   <TR><TD colspan="2">&nbsp;</TD><TD class="ms-authoringcontrols" colspan="2" height="21">&nbsp;</TD></TR>
  <!--OK/Cancel-->
               <TR><TD colspan="4" valign="top" height="25"><hr size="1"></TD></TR>
               <TR><TD colspan=4> <TABLE cellpadding=0 cellspacing=0 width=100%> <COLGROUP> <COL width=99%> <COL width=1%> </COLGROUP> <TR> <TD>&nbsp;</TD> <TD nowrap id=align06>
                  <asp:button ID="btnCreate" text="   Create   " AccessKey="C" CssClass="ms-ButtonHeightWidth" OnClick="SubmitBtn_Click" runat="server"/>
                  <INPUT id="onetidClose" class="ms-ButtonHeightWidth" type="button" onclick="window.parent.history.back()" value="   Cancel   ">
                  <SharePoint:FormDigest ID="FormDigest1" runat=server/>
               </TD> </TR> </TABLE> </TD></TR>
               <TR><TD colspan="4" height="60">&nbsp;</td></TR>
             </TABLE>
             <TD width="10px">&nbsp;</TD>
            </FORM>
          </TD>
        </TR>
      </TABLE>
    </TD>
    </TR>
  </TABLE>

</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Create Custom Web Part Pages
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Create Custom Web Part Pages
</asp:Content>