Jpaul
Jpaul

Reputation: 278

How to get a value of a list in SharePoint?

I have created a calendar list and I have added few columns to that list. Now I wanted to retrieve a value of a particular list item from the calendar list. List name -->
Calendar Fields --> Program Name , Start Date, End Date, Facilitator, Total Seat, etc....

I would like to retrive value of "Total Seat" alone.

namespace CourseCount.EventReceiver1 
{ 
  public override void ItemAdded(SPItemEventProperties properties) 
  { 
    base.ItemAdded(properties); 
    SPSite oSiteCollection = SPContext.Current.Site; 
    SPWeb myWeb = SPContext.Current.Web; 
    SPList spList = myWeb.Lists["Calendar"]; 
    SPListItemCollection spListItemCollection = spList.Items;
    SPListItem item = spListItemCollection.Add(); 
    int count = item["Total Seat"]; // <-here is my doubt 
  } 
} 

Upvotes: 0

Views: 6116

Answers (3)

skorzinetzki
skorzinetzki

Reputation: 83

Although the question is months ago... I stepped over a similar problem and got a solution, that I want to share right here. With my code example I extract the value out of a SPCalculatedField of type number into a float variable. I think you could use this for your problem, too. The variable item is of type SPListItem. The function GetFormattedValue comes with the SharePoint API.

float count;
bool success = float.TryParse(item.GetFormattedValue("Total Seat"), out count);

Upvotes: 1

Stefan
Stefan

Reputation: 14880

I guess you want to read the Total Seat field from the item triggering the event receiver. You can access the SPListItem via the SPItemEventProperties:

public override void ItemAdded(SPItemEventProperties properties)
{
  int totalSeal = (int)properties.ListItem["Total Seat"];
  // ...
}

Upvotes: 3

Shegit Brahm
Shegit Brahm

Reputation: 717

Did you went through with debugger? Referring to your 2nd comment, I just would do it a bit different because I have doubt that this will work without problems. But if your previous calls were successful without any converting, this should be too. What you have to determine is that this column exists.

int count = Convert.ToInt32(item["Total Seal"].ToString());

Upvotes: 1

Related Questions