SharePoint“线程正在中止”循环访问 SPListItemCollection

发布于 2024-08-09 22:33:09 字数 328 浏览 1 评论 0 原文

我有一个 SPListItemCollection,其中包含约 500 个项目。我使用 foreach 循环进行循环,每次抓取文件并将其写入 pdf 文档。我收到错误“线程正在中止”。看起来是超时问题,但我不知道如何解决?包含 200-300 件物品的集合效果很好。有什么办法可以让我停止收到此错误。如果我无法增加超时,我将需要对其进行批处理。

更新

我尝试将处理分成 100 个项目的批次。每新增 100 个项目,都会使用新的 spweb 和站点,并从共享点中提取项目。这一切都是在同一个方法中完成的(所以我没有重新创建我的自定义对象),但是相同的错误仍然发生......

I have a SPListItemCollection with ~500 items in. I am looping through using a for each loop, each time grabbing the file and writing it out to a pdf document. I am getting the error "Thread was being aborted". It looks to be a time out issue but I dont know how to fix it? Collections with 200-300 items work fine. Is there any way I can stop getting this error. If I cant increase the timeout I will need to batch it up.

Update

I have tried splitting up the processing in batches of 100 items. With each new 100 items using a new spweb and site and pulling the items from sharepoint. This is all done within the same method (so I am not recreating my custom object) however the same error is still occuring...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

吖咩 2024-08-16 22:33:09

http://blogs.msdn.com/solutions/archive/2009/01/08/getting-around-thread-was-being-aborted-error-when-creating-ep-site.aspx< /a>

  1. 在记事本等基本文本编辑器中,打开 web.config 文件,例如 ' %SYSTEMDRIVE%\Inetpub\wwwroot
    -或者-
    %SYSTEMDRIVE%\\Inetpub\wwwroot\wss\VirtualDirectories\80 文件夹

  2. 按 CTRL + F 打开“查找”对话框。

  3. 找到以下标签:

  4. 用这个标签替换它:

这似乎对我有用。

http://blogs.msdn.com/solutions/archive/2009/01/08/getting-around-thread-was-being-aborted-error-when-creating-ep-site.aspx

  1. In a basic text editor such as Notepad, open the web.config file for example ' %SYSTEMDRIVE%\Inetpub\wwwroot
    -or-
    %SYSTEMDRIVE%\\Inetpub\wwwroot\wss\VirtualDirectories\80 folder

  2. Press CTRL + F to open the Find dialog box.

  3. Find the following tag:

    <httpRuntime maxRequestLength="51200" />

  4. Replace it with this tag:

    <httpRuntime executionTimeout="6000" maxRequestLength="51200" />

This seems to have got it working for me.

娇柔作态 2024-08-16 22:33:09

请记住,迭代列表项的方法有正确和错误之分,这会对性能产生巨大影响。错误的方式会导致列表数据对数据库进行n+1次调用;正确的方法只对数据库进行一次调用。

最差:

for (int i = 0; i < myList.Items.Count; i++)
{
  SPListItem thisItem = myList.Items[i];

  // yada yada yada
}

不好:

foreach (SPListItem item in myList.Items)
{
  // yada yada yada
}

正确:

SPListItemCollection myItems = myList.Items;
foreach (SPListItem item in myItems)
{
  // yada yada yada
}

更新: 反映更准确的指导

Keep in mind there is a right and a very wrong way to iterate over list items, and it makes a huge difference in performance. The wrong way will cause n+1 calls to the database for the list data; the right way only makes a single call to the database.

Worst:

for (int i = 0; i < myList.Items.Count; i++)
{
  SPListItem thisItem = myList.Items[i];

  // yada yada yada
}

Bad:

foreach (SPListItem item in myList.Items)
{
  // yada yada yada
}

Right:

SPListItemCollection myItems = myList.Items;
foreach (SPListItem item in myItems)
{
  // yada yada yada
}

Updated: To reflect more accurate guidance

浸婚纱 2024-08-16 22:33:09

迭代 SPListItemCollection 从来都不是一个好主意(但遗憾的是有时这是唯一的方法)。您可以考虑将此代码包装在 LongRunningOperation 中。这是我根据自己的一些代码改编的一些代码:

实际类别:

using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.SharePoint;

namespace Common.LongRunningJobs
{
  /// <summary>
  /// Provides a long running job wrapper around converting multiple files
  /// </summary>
  [CLSCompliant(false)]
  public class FileToPdfLongRunningJob : LongRunningOperationJob
  {
    private List<string> fileUrls;

    /// <summary>
    /// Initializes a new instance of the <see cref="FileToPdfLongRunningJob"/> class.
    /// </summary>
    /// <param name="urls">The urls of the files to create pdfs from.</param>
    public FileToPdfLongRunningJob(List<string> urls)
    {
      fileUrls = urls;
    }

    /// <summary>
    /// Does the actual work of converting the files, while providing the user with status updates.
    /// </summary>
    public override void DoWork()
    {
      try
      {
        using (var currentWeb = Site.OpenWeb(parentWeb))
        {
          OperationsPerformed = 0;

          foreach (var url in fileUrls)
          {
            SPFile file = currentWeb.GetFile(url);

            // DO PDF OUTPUT

            StatusDescription = string.Format(CultureInfo.InvariantCulture, "busy converting {0} van {1}, file: {2}...", (OperationsPerformed + 1), TotalOperationsToBePerformed, file.Name);
            UpdateStatus();

            OperationsPerformed++;
          }
        }
      }
      catch (Exception ex)
      {
        // LOG ERROR
      }
    }
  }
}

使用以上类别:

private void ConvertFiles()
{
  const string PROGRESS_PAGE_URL = "/_layouts/LongRunningOperationProgress.aspx";
  var urls = new List<string>();
  foreach (SPListItem item in yourlistitemcollection)
  {
    urls.Add(item.File.Url);
  }
  var longRunningJob = new FileMoveLongRunningJob(urls)
  {
    Title = "Pdf conversion Long Running Job",
    TotalOperationsToBePerformed = urls.Count,
    RedirectWhenFinished = true,
    NavigateWhenDoneUrl = urlToRedirectTo;//i.e. SPContext.GetContext(Context).List.RootFolder.ServerRelativeUrl
  };

  longRunningJob.Start(SPContext.Current.Web);
  string url = string.Format("{0}{1}?JobId={2}", SPContext.Current.Web.Url, PROGRESS_PAGE_URL, longRunningJob.JobId);
  SPUtility.Redirect(url, SPRedirectFlags.Default, Context);
}

Iterating through an SPListItemCollection is never a good idea (but sadly sometimes the only way to go). You could consider wrapping up this code in a LongRunningOperation. Here's some code I adapted from some of my own:

ACTUAL CLASS:

using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.SharePoint;

namespace Common.LongRunningJobs
{
  /// <summary>
  /// Provides a long running job wrapper around converting multiple files
  /// </summary>
  [CLSCompliant(false)]
  public class FileToPdfLongRunningJob : LongRunningOperationJob
  {
    private List<string> fileUrls;

    /// <summary>
    /// Initializes a new instance of the <see cref="FileToPdfLongRunningJob"/> class.
    /// </summary>
    /// <param name="urls">The urls of the files to create pdfs from.</param>
    public FileToPdfLongRunningJob(List<string> urls)
    {
      fileUrls = urls;
    }

    /// <summary>
    /// Does the actual work of converting the files, while providing the user with status updates.
    /// </summary>
    public override void DoWork()
    {
      try
      {
        using (var currentWeb = Site.OpenWeb(parentWeb))
        {
          OperationsPerformed = 0;

          foreach (var url in fileUrls)
          {
            SPFile file = currentWeb.GetFile(url);

            // DO PDF OUTPUT

            StatusDescription = string.Format(CultureInfo.InvariantCulture, "busy converting {0} van {1}, file: {2}...", (OperationsPerformed + 1), TotalOperationsToBePerformed, file.Name);
            UpdateStatus();

            OperationsPerformed++;
          }
        }
      }
      catch (Exception ex)
      {
        // LOG ERROR
      }
    }
  }
}

USING ABOVE CLASS:

private void ConvertFiles()
{
  const string PROGRESS_PAGE_URL = "/_layouts/LongRunningOperationProgress.aspx";
  var urls = new List<string>();
  foreach (SPListItem item in yourlistitemcollection)
  {
    urls.Add(item.File.Url);
  }
  var longRunningJob = new FileMoveLongRunningJob(urls)
  {
    Title = "Pdf conversion Long Running Job",
    TotalOperationsToBePerformed = urls.Count,
    RedirectWhenFinished = true,
    NavigateWhenDoneUrl = urlToRedirectTo;//i.e. SPContext.GetContext(Context).List.RootFolder.ServerRelativeUrl
  };

  longRunningJob.Start(SPContext.Current.Web);
  string url = string.Format("{0}{1}?JobId={2}", SPContext.Current.Web.Url, PROGRESS_PAGE_URL, longRunningJob.JobId);
  SPUtility.Redirect(url, SPRedirectFlags.Default, Context);
}
小瓶盖 2024-08-16 22:33:09

在获取项目(并从新网站获取它们)之前尝试创建新的 SPSite 和 SPWeb 对象。

很多时候就可以解决问题。

Try creating new SPSite and SPWeb objects before getting items (and get them from new webs).

Often times that solves the problem.

赠意 2024-08-16 22:33:09

迭代列表的代码中可能存在潜在的陷阱。如果您可以将代码粘贴到此处,那就太好了。
另外,最好使用 Caml 查询列表项,并仅选择操作所需的字段,而不是全部。

There can be potential pit falls in the code that iterates list. It will be good if you can paste your code here.
Also, its better to use Caml to query for list items and select only those fields that are required for your operation rather than all.

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