1

I currently am developing a SharePoint Web part that will populate some stuff based of the of the List. Everything is working as expected but I can't seem to access the variable that has all the list items!

The Variable I want to access is theValue1.

Code:

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

namespace SPListWebPart.VisualWebPart1 {
    public partial class VisualWebPart1UserControl : UserControl {
        public void Page_Load(object sender, EventArgs e) {
            string theValue1;
            SPSite site = new SPSite("http://maindt/sites/dev");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["Links"];

            foreach (SPListItem item in list.Items) {
                theValue1 = item["URL"].ToString();
            }
        }
    }    
}
1
  • 2
    Not sure what your design is, but theValue1 is a string, not a list, and its value gets overwritten on every iteration of the loop. Jul 5, 2013 at 20:06

3 Answers 3

2

To get around the error message, you just need to provide a default value:

string theValue1 = string.Empty;

That being said, your current code will just set theValue1 to the last URL in list.Items, so your loop could be rewritten as:

theValue1 = list.Items.Last()["URL"].ToString();

If you want the string to represent all of the items, you might want to consider using something like:

theValue1 = string.Join(", ", list.Items.Select(item => item["URL"].ToString()));

If you want to store a List<string> of all of the URLs, you could use:

List<string> theValues = list.Items.Select(i => i["URL"].ToString()).ToList();
0
  • You are overwriting the theValue1 in each iteration of your loop.
  • If you want to access your variable somewhere else, put it outside of the Page_Load function
0

You need to set a default value for it, else what happens if there's no items?

Also, your current code just gets the last value and puts it in the string. If you want all the values use this:

public void Page_Load(object sender, EventArgs e)
{
    string theValue1 = list.Items.Last()["URL"].ToString();
    SPSite site = new SPSite("http://maindt/sites/dev");
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists["Links"];
}

However, if you want all the values you're looking for an Enumerable and Select:

public void Page_Load(object sender, EventArgs e)
{
    IEnumerable<string> theValues1 = list.Items.Select(i => i["URL"].ToString());
    SPSite site = new SPSite("http://maindt/sites/dev");
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists["Links"];
}
1
  • 1
    Also you should be using a using block as SPsite and SPweb will not be disposed of correctly
    – ben
    Jul 8, 2013 at 15:38

Your Answer

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

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