将字符串分配给 SPListItem 字段时出现无法解释的延迟
我通过与 SharePoint 安装相同的服务器上的控制台应用程序使用 SharePoint 对象模型,并使用以下代码:
SPSite MySite = new SPSite("http://server/");
SPWeb MyWeb = MySite.OpenWeb();
MyWeb.AllowUnsafeUpdates = true;
SPList MyList = MyWeb.Lists["Test"];
const string EmptyQuery = "0";
SPQuery q = new SPQuery { Query = EmptyQuery };
String Source = "Test String";
for( int i = 1; i < 1000; i++)
{
Console.WriteLine("Creating new item");
SPListItem MyItem = MyList.GetItems(q).Add();
Console.WriteLine("Created new item");
Console.WriteLine("Assigning Title Value");
MyItem["Title"] = Source.ToString();
Console.WriteLine("Assigned Title Value");
MyItem.Update();
}
我在“分配标题值”和“分配的标题值”之间出现几秒钟的暂停。
当我将代码部署为 Web 部件时,它是瞬时的,只有当代码部署为控制台应用程序时才会出现延迟。
编辑:更多信息!当我分配多个字段时,它总是第一个字段较慢,任何后续分配都与预期一样快。如果我改变字段的顺序,它对延迟没有影响 - 第一个字段总是很慢。
有什么想法吗?
I am using the SharePoint Object Model via a console app on the same server as the SharePoint installation, and using the following code:
SPSite MySite = new SPSite("http://server/");
SPWeb MyWeb = MySite.OpenWeb();
MyWeb.AllowUnsafeUpdates = true;
SPList MyList = MyWeb.Lists["Test"];
const string EmptyQuery = "0";
SPQuery q = new SPQuery { Query = EmptyQuery };
String Source = "Test String";
for( int i = 1; i < 1000; i++)
{
Console.WriteLine("Creating new item");
SPListItem MyItem = MyList.GetItems(q).Add();
Console.WriteLine("Created new item");
Console.WriteLine("Assigning Title Value");
MyItem["Title"] = Source.ToString();
Console.WriteLine("Assigned Title Value");
MyItem.Update();
}
I am getting a several second pause between "Assigning Title Value" and "Assigned Title Value".
When I deploy the code as a Web Part, its instantaneous, the delay only seems to be when the code is deployed as a console application.
Edit: More information! When I have more than one field being assigned, its always the first field that is slow, any subsequent assignments are as fast as expected. If I switch the order of fields around, it has no effect on the delay - the first field is always slow.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来这是因为您在使用设置器之前没有访问任何字段。如果您首先读取至少一个字段,您可能会看到那里有轻微的延迟,并且 setter 上没有延迟,因为在底层
SetValue()
调用EnsureFieldCollection()
,如果列表项的字段尚未填充,则必须检查SPList
的字段集合。另外,这不在您的代码片段中,但请确保在完成后处理您的
SPWeb
和SPSite
对象。一个好的模式是使用using
:It looks like this is because you're not accessing any fields before using the setter. If you read at least one field first, you'd probably see a slight delay there, and no delay on the setter, because under the hood
SetValue()
callsEnsureFieldCollection()
, which - if the fields for the list item haven't already been populated - has to check back to theSPList
's fields collection.Also, this is not in your code snippet, but make sure you are disposing of your
SPWeb
andSPSite
objects when you're done. A good pattern is to useusing
:首先,我建议使用
You can check EnsureFieldCollection by read the all fields of the item before update the field.如果之后您的第一个字段立即更新,您可以非常确定这就是原因。
First of all I would suggest making use of
You can check the EnsureFieldCollection by reading all the fields of the item before updating the field. If after that your first field is updated instantly you can be pretty sure that that is the reason.
这可能是发布和调试版本之间的差异,或者它附加了调试器。尝试更改为发布版本,如果这不起作用,请尝试在不使用调试器的情况下运行它(在 Visual Studio 中按 Ctrl+F5)
It may be a difference between Release and Debug builds, or the fact it has the debugger attatched. Try changing to a Release build, and if that doesn't work try running it without the debugger (Ctrl+F5 in Visual Studio)