I have a list of list item ids and I was trying to get SPListItemCollection ?

       var siteId = SPContext.Current.Site.ID;
        var webId = SPContext.Current.Web.ID;
        var listId = Request.QueryString["listId"];
        var itemIds = Request.QueryString["IDs"].Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        var listItems = new List<SPListItem>();

        using (var site = new SPSite(siteId))
            using (var web = site.OpenWeb(webId))
                var spList = web.Lists[new Guid(listId)];
                foreach (var itemId in itemIds)
                    SPListItem spListItem = spList.GetItemById(Convert.ToInt32(itemId));

How can I get SPListItemCollection from listItems list or is there any other better to approach to get the same?


You should use a CAML query using the IN operator

For an example see this blog entry

  • Is there a way to achieve the same using LINQ to SharePoint? Since the listItem IDs are retrieved on the fly, it would be a little difficult to build the SPQuery I would think? – David LA Oct 5 '13 at 1:17
  • An LINQ query with itemIds.Contains(list.Id) should work by generally I avoid LINQ to SharePoint as you don't know what it'll do unless you use the Log property. It should not be hard to generate the right query using Select, string.Format and string.Join – Per Jakobsen Oct 5 '13 at 4:00

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.