Store specific data from SPListItem data?

424 views Asked by At

I have the following C# code:

using (SPSite site = new SPSite("http://mysharepointsiteurl")
{
    using (SPWeb web = site.OpenWeb())
    {
        SPListItemCollection itemCollection = web.Lists["List Name"].Items;

        foreach (SPListItem item in itemCollection)
        {
            Console.WriteLine(item["Field Name"]);
            // prints 5 different results.
        }
    web.Dispose();
}
site.Dispose();

Is there anyway I can get, say the 4th printed out result, and store it in a string? I'm sure there is a way but I can't seem to work it out. Thanks for any help! :)

1

There are 1 answers

1
Servy On BEST ANSWER

You can use the indexer to get the item at a given position:

using (SPSite site = new SPSite("http://mysharepointsiteurl"))
using (SPWeb web = site.OpenWeb())
{
    var items = web.Lists["List Name"].GetItems("Field Name");
    string value = (string)items[3]["Field Name"];
}