Tuesday 3 December 2013

Create sharepoint list custom form via visual studio 2012

Share it Please
1.open visual studio 2012
2.Add Sharepoint 2013 Empty project Named it "Employee"
3.Create a custom list name as "Employee" and add the below columns

4.Right click the project add the layouts folder inside that add one Application page named it "EmpCustomForm"
5.Paste the below code inside the Content ID "Main"


      <p style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif">&nbsp; Employee Custom Form</p>
    <table>
                <tr>
                    <td class="auto-style1">
                        <asp:Label ID="LblTitle" runat="server" Text="Title: "></asp:Label>
                    </td>
                   
                    <td>
                         <asp:TextBox ID="Txttile" runat="server" Height="46px"  Width="470px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                         <asp:Label ID="LblName" runat="server" Text="Name : "></asp:Label>
                    </td>
                    
                    <td>
                         <asp:TextBox ID="TxtName" runat="server" Height="46px"  Width="470px"></asp:TextBox>
                    </td>
                </tr>
                 <tr>
                    <td>
                         <asp:Label ID="LblAge" runat="server" Text=" Age : "></asp:Label>
                    </td>

                    <td>
                           <asp:TextBox ID="TxtAge" runat="server" Height="46px"  Width="470px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                         <asp:Label ID="LblEmail" runat="server" Text=" Email : "></asp:Label>
                    </td>
                    
                    <td>
                        <asp:TextBox ID="TxtEmail" runat="server" Height="46px"   Width="470px"></asp:TextBox>
                    </td>
                </tr>
                       </table>
        <table>
            <tr>
                <td>
                    <asp:Button ID="BtnSave" runat="server" Text="Save" OnClick="BtnSave_Click" Width="94px" />
                </td>
                <td>
                    <asp:Button ID="BtnClear" runat="server" Text="Clear" OnClick="BtnClear_Click" Width="102px" />
                </td>
            </tr>
        </table>
6.Paste the below code inside the cs page

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
            var itemId = 0;
          
            if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
            {
                int.TryParse(Request.QueryString["id"], out itemId);
            }
            if (itemId != 0)
            {
                var listItem = list.GetItemById(itemId);
                Txttile.Text = (listItem["Title"] == null) ? string.Empty : listItem["Title"].ToString();
                TxtName.Text = (listItem["EmpName"] == null) ? string.Empty : listItem["EmpName"].ToString();
                TxtAge.Text = (listItem["Age"] == null) ? string.Empty : listItem["Age"].ToString();
                TxtEmail.Text = (listItem["Email"] == null) ? string.Empty : listItem["Email"].ToString();
                SPSite sites = new SPSite(SPContext.Current.Site.ID);
                SPWeb webs = sites.OpenWeb();
                SPList lists = webs.Lists["Employee"];
                SPListItem item = listItem;
            }

        }
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                var list = SPContext.Current.Web.Lists[new Guid(Request.QueryString["list"])];
                string ItemsID = string.Empty;
                var itemId = 0;
                if (string.IsNullOrEmpty(Request.QueryString["id"]) == false)
                {
                    int.TryParse(Request.QueryString["id"], out itemId);
                }
                SPListItem listItem = null;
                if (itemId == 0)
                {
                    listItem = list.AddItem();
                }
                else
                {
                    listItem = list.GetItemById(itemId);
                }
                ItemsID = Convert.ToString(itemId);
                listItem["Title"] = Txttile.Text;
                listItem["EmpName"] = TxtName.Text;
                listItem["Age"] = TxtAge.Text;
                listItem["Email"] = TxtEmail.Text;
                listItem.Update();
            });
            Txttile.Text = string.Empty;
            TxtAge.Text = string.Empty;
            TxtName.Text = string.Empty;
            TxtEmail.Text = string.Empty;
        }
        protected void BtnClear_Click(object sender, EventArgs e)
        {
            Txttile.Text = string.Empty;
            TxtAge.Text = string.Empty;
            TxtName.Text = string.Empty;
            TxtEmail.Text = string.Empty;

        }
7.Go to the Employee List click the Schema.xml add the below lines

        <XmlDocuments>
          <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
            <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
              <Display>/sites/Test1/_layouts/15/Employee/EmpCustomForm.aspx</Display>
              <Edit>/sites/Test1/_layouts/15/Employee/EmpCustomForm.aspx</Edit>
              <New>/sites/Test1/_layouts/15/Employee/EmpCustomForm.aspx</New>
            </FormUrls>
          </XmlDocument>
        </XmlDocuments>
8. ouput



No comments:

Post a Comment

Followers

Follow The Author