2

I'd like to know how to get values from SharePoint list column in custom WebPart and I want to display it in the visual webPart.

 splist lists = web.list["list name"];
 spitem item = list.additem();
 item["Title"] = "doc";
 item["No"] = "1";'

I can use code something similar to the above to assign value in list column from visual WebPart. Is there a solution for the other way around -- to get the value from SharePoint list and display it in visual WebPart?

dnet
  • 1,411
  • 9
  • 19
MRu
  • 1,225
  • 7
  • 20
  • 44
  • 1
    I would start with some intro tutorials on using sharepoint. You haven't even described how you want to know what item to get. Do you want an item by ID, all items in a list, an item where the value of a particular column is a fixed value, or some more complex query. – Servy Nov 29 '12 at 15:56

1 Answers1

2

there are lots of examples to populate a literal,label etc with the content of a list. you also might want to filter what you want to display. This is some code to get you started, if you require a more precise result just let us know what is the final result you wish to achieve:

 using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lists = web.Lists["list name"];
                    foreach (SPListItem itemin lists.Items)
                    {
                       string test = Convert.ToString( item["test"]);
                        TextBox1.Text = test;

                    }
                }
             }
user1211929
  • 1,190
  • 1
  • 12
  • 32
  • im really new with this so bare with me. but how im going to assign for specific column sharepoint list is for specific textbox in web part? – MRu Nov 29 '12 at 16:08
  • I've changed the code slightly it uses the SPListItems instead. I recommend to take some time and study how C# deals with share point lists and columns, it takes time but is worth it. – user1211929 Nov 29 '12 at 16:29
  • it does the job but it only get the latest new item that i added. when i click the previous items it still only shows the latest item that i added. how can i make it to show only related item that i clicked? – MRu Nov 30 '12 at 02:10
  • i already test to change the url and id but still get the same result. only the latest item in the list is shown in web part. where did it get the value from actually? isn't it from the url that we set? – MRu Nov 30 '12 at 02:45
  • owh i get it now. just use GetItemById to view desired list. thanks! – MRu Nov 30 '12 at 03:42