Read Write in SharePoint 2010 WebPart

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

namespace TestWebPart.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        #region Get DATA
        void GetDate()
        {
            TestTB.Text = DateTime.Now.ToLongTimeString();
        }
        #endregion

        #region Read Global List
        void ReadGlobalList(string URL, string Listname,string cloName)
        {
            // Use using to make sure resources are released properly 
            using (SPSite oSite = new SPSite(URL))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    // Alternately you can use oSite.RootWeb if you want to access the main site 

                    SPList oList = oWeb.Lists[Listname];  // The display name, ie. "Calendar" 

                    foreach (SPListItem oItem in oList.Items)
                    {
                        // Access each item in the list... 
                        TestTB.Text += oItem[cloName].ToString() + " , ";
                        // etc.... 
                    }

                }
            }

        }
        #endregion

        void ReadList()
        {
            // Use using to make sure resources are released properly 
            using (SPSite oSite = new SPSite("URL HERE"))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    // Alternately you can use oSite.RootWeb if you want to access the main site 

                    SPList oList = oWeb.Lists["LISTNAME"];  // The display name, ie. "Calendar" 
                    DrawTblLtr.Text += @"<table border='5'><tr>";
                    foreach (SPListItem oItem in oList.Items)
                    {
                        // Access each item in the list... 
                        DrawTblLtr.Text += "<td>" + oItem["Date"].ToString() + "</td>";
                        // etc.... 
                    }
                    DrawTblLtr.Text += @"</tr></table>";

                }
            }

        }

        void WriteList()
        {
            using (SPSite site = new SPSite("UR URL HERE"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPListItemCollection listItems = web.Lists["LISTNAME"].Items;
                    SPListItem item = listItems.Add();
                    item["Date"] = DateTime.Now.ToLongTimeString();
                    item.Update();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //GetDate();
            ReadList();
        }

        protected void WriteListBtn_Click(object sender, EventArgs e)
        {
            WriteList();
            ReadGlobalList("UR URL HERE", "LISTNAME", "Date");
        }
    }
}

No comments:

Post a Comment