1

I have a custom list ("Contacts") as well as a web part with forms. I want to take the custom list fields and map them to the web part form. My problem is getting the values from the custom list. I've tried looking through both list and field properties but I can't seem to find one that gives the value. I can get everything known to man about the value.

I know it's weird, but I'm just trying to familiarize myself.

protected void Page_Load(object sender, EventArgs e)
    {
        SPSite site = SPContext.Current.Site;
        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists["Contacts"];
        SPListItemCollection listItem = list.Items;


        TextCompany.Text = listItem[0].Name;//Returns the list item title
        TextFirstName.Text = listItem[0].ToString();//Returns "Microsoft.SharePoint.SPListItem"
    }

1 Answer 1

1

List Item property values are accessed with an indexer. So if you want the value of the "foo" column, then you would write listItem[0]["foo"]

1
  • Worked like a charm. Thanks for helping out us beginners. Commented May 25, 2012 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.