【问题标题】:add SPListItem to a SPListItemCollection将 SPListItem 添加到 SPListItemCollection
【发布时间】:2015-11-23 20:28:54
【问题描述】:

我有以下代码用于从共享点站点加载列表。

除了 SPList 项到 SPListItemCollection 之外,ALl 运行良好。

 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;


                }
            }

        }

现在在 if(A== "2"){ ITEM.Add(item); }

我想将 SPListItem 添加到 SPListItemCollection。请帮忙。

【问题讨论】:

    标签: c# asp.net sharepoint-2013


    【解决方案1】:

    您正在尝试将项目添加到 null,因为您的 ITEM 为空。我不知道你得到了什么错误(你不要写它)但你必须初始化你的集合:

        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 ITEM = new SPListItemCollection(); Microsoft.Sharepoint.SPListItemCollection() 类型没有定义构造函数
    • @ShahrozShaikh 我没有看到你的代码和Sharepoint,所以你需要初始化它然后使用上面的代码
    • 对于 SPListItemCollection ITEM = null 它在 ITEM.Add(item) 处的无效参数;在 SPListItemCollection ITEM = new SPListItemCollection(); Microsoft.Sharepoint.SPListItemCollection() 类型目前没有定义构造函数来面对这些问题
    • @ShahrozShaikh I Unserstand。我无法初始化集合,因为我没有看到您的 Sharepoint 和所有代码,您必须单独完成并使用我向您展示的上述代码
    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2010-10-04
    • 1970-01-01
    相关资源
    最近更新 更多