Search This Blog

Saturday, September 5, 2015

Multiple Row Delete in GridView using SharePoint List Data

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ 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" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DMRGridViewUserControl.ascx.cs" Inherits="EUDGridView.DMRGridView.DMRGridViewUserControl" %>
<script type="text/javascript" language="javascript">
    function DeleteConfirmation() {
        if (confirm("Are you sure, you want to delete selected records ?") == true)
            return true;
        else
            return false;
    }
</script>
<table align="left" bgcolor="navy">
<tr>
<td>
<span>
<asp:Button ID="btnCheckAll" runat="server" Text="Check All"
        onclick="btnCheckAll_Click" />
</span>
<span>
<asp:Button ID="btnUncheckAll" runat="server" Text="UnCheck All"
        onclick="btnUncheckAll_Click" />
</span>
<span>
<asp:Button ID="btnDelete" runat="server" Text="Delete"
        OnClientClick="return DeleteConfirmation();" onclick="btnDelete_Click" /></span>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="dgvMultipleRecordsDelete" runat="server"
    AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" Width="600px">
    <AlternatingRowStyle BackColor="White"   />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkMultiple" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemID">
<ItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>'  />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Player Name" DataField="Title" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</td>
</tr>
<tr>
<td bgcolor="white">
<asp:Label ID="lblMessage"  runat="server"></asp:Label>
</td>
</tr>
</table>


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Collections;
namespace EUDGridView.DMRGridView
{
    public partial class DMRGridViewUserControl : UserControl
    {
        string strListID = string.Empty;
        ArrayList ArrItem = new ArrayList();
        int count;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)

                    getData();

            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
        public void getData()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Players Location"];
                    strListID = lst.ID.ToString();
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    count = myColl.Count;
                    if (myColl.Count > 0)
                    {
                        dgvMultipleRecordsDelete.DataSource = myColl.GetDataTable();
                        dgvMultipleRecordsDelete.DataBind();
                    }
                });
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleDelete;
                foreach (GridViewRow gi in dgvMultipleRecordsDelete.Rows)
                {
                    chkMultipleDelete = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    if (chkMultipleDelete.Checked == true)
                    {
                        ArrItem.Add(dgvMultipleRecordsDelete.DataKeys[gi.RowIndex].Value);
                    }

                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Players Location"];
                    string strBatch = string.Empty;
                    strBatch += "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>";
                    for (int i = 0; i < ArrItem.Count; i++)
                    {
                        strBatch += "<Method><SetList Scope=\"Request\">" + lst.ID + "</SetList><SetVar Name=\"ID\">" + ArrItem[i].ToString() + "</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
                    }
                    strBatch += "</Batch>";
                    string strProcessBatch = currentWeb.ProcessBatchData(strBatch);
                    getData();
                    if (count == 0)
                    {
                        Response.Redirect(currentWeb.Url + "/UserPages/Multiple Row Delete in GridView.aspx");
                    }
                });

            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnCheckAll_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleDelete;
                foreach (GridViewRow gi in dgvMultipleRecordsDelete.Rows)
                {
                    chkMultipleDelete = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    chkMultipleDelete.Checked = true;
                }
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnUncheckAll_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleDelete;
                foreach (GridViewRow gi in dgvMultipleRecordsDelete.Rows)
                {
                    chkMultipleDelete = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    if (chkMultipleDelete.Checked == true)
                    {
                        chkMultipleDelete.Checked = false;
                    }
                }
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.ToString();
            }                                                         
        }
    }
}





Creating a Web Part Project(Using Webpart(Dynamic Creation))





