Sharepoint 2010 - 更新大型列表,处理速度非常慢,超时

时间:2014-01-22 12:06:14

标签: c# sharepoint sharepoint-2010

我有一种情况,我必须将源记录列表状态列更新为打开或关闭,具体取决于记录是否具有打开或关闭项目 - 项目列表可以包含许多链接到源记录的项目根据项目中源记录和客户ID的ID,代码是检索客户项目,然后检查是否有结束日期,如果没有 - 它打开 - 如果有 - 它关闭。这些共享点列表很大

我编写的代码基本上允许我指定一个起始ID,然后它会在我点击一个运行按钮停止之前处理这么多记录 - 我把它放在适当的位置,因为这个过程非常缓慢甚至超时,但我不明白为什么它如此缓慢而且不稳定 - 如果我尝试处理任何超过150条记录 - 它超时,服务器本身是一个带有24GB内存的八核系统所以我不认为它是服务器,这可能是我的代码。

    protected void Button1_Click(object sender, EventArgs e)
    {
        SPWeb web = SPControl.GetContextWeb(this.Context);
        string SPsiteUrl = SPContext.Current.Web.Url;
        Label1.Text = "Running";
        int start;
        start = Convert.ToInt32(TextBox1.Text);
        start = int.Parse(TextBox1.Text);
        int end = start + 150;
        int count = 0;

        using (SPSite oSite = new SPSite(SPsiteUrl))
        {
            using (SPWeb oWeb = oSite.OpenWeb("/Client"))
            {
                // get lists
                SPList oList = oWeb.Lists["Source Records"];
                SPList pList = oWeb.Lists["Project"];

                // query Source Records 
                string sQuery = @"<Where><And><Geq><FieldRef Name='ID' /><Value Type='Number'>" + start + "</Value></Geq><Leq><FieldRef Name='ID'/><Value Type='Number'> "+ end +"</Value></Leq></And></Where>";
                string sViewFields = @"<FieldRef Name='ID' />";
                string sViewAttrs = @"Scope=""Recursive""";
                uint iRowLimit = 0;

                var oQuery = new SPQuery();
                oQuery.Query = sQuery;
                oQuery.ViewFields = sViewFields;
                oQuery.ViewAttributes = sViewAttrs;
                oQuery.RowLimit = iRowLimit;

                SPListItemCollection collListItems = oList.GetItems(oQuery);

                // for each item

                foreach (SPListItem oListItem in collListItems)
                {
                    // get the client id 

                    int id = oListItem.ID;

                        count = count + 1; 
                        //Label1.Text = "Checking record" + id;
                        bool isopen = false;
                        // for each client id access the projects list
                        // query the closed date
                        string Query = @"<Where><Eq><FieldRef Name=""Client_x003a_ID"" /><Value Type=""Text"">" + id + "</Value></Eq></Where>";
                        string pViewFields = @"<FieldRef Name=""End_x0020_Date"" />";
                        string pViewAttrs = @"Scope=""Recursive""";
                        uint pRowLimit = 0;
                        var pQuery = new SPQuery();
                        pQuery.Query = Query;
                        pQuery.ViewFields = pViewFields;
                        pQuery.ViewAttributes = pViewAttrs;
                        pQuery.RowLimit = pRowLimit;
                        SPListItemCollection ListItems = pList.GetItems(pQuery);

                        foreach (SPListItem ListItem in ListItems)
                        {
                            try
                            {
                                DateTime enddate = (DateTime)ListItem["End_x0020_Date"];
                                if (enddate != null)
                                {
                                    isopen = false;
                                }
                                // else if the date is null set the open to true
                                else
                                {
                                    isopen = true;
                                }
                            }
                            catch
                            {
                                string enddate = null;
                                if (enddate == null)
                                {
                                    // if the end date is present set project closed to true
                                    isopen = true;
                                }
                            }

                        // if project is open
                        if (isopen)
                        {
                            oWeb.AllowUnsafeUpdates = true;
                            SPListItem itemupdate = oWeb.Lists["Source Records"].Items.GetItemById(id);
                            itemupdate["Status"] = "Open";
                            itemupdate.Update();
                            oWeb.AllowUnsafeUpdates = false;
                            // set records on source record to open
                        }
                        //else // project is closed
                        //{
                        //    oWeb.AllowUnsafeUpdates = true;
                        //    SPListItem itemupdate = oWeb.Lists["Source Records"].Items.GetItemById(id);
                        //    itemupdate["Status"] = "Closed";
                        //    itemupdate.Update();
                        //    oWeb.AllowUnsafeUpdates = false;
                        //    // set the record to closed 
                        //}
                    }
                }
                Label1.Text = "Finished Number of records checked: " + count + " of " + collListItems.Count;
            }
        }
    }'

2 个答案:

答案 0 :(得分:1)

而不是

SPListItem itemupdate = oWeb.Lists["Source Records"].Items.GetItemById(id);

试试这个:

SPListItem itemupdate = oWeb.Lists["Source Records"].GetItemById(id);

答案 1 :(得分:-2)

您需要更新嵌套循环的代码。而不是:

foreach (SPListItem item1 in sPList.GetItems(sPQuery1))

尝试使用它:

SPListItemCollection items1 = sPList.GetItems(sPQuery1);
foreach (SPListItem item1 in items1)

有关详细信息,请参阅此blog post

更新:你必须尝试摆脱嵌套循环。我认为您可以通过在源列表中添加引用到项目列表来实现这一点,并参考那里的End Date列。