2

I have the following code to load a list from SharePoint site.

All is working well except a SPList item to SPListItemCollection.

 private void Data_load()
        {
            DataTable dt = new DataTable();


            string currentName = SPContext.Current.Web.CurrentUser.Name;
            SPQuery query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Editor'/><Value Type='Person or Group'>" + currentName + "</Value></Eq></Where>";

            using (SPSite site = new SPSite("http://spdev-6/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lists = web.GetList("Lists/Advertisements");

                    SPListItemCollection items = lists.GetItems(query);

                    if (items.Count > 0)
                    {
                        DataRow dr=null;
                        SPListItemCollection ITEM = null;
                        foreach(SPListItem item in items)
                        {
                            string A = item["Approval Status"].ToString();
                            if(A== "2")
                            {
                                ITEM.Add(item);

                            }


                        }
                        if(dt.Rows.Count==0)
                            lbldata.Text = "No data to show";

                      //  dt = items.GetDataTable();


                    }
                    else
                        lbldata.Text = "No data to show";



                    GridViewD.DataSource = dt;
                    GridViewD.DataBind();
                    HttpContext.Current.Session["Advertisement"] = dt;


                }
            }

        }

Now in if(A== "2"){ ITEM.Add(item); }

I want to add SPListItem to a SPListItemCollection. How can I achieve this?

  • ITEM.Add(item); isn't it working? – Atish Dipongkor - MVP Nov 27 '15 at 8:07
  • 2
    SPListItemCollection ITEM = null; what is this? You never assign this a value this and then you do an .Add on it. That will surely throw a NullReferenceException. Not sure what you are really trying to achive? – Robert Lindgren Nov 27 '15 at 8:13
  • Atish its not working – Shahroz Shaikh Nov 27 '15 at 9:15
  • Robert initially there is nothing to assign ITEM. – Shahroz Shaikh Nov 27 '15 at 9:16
1

First its a bit difficult to understand the code. However I am assuming below

  1. You are getting all the items in items object.
  2. Now based on the Approval Status you are creating a new SPListItemCollection object.
  3. Lastly you get all the filtered items in DataTable and bind to a gridview.

There can be 2 things which you can look at:

  1. Instead of filtering items after you get all of them, why not add a clause in your CAML query. This will bring a nice filtered data which needs not any processing and can be directly bind to Gridview.
  2. You are calling Add(item) method on object of SPListItemCollection. But according to the list of members of SPListItemCollection, there is overload of Add which contains single parameter.
|improve this answer|||||
0

Not sure if this is helpful to anyone else. I had a SPListItemCollection, but from that I needed a subset of the values (I did not modify the query as I needed all the values initially, then a small subset). I was looping through the collection as you did, but because you cannot add an SPListItem to a Collection, I simply created a List of type SPListItem and added each item to the list followed by continue. I could then iterate through that list as needed.

    List<SPListItem> subList = new List<SPListItem>();
    foreach (SPListItem item in ColItem) {
      string parentID = Convert.ToString(item["ParentID"]);
      if (parentID != "0" && parentID != "")
                            {
                                subList.Add(item);
                                continue;
                            }
                            REST OF MY CODE
|improve this answer|||||

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.