0

I have a list with column "English Desc". How do I read it in my web part?

I can see that in web part, this item has other properties such as item.ID, item.Name, item.Url but this property/column is not there.

EDIT

This is my code. I have changed column name from "English Desc" to "EnglishDesc" in my list just to keep it simple. Getting error "Value does not fall within the expected range." on line MessageBox.Show

String siteName = "http://win-qmdf4x092kh/sites/mysite";
String listName = "Announcements";
String viewName = "All Items";

SPSite site = new SPSite(siteName);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[listName];
SPQuery query = new SPQuery(list.Views[viewName]);

query.RowLimit = 1;

String caml = "";

caml = "<orderby>";
caml += "<fieldref name=\"created\" ascending=\"false\" />";
caml += "</orderby>";

query.Query = caml;
query.ViewFields = "<FieldRef Name=\"EnglishDesc\"/>";
SPListItemCollection result = list.GetItems(query);

foreach (SPListItem i in result)
{
      MessageBox.Show(i["EnglishDesc"].ToString());
}

1 Answer 1

2

If you defined your model with SPMetal without overriding the output it might explain why you don't have it (http://socialsp.com/2009/12/11/having-fun-with-the-new-linq-to-sharepoint-on-sharepoint-2010-sp2010/).

Basically you need to create an Xml file which overrides what fields you would like to be added to the definition - http://msdn.microsoft.com/en-us/library/ee535056(v=office.14).aspx

When using list querying techniques (such as CAML based), you could rely on using SPQuery and include your column in the returned resulting columns. Most important here is to identify the actual Internal Name of your column, either via Visual Studio Server Explorer or via the Browser Address bar while choosing Edit Column in the list/library or the Content type:

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{

    SPList oList = oWebsiteRoot.Lists.TryGetList("MyListTitle");
if (oList!=null)
{
    SPQuery myQuery = new SPQuery();
    myQuery.Query = "<Where><Eq><FieldRef Name='Title'/>" +
        "<Value Type='Text'>Completed</Value></Eq></Where>";
    myQuery.ViewFields = "English_x200_Desc";
    SPListItemCollection colItems = oList.GetItems(myQuery);

    //or 
    //DataTable resultsTable = colItems.GetDataTable();

    foreach (SPListItem oListItem in colItems)
    {
        //do something
    }

}
}

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.aspx

5
  • Ok if I want to use CAML, how do I do that? I just made my first application that uses CAML so now I want to learn how to use my custom column "English Desc" in it. Commented Apr 15, 2013 at 8:51
  • I would suggest you use the SpQuery (msdn.microsoft.com/en-us/library/…). I've updated my answer with a simple example Commented Apr 15, 2013 at 8:55
  • I compared your code with mine and saw that the only difference is you are using "myQuery.ViewFields" so I also used it in my code. Since I don't need any WHERE clause so I am not using it like you did. But now when I try to do something in that last FOR loop, for e.g. MessageBox.Show(oListItem["Title"].ToString()) OR MessageBox.Show(oListItem["English Desc"].ToString()) I get error that "Value does not fall within the specified range" Commented Apr 15, 2013 at 9:59
  • I have added my code above. Commented Apr 15, 2013 at 10:35
  • Ok I fixed it. I was not using correct name/reference from the browser address bar like you said. Commented Apr 15, 2013 at 12:24

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.