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.

closed as off topic by Alex Angas Jun 14 '11 at 23:09

Questions on SharePoint Stack Exchange are expected to relate to SharePoint within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I think you will find that the item has not been created yet. What are you trying to do with the id? – djeeg Apr 16 '11 at 5:09
  • You're right, I am finding that the item has not been created yet. Do you know how to do that programmatically? I would like modify the permissions of the current form if a certain checkbox is checked. – Meyer Denney Apr 18 '11 at 16:42

Arrays/Collections in C# are zero based so item Items[list.Items.Count] does not exist the last item in this collection is Items[list.Items.Count-1].

Please note: I am not sure this solves your problem completely as I never tried to retrieve items from InfoPath via C#.

  • I tried this and it grabbed the second most recent item in the list. I am starting to think that perhaps the current item is not saved in the list library and I will have to find another way of accessing it. – Meyer Denney Apr 15 '11 at 20:35

While the user is in the infopath form and adding data, there is no current item yet, as it's not created until the user saves the form.
What properties of the current item you are trying to get? They should all be on the current form the user fills out.

  • Do you know how to save the form programmatically? I am trying to modify the permission levels of the current form if a checkbox gets checked. – Meyer Denney Apr 18 '11 at 16:43

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