Search This Blog

Thursday, March 12, 2015

Bulk Edit and Update of SharePoint List Items within a GridView



<%@ 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="MEUGridViewUserControl.ascx.cs" Inherits="EUDGridView.MEUGridView.MEUGridViewUserControl" %>
 <table align="left">
<tr>
<td>
<span>
<asp:Button ID="btnCheckAll" runat="server" Text="Check All"
        onclick="btnCheckAll_Click" />
</span>
<span>&nbsp;</span>
<span>
<asp:Button ID="btnUncheckAll" runat="server" Text="UnCheck All"
        onclick="btnUncheckAll_Click" />
</span>
<span>&nbsp;</span>
<span>
<asp:Button ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click"  />
</span>
<span>&nbsp;</span>
<span>
<asp:Button ID="btnUpdate" runat="server" Text="Update"
        onclick="btnUpdate_Click"  />
</span>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="dgvMultipleRecordsEditUpdate" runat="server"
    AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" Width="600px">
    <AlternatingRowStyle BackColor="White"  />
<Columns>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:CheckBox ID="chkMultiple" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate >
<span>ID</span>
<span>Player Name</span>
<span>Location</span>
</HeaderTemplate>
<ItemTemplate>
<asp:Panel ID="pnlLabels" runat="server">
<span><%# Eval("ID") %></span>
<span><%# Eval("Title") %></span>
<span><%# Eval("Location") %></span>
</asp:Panel>
<asp:Panel ID="pnlControls" runat="server" Visible="false">
<span style="text-align:center;"><asp:Label ID="lblItemID" Text='<%# Eval("ID") %>' runat="server"></asp:Label> </span>
<span style="text-align:center;"><asp:TextBox ID="txtPlayerName" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox></span>
<span style="text-align:center;"><asp:TextBox ID="txtLocation" runat="server" Text='<%# Eval("Location") %>'></asp:TextBox></span>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</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.MEUGridView
{
    public partial class MEUGridViewUserControl : UserControl
    {
        ArrayList ArrItemID = new ArrayList();
        ArrayList ArrPlayerNames = new ArrayList();
        ArrayList ArrLocation = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
            }
        }

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

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

        protected void btnUncheckAll_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleEdit;
                Panel pnlLabels1;
                Panel pnlControls1;
                foreach (GridViewRow gi in dgvMultipleRecordsEditUpdate.Rows)
                {
                    chkMultipleEdit = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    chkMultipleEdit.Checked = false;
                    if (chkMultipleEdit.Checked == false)
                    {
                        pnlLabels1 = (Panel)gi.Cells[0].FindControl("pnlLabels");
                        pnlControls1 = (Panel)gi.Cells[0].FindControl("pnlControls");
                        if (pnlControls1.Visible == true)
                        {
                            pnlControls1.Visible = false;
                        }

                        if (pnlLabels1.Visible == false)
                        {
                            pnlLabels1.Visible = true;
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleEdit;
                Panel pnlLabels1;
                Panel pnlControls1;
                foreach (GridViewRow gi in dgvMultipleRecordsEditUpdate.Rows)
                {
                    chkMultipleEdit = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    pnlLabels1 = (Panel)gi.Cells[0].FindControl("pnlLabels");
                    pnlControls1 = (Panel)gi.Cells[0].FindControl("pnlControls");
                    if (chkMultipleEdit.Checked == true)
                    {
                        pnlLabels1.Visible = false;
                        pnlControls1.Visible = true;
                    }
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkMultipleEdit;
                TextBox txtPlayerNames1;
                TextBox txtLocation1;
                foreach (GridViewRow gi in dgvMultipleRecordsEditUpdate.Rows)
                {
                    chkMultipleEdit = (CheckBox)gi.Cells[0].FindControl("chkMultiple");
                    txtPlayerNames1 = (TextBox)gi.Cells[1].FindControl("txtPlayerName");
                    txtLocation1 = (TextBox)gi.Cells[1].FindControl("txtLocation");
                    if (chkMultipleEdit.Checked == true)
                    {
                        ArrItemID.Add(dgvMultipleRecordsEditUpdate.DataKeys[gi.RowIndex].Value);
                        ArrPlayerNames.Add(txtPlayerNames1.Text);
                        ArrLocation.Add(txtLocation1.Text);
                    }

                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Players Location"];
                    string strBatch = string.Empty;
                    string strBatchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ows:Batch OnError=\"Return\">{0}</ows:Batch>";
                    int k = 1;
                    for (int i = 0; i < ArrItemID.Count; i++)
                    {
                        strBatch += "<Method ID=\"{0}\">";
                        strBatch += "<SetList>" + lst.ID + "</SetList>";
                        strBatch += "<SetVar Name=" + "\"ID\">" + ArrItemID[i].ToString() + "</SetVar>";
                        strBatch += "<SetVar Name=\"Cmd\">Save</SetVar>";
                        strBatch += "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#Title\">" + ArrPlayerNames[i].ToString() + "</SetVar>";
                        strBatch += "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#Location\">" + ArrLocation[i].ToString() + "</SetVar>";
                        strBatch += "</Method>";
                        k++;
                        if (i == ArrItemID.Count)
                        {
                            break;
                        }
                    }
                    string strCompleteBatch = string.Format(strBatchFormat, strBatch);
                    string strProcessBatch = currentWeb.ProcessBatchData(strCompleteBatch);
                    lst.Update();
                    getData();
                });
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

    }
}





No comments:

Post a Comment