0

I have a code in which I have to get the properties of selected item

string ID= QueryString["Id"]//this value is taken from url created dynamically and never be null
SPList list = web.Lists["MyList"];
SPListItem item= list.Items.GetItemById(Convert.ToInt32(ID));

But GetItemById() throws exception that the value doesn't fall in expected range. Please let me now why this happening as the ID is fetch from the item while making the url. Can content DB will help me out in this case?

UPDATE

The above code is working for some items.

  • 1
    I assume it is SharePoint 2013. Does the list contain more than 5000 (i.e. threshold) items? – Paiman Samadian Jan 8 '15 at 23:12
  • Yes. It contains around 70000 docs – nbi Jan 9 '15 at 2:25
  • 1
    Then list.GetItemById(Convert.ToInt32(ID)); should work for you as this doesn't try to enumerate all the items in the list like list.Items.GetItemById(Convert.ToInt32(ID)); does thereby not hitting the thresh hold – Eric Alexander Jan 9 '15 at 12:49
  • @PirateEric You hit the bull's eye. Yes that was the culprit. Now using List.GetItemByID() and its working smoothly – nbi Jan 9 '15 at 16:57
1

This article says to not use list.Items.GetItemById, but instead to use List.GetItemById(ID). The latter will not enumerate all items in the list. It might not be the source of your issue, but does have a performance impact.

Based on their article, you'd just do SPListItem item= list.GetItemById(Convert.ToInt32(ID));

  • that was typo :P – nbi Jan 8 '15 at 17:45
  • Instead of list.Items.GetItemById If I used ListItemCollection.GetItemById() will this make any difference – nbi Jan 8 '15 at 17:48
  • Does SPListItem item= list.GetItemById(Convert.ToInt32(ID)); result in the same error? That's what MSDN and the linked article are saying. – Eric Alexander Jan 8 '15 at 18:05
0

You can also use Int32.Parse instead of Convert.ToInt32(ID). I feel the value of ID that you are passing is causing the issue.

Also check the value of the variable ID, whether it is null or not. If it is null then it shall cause issue in getting converted to Int

One of the above should work for you. Give it a try and let me know.

  • thanks Mancy for the infomration, but as I said the ID can not be null and there is no problem in parsing through Convert.ToInt32() – nbi Jan 9 '15 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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