using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace EUDGridView.ContactFormWebPart
{
    [ToolboxItemAttribute(false)]
    public class ContactFormWebPart : WebPart
    {
        TextBox txtContactName;
        TextBox txtEmailAddress;
        TextBox txtPhone;
        TextBox txtMessage;
        Button btnSendMessage;
        Label lblMessageSent;
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Table t;
            TableRow tr;
            TableCell tc;

            // A table that is used to layout the controls
            t = new Table();
                         
            // Label with instructions for the user
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblInstructions = new Label();
            lblInstructions.Text = "Please enter your contact" +
                                   " information and message below.";
            tc.Controls.Add(lblInstructions);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Contact Name label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblContactName = new Label();
            lblContactName.Text = "Name:";
            tc.Controls.Add(lblContactName);
            tr.Controls.Add(tc);

            // Contact Name textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtContactName = new TextBox();
            txtContactName.ID = "txtContactName";
            txtContactName.Width = Unit.Pixel(300);
            tc.Controls.Add(txtContactName);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Email Address label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblEmailAddress = new Label();
            lblEmailAddress.Text = "Email Address:";
            tc.Controls.Add(lblEmailAddress);
            tr.Controls.Add(tc);

            // Email Address textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtEmailAddress = new TextBox();
            txtEmailAddress.ID = "txtEmailAddress";
            txtEmailAddress.Width = Unit.Pixel(300);
            tc.Controls.Add(txtEmailAddress);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Phone Number label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblPhone = new Label();
            lblPhone.Text = "Phone Number:";
            tc.Controls.Add(lblPhone);
            tr.Controls.Add(tc);

            // Phone Number textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtPhone = new TextBox();
            txtPhone.ID = "txtPhone";
            txtPhone.Width = Unit.Pixel(300);
            tc.Controls.Add(txtPhone);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Message label
            tr = new TableRow();
            tc = new TableCell();
            tc.Style["padding-top"] = "7px";
            tc.VerticalAlign = VerticalAlign.Top;
            Label lblMessage = new Label();
            lblMessage.Text = "Message:";
            tc.Controls.Add(lblMessage);
            tr.Controls.Add(tc);

            // Message textbox
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            txtMessage = new TextBox();
            txtMessage.ID = "txtMessage";
            txtMessage.Height = Unit.Pixel(100);
            txtMessage.Width = Unit.Pixel(400);
            txtMessage.TextMode = TextBoxMode.MultiLine;
            txtMessage.Wrap = true;
            tc.Controls.Add(txtMessage);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Empty table cell
            tr = new TableRow();
            tc = new TableCell();
            tr.Controls.Add(tc);

            // Label for telling the user the message was sent
            tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            lblMessageSent = new Label();
            lblMessageSent.Text = "Your message has been sent. Thank you.";
            lblMessageSent.Font.Bold = true;
            lblMessageSent.Visible = false;
            tc.Controls.Add(lblMessageSent);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            // Empty table cell
            tr = new TableRow();
            tc = new TableCell();
            tr.Controls.Add(tc);

            // Send Message button
            tc = new TableCell();
            btnSendMessage = new Button();
            btnSendMessage.Text = "Send Message";
            btnSendMessage.Click += new EventHandler(btnSendMessage_Click);
            tc.Controls.Add(btnSendMessage);
            tr.Controls.Add(tc);

            t.Controls.Add(tr);

            this.Controls.Add(t);
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            // Build the email subject string
            System.Text.StringBuilder subject = new System.Text.StringBuilder();
            subject.Append("Contact Form Message from ");
            subject.Append(txtContactName.Text);

            // Build the email message string
            System.Text.StringBuilder message = new System.Text.StringBuilder();
            message.Append("Contact Name: ");
            message.AppendLine(txtContactName.Text);
            message.Append("Email Address: ");
            message.AppendLine(txtEmailAddress.Text);
            message.Append("Phone: ");
            message.AppendLine(txtPhone.Text);
            message.AppendLine();
            message.AppendLine("Message:");
            message.AppendLine(txtMessage.Text);

            // Construct the message header
            System.Collections.Specialized.StringDictionary messageHeader =
            new System.Collections.Specialized.StringDictionary();
            // TODO: Where to send the email
            messageHeader.Add("to", "CustomerService@example.com");
            // TODO: Who the email should be "from"
            messageHeader.Add("from", "ContactForm@example.com");
            messageHeader.Add("subject", subject.ToString());
            messageHeader.Add("content-type", "text/plain");

            // Send the email
            Microsoft.SharePoint.Utilities.SPUtility.SendEmail(
            SPContext.Current.Web, messageHeader, message.ToString());

            // Let the user know the message was sent
            lblMessageSent.Visible = true;

            // Clear out the input fields
            txtContactName.Text = "";
            txtEmailAddress.Text = "";
            txtPhone.Text = "";
            txtMessage.Text = "";
        }
    }
}



Wednesday, September 2, 2015

How to Enable ‘Save Site as Template’ in SharePoint 2013 Using SharePoint Designer

Are you attempting to save a Web Part page as a template but cannot see 'Save site as template' when you go to Site Settings > Site Actions? This post will show you how to enable 'Save site as template' in Site Actions.
Go to Start > All Programs > SharePoint Designer 2013 and open your site.
In Site Name, type your site (for example: my site is http://vngo15sql) and click Open button.
Enter user name and password then OK.
Click Site Options on the top ribbon.
Find SaveSiteAsTemplateEnabled on Parameters tab. 
Click Modify button to change the Value from 'false' to 'true', click OK, then Apply, and click OK. 
Finally, open your site in the browser and launch it.