我有一種情況,我必須根據記錄是否有打開或關閉的項目將源記錄列表狀態列更新爲打開或關閉狀態 - 項目列表可以通過源項目的ID和項目中的客戶端ID將多個項目鏈接到源記錄,代碼是檢索客戶項目,然後檢查是否有結束日期,如果沒有 - 它的開放 - 如果有的話 - 它的封閉。這些sharepoint列表很大Sharepoint 2010 - 更新大列表,處理非常緩慢和超時
我已經寫了代碼,基本上允許我指定一個開始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;
}
}
}'
的代碼被編碼後的文件從WSP拉 - 已經編輯到包括整個代碼,包括註釋掉的代碼 – Carl