【发布时间】:2014-01-22 12:06:14
【问题描述】:
我有一种情况,我必须根据记录是否有打开或关闭的项目,将源记录列表状态列更新为打开或关闭 - 项目列表可以有多个项目链接到源记录通过项目中源记录的 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;
}
}
}'
【问题讨论】:
-
代码是从 wsp 中提取的编码文件 - 已编辑以包含整个代码,包括注释掉的代码
标签: c# sharepoint sharepoint-2010