I am trying to grab the current list item, which is an infopath form. So far I am connecting to the sharepoint site, opening the list and trying to grab the current item, but I keep getting an index out of range error. Here is my code
SPSite WebApp = new SPSite("http://site/");
SPWeb site = WebApp.OpenWeb();
site.AllowUnsafeUpdates = true;
SPList list = Site.Lists.TryGetList("List_Library");
WebApp.AllowUnsafeUpdates = true;
SPListItem item = list.Items[list.Items.Count];
My question is how can I grab the current list? This code is located in the code behind for the infopath form and is in the FormEvents_Submit method that is called when the Submit event is fired. I tried moving the code to the bottom of the method to ensure that the form gets submitted first, but was still receiving the out of range error.
It turns out I was trying to access the list item BEFORE the InfoPath form was submitted, so it didn’t exist in the list yet. To get around this, I would have to put my code at the end of the Form_Submit event and make sure the list.Update() was finished before trying to grab the item.