Tuesday, 6 September 2011

Lists: Retriveing datas from lists

 protected override void CreateChildControls()
        {
            if (!_error)
            {
              
                SPWeb web = SPContext.Current.Site.OpenWeb();
           
                try
                {
                    base.CreateChildControls();
                    // Your code here...
             
       
                    SPList li = web.Lists["123"];
                    SPQuery query = new SPQuery();
                    query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>aaa</Value></Eq></Where>";
                    SPListItemCollection collist = li.GetItems(query);
                    foreach (SPListItem i in collist)
                    {
                        Label l = new Label();
                        l.ID = i.ID.ToString();
                        l.Text = i.Title.ToString();
                       
                        this.Controls.Add(l);
                    }
                   
                }

                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    web.Dispose();
                }
            }
        }


Creating Lists Programatically:

 public void createList()
        {
            // choose your site
            SPSite site = new SPSite("http://merdev-moss:5050");
            SPWeb web = site.OpenWeb();
            SPListCollection lists = web.Lists;

            // create new Generic list called "My List"
            lists.Add("My List", "My list Description", SPListTemplateType.GenericList);

            SPList list = web.Lists["My List"];

            // create Text type new column called "My Column"
            list.Fields.Add("My Column", SPFieldType.Text, true);

            // make new column visible in default view
            SPView view = list.DefaultView;
            view.ViewFields.Add("My Column");
            view.Update();
        }



Creating and retrieving listsin c# webpart

 protected override void CreateChildControls()
        {
            if (!_error)
            {
              
                SPWeb web = SPContext.Current.Site.OpenWeb();
           
                try
                {
                  
                    base.CreateChildControls();
                     web.AllowUnsafeUpdates = true;
                    // creating list
                    SPListCollection newlist = web.Lists;
                    newlist.Add("2", "asd", SPListTemplateType.GenericList);
                    SPList li = web.Lists["2"];
                    li.Fields.Add("Title", SPFieldType.Text,true);
                    SPListItemCollection listitemcol= web.Lists["2"].Items;
                    SPListItem item = listitemcol.Add();
                    item["Title"] = "aa9a";
                  
                    item.Update();
                    web.AllowUnsafeUpdates = false;
                   // retrieving datas from list
                 //   SPQuery query = new SPQuery();
                //    query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>";
                //    SPListItemCollection collist = li.GetItems(query);
                    SPListItemCollection collist = li.Items;
                    foreach (SPListItem i in collist)
                    {
                        Label l = new Label();
                        l.ID = i.ID.ToString();
                        l.Text = i.Title.ToString();                    
                        this.Controls.Add(l);
                    }
                   
                }

                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    web.Dispose();
                }
            }
        }

No comments:

Post a Comment