将字符串分配给 SPListItem 字段时出现无法解释的延迟

发布于 2024-08-05 22:19:16 字数 894 浏览 2 评论 0原文

我通过与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女猫性小仙女 2024-08-12 22:19:16

看起来这是因为您在使用设置器之前没有访问任何字段。如果您首先读取至少一个字段,您可能会看到那里有轻微的延迟,并且 setter 上没有延迟,因为在底层 SetValue() 调用 EnsureFieldCollection(),如果列表项的字段尚未填充,则必须检查 SPList 的字段集合。

另外,这不在您的代码片段中,但请确保在完成后处理您的 SPWebSPSite 对象。一个好的模式是使用 using

using(SPSite site = new SPSite("url"))
{
    using(SPWeb web = site.OpenWeb())
    {
        //do stuff
    }
}

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() calls EnsureFieldCollection(), which - if the fields for the list item haven't already been populated - has to check back to the SPList's fields collection.

Also, this is not in your code snippet, but make sure you are disposing of your SPWeb and SPSite objects when you're done. A good pattern is to use using:

using(SPSite site = new SPSite("url"))
{
    using(SPWeb web = site.OpenWeb())
    {
        //do stuff
    }
}
栖迟 2024-08-12 22:19:16

首先,我建议使用

using (SPSite MySite = new SPSite("http://server/"))
{
   using (SPWeb MyWeb = MySite.OpenWeb())
   {
      //do your stuff here
   }
}

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

using (SPSite MySite = new SPSite("http://server/"))
{
   using (SPWeb MyWeb = MySite.OpenWeb())
   {
      //do your stuff here
   }
}

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.

一场春暖 2024-08-12 22:19:16

这可能是发布和调试版本之间的差异,或者它附加了调试器。尝试更改为发布版本,如果这不起作用,请尝试在不使用调试器的情况下运行它(在 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文