C# 将SPListItem添加到SPListItem集合

C# 将SPListItem添加到SPListItem集合,c#,asp.net,sharepoint-2013,C#,Asp.net,Sharepoint 2013,我有以下代码用于从sharepoint网站加载列表 将SPList项排除到SPListItemCollection之外,一切正常 private void Data_load() { DataTable dt = new DataTable(); string currentName = SPContext.Current.Web.CurrentUser.Name; SPQuery query = ne

我有以下代码用于从sharepoint网站加载列表

将SPList项排除到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;


                }
            }

        }
private void Data_load()
{
DataTable dt=新的DataTable();
字符串currentName=SPContext.Current.Web.CurrentUser.Name;
SPQuery query=新建SPQuery();
query.query=“”+当前名称+”;
使用(SPSite站点=新SPSite(“http://spdev-6/"))
{
使用(SPWeb=site.OpenWeb())
{
SPList list=web.GetList(“列表/广告”);
SPListItemCollection items=lists.GetItems(查询);
如果(items.Count>0)
{
数据行dr=null;
SPListItemCollection项=null;
foreach(项目中的SPListItem项目)
{
字符串A=项目[“批准状态”]。ToString();
如果(A=“2”)
{
项目。添加(项目);
}
}
if(dt.Rows.Count==0)
lbldata.Text=“无需显示的数据”;
//dt=items.GetDataTable();
}
其他的
lbldata.Text=“无需显示的数据”;
GridViewD.DataSource=dt;
GridViewD.DataBind();
HttpContext.Current.Session[“广告”]=dt;
}
}
}
现在在if(A==“2”){ITEM.Add(ITEM);}


我想将SPListItem添加到SPListItem集合中。请帮助。

您正试图将项目添加到
null
中,因为您的
项目
为null。我不知道您遇到了什么错误(您没有写入错误),但您必须初始化集合:

    if (items.Count > 0)
    {
          DataRow dr=null;
          SPListItemCollection ITEM = ... //
          foreach(SPListItem item in items)
          {
              string A = item["Approval Status"].ToString();
              if(A== "2")
              {
                 SPListItem myItem = ITEM.Add();
                 // set your item's fields here 
                 // Use indexers on this object for each field to assign specific values, and then call the Update method on the item to effect changes in the database.
                 myItem["Approval Status"] = item["Approval Status"];
                 ...
                 myItem.Update();
              }
          }
          if(dt.Rows.Count==0)
             lbldata.Text = "No data to show";

             //  dt = items.GetDataTable();

    }

错误:具有一些无效参数。在SPListItemCollection项=新建SPListItemCollection();类型Microsoft.Sharepoint.SPListItemCollection()没有构造函数defined@ShahrozShaikh我看不到您的代码和Sharepoint,因此您需要对其进行初始化,然后使用上面的代码for SPListItemCollection ITEM=null在ITEM.Add(ITEM)处的无效参数;在SPListItemCollection项处=新建SPListItemCollection();类型Microsoft.Sharepoint.SPListItemCollection()没有定义构造函数来同时解决这些问题moment@ShahrozShaikh我不支持。我无法初始化集合,因为我看不到您的Sharepoint和所有代码,您必须单独执行,并使用我向您显示的上述代码