Steps to Create Custom WebPartPage in sharepoint

1. Open VS 2008 -> File -> New -> Project -> Visual C# -> ASP.NET Web Application -> Give a Proper Name to the WebApplication for Example("MyMOSSApp").


2. Add following AppSettings and connectionStrings in Web.config file in between </configSections> and <system.web>
    
        <appSettings>
        <add key="Mywebsite" value="http://myserver:1234/"/>
    </appSettings>
    <connectionStrings>
        <add name="MyMembershipConnString" connectionString="data source=SURESH;Integrated Security=SSPI;initial catalog=Test"/>
    </connectionStrings>

Note that you have to add same AppSettings and connectionStrings in Web.config of your virtual directory between  </SharePoint> and   <system.web> tags.

3. Add Refrence of "Microsoft.SharePoint.dll" file in your WebApplication.

4. Add new ASPX Page in your WebApplication and Name it MyPage.aspx. Put following content in Form tag of your aspx page:

    <div>
            <asp:Label ID="lblMessage" runat="server"></asp:Label>
        </div>

5. Import following NameSpaces in your MyPage.aspx.cs file:

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

6. Make Sure that MyPage.aspx.cs will looks like (Notice the namespace and class name):

namespace MyMOSSApp
{
    public partial class MyPage : System.Web.UI.Page
    {
        SPSite site;
        SPWeb web;
        SPList list;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                site = new SPSite(ConfigurationManager.AppSettings["Mywebsite"]);
                web = site.OpenWeb();

                list = web.Lists["TestList"];
                SPListItemCollection spcol = list.Items;
                foreach (SPListItem item in spcol)
                {
                    lblMessage.Text += Convert.ToString(item["Title"]) + "<br/>";
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }

        }
    }
}

7. Make Sure that Page directive of your MyPage.aspx contains Inherits="MyMOSSApp.MyPage".

8. Now build your WebApplication by simply click on build or ctrl+shift+B.

9. Copy and paste MyMOSSApp.dll and "Microsoft.SharePoint.dll" from your bin directory to virtual-directory's bin folder.

10. Add safecontrol in web.config file:

    <SafeControl Assembly="MyMOSSApp" Namespace="MyMOSSApp" TypeName="*" Safe="True" />

11. Change <trust level="WSS_Minimal" originUrl="" /> to <trust level="Full" originUrl="" /> in web.config file.

12. Create a new ASPX page in your Sahrepoint Site using Sharepoint Designer:

    File -> New-> Create from Master Page -> Save it as MyPage.aspx

13. Replace the inherit property in Page directive from "Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" to "MyMOSSApp.MyPage".

14. Put Copy the content in between form tag from your WebApplication Page to the Shaprepoint Page in ContentPlaceHolder "PlaceHolderMain". Sharepoint Page Looks like follows:
    
    <asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
        <div>
                <asp:Label ID="lblMessage" runat="server"></asp:Label>
            </div>                    
    </asp:Content>

15. If you want to add any event with controls like button click, then you have to add eventhandler on page_load event in code-behind file you can't add eventhandler on html part of the page. For example:

Button1.Click += new EventHandler(Button1_Click);

16. insert following tag in <PageParserPaths></PageParserPaths> tags in web.config file if you want to use TemplateField column of gridview in the page.

    <PageParserPath VirtualPath="/MyPage.aspx" CompilationMode="Always" AllowServerSideScript="true"/>

Web.config file looks like:

    <PageParserPaths>
                <PageParserPath VirtualPath="/MyPage.aspx" CompilationMode="Always" AllowServerSideScript="true"/>
          </PageParserPaths>

Note that:  Add one <PageParserPath> tag with page name for each page on which you are using TemplateField column.
 
17. Now you can run your Page.
 

Ebook Download
View all
Learn
View all