JG Vimalan's Blog

It is all about coding!

Dynamically create contols in custom web part to perform batch update.

Hi, 

I got a requirement that, user shall be able to view all items in a sharepoint list and perform a batch update. That is, at one go, user shall view all items and modify values and perform a batch update. Here is the code and snapshots. 

Target List: 

This is the list going to undergo batch update…

dy1

The item (Technology) is going to be updated by the user at one go. 

Code: 

Created a custom webpart to read all values from this list and dynamic controls to display the values from Technology column. 

Modify the tool bar to have only the ‘Update’ menu item and make redirection to the webpart page. 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;

namespace DynamicControlsLibrary
{
    public class DynamicControls:System.Web.UI.WebControls.WebParts.WebPart
    {
        Table _CommonContainer = null;
        Button _UpdateButton = null;

        protected override void CreateChildControls()
        {
            _CommonContainer = new Table();

            TableRow fr = new TableRow();
           
            TableCell frfc = new TableCell();
            frfc.Text = “ID”;
            fr.Cells.Add(frfc);

            TableCell frsc = new TableCell();
            frsc.Text = “Technology”;
            fr.Cells.Add(frsc);

            _CommonContainer.Rows.Add(fr);

            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                {
                    SPList list = web.Lists[“TechnicalSkills”];

                    foreach (SPListItem item in list.Items)
                    {
                        TableRow dyr = new TableRow();

                        TableCell dyrfc = new TableCell();
                        Label id = new Label();
                        id.ID = “L” + item.ID.ToString();
                        id.Text = item.ID.ToString();
                        dyrfc.Controls.Add(id);

                        TableCell dyrsc = new TableCell();
                        TextBox tech = new TextBox();
                        tech.ID = “T” + item.ID.ToString();
                        tech.Text = item[“Title”].ToString();
                        dyrsc.Controls.Add(tech);

                        dyr.Cells.Add(dyrfc);
                        dyr.Cells.Add(dyrsc);

                        _CommonContainer.Rows.Add(dyr);
                    }
                }
            }

            TableRow tr = new TableRow();

            TableCell trfc = new TableCell();
            tr.Cells.Add(trfc);

            TableCell trsc = new TableCell();
            _UpdateButton = new Button();
            _UpdateButton.ID = “_UpdateBtn”;
            _UpdateButton.Text = “Update”;
            _UpdateButton.Click += new EventHandler(_UpdateButton_Click);
            trsc.Controls.Add(_UpdateButton);

            tr.Cells.Add(trsc);

            _CommonContainer.Rows.Add(tr);

            this.Controls.Add(_CommonContainer);
        }

        void _UpdateButton_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                {
                    SPList list = web.Lists[“TechnicalSkills”];

                    ReadData(list);                  
                }
            }
        }

        void ReadData(SPList list)
        {
            foreach (TableRow row in _CommonContainer.Rows)
            {
                int id = 0;

                foreach (TableCell cell in row.Cells)
                {
                    if (cell.Controls.Count > 0)
                    {
                        Control control = cell.Controls[0];

                        if (control.GetType().ToString().Equals

                                            (“System.Web.UI.WebControls.Label”) &&
                            control.ID.IndexOf(“L”) == 0)
                        {
                            Label label = (Label)control;
                            id = Convert.ToInt32(label.Text);
                        }

                        if (control.GetType().ToString().Equals

                                             (“System.Web.UI.WebControls.TextBox”) &&
                            control.ID.IndexOf(“T”) == 0)
                        {
                            TextBox textbox = (TextBox)control;
                            SPListItem item = list.GetItemById(id);
                            item[“Title”] = textbox.Text;
                            item.Update();
                        }
                    }
                }
            }
        } 

        void ReadControls(Control thisPage, SPList list)
        {
            foreach (Control control in thisPage.Controls)
            {
                int id = 0;

                if (control.GetType().ToString().Equals

                                             (“System.Web.UI.WebControls.Label”) &&
                    control.ID.IndexOf(“L”) == 0)
                {
                    Label label = (Label)control;
                    id = Convert.ToInt32(label.Text);
                }

                if (control.GetType().ToString().Equals

                                              (“System.Web.UI.WebControls.TextBox”) &&
                    control.ID.IndexOf(“T”) == 0)
                {
                    TextBox textbox = (TextBox)control;
                    SPListItem item = list.GetItemById(id);
                    item[“Title”] = textbox.Text;
                    item.Update();
                }

                if (control.Controls.Count > 0)
                {
                    ReadControls(control, list);
                }
            }
        }

        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            this.EnsureChildControls();
            _CommonContainer.RenderControl(writer);
        }
    }

OutPut: 

After deploying the webpart, you will be able to see the output as shown below,

dy2_1

Happy coding!!!

JG Vimalan

Advertisements

August 6, 2009 - Posted by | SharePoint

3 Comments »

  1. thanks

    Comment by arreniareab | November 2, 2010 | Reply

  2. Great post, I am almost 100% in agreement with you

    Comment by escophilecice | December 9, 2010 | Reply

  3. Nice site with useful info

    Comment by Azeez | February 19, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: