是否有内置方法可以判断特定 URL 属于此 SPWeb 还是其他 SPWeb?

发布于 2024-08-25 13:55:37 字数 777 浏览 11 评论 0原文

我有一个 url 列表(内容类型使用 url)。现在,内容类型已跨网络部署。我需要循环这些内容类型并在每个列表中执行一些操作,但每次循环打开新的 SPWeb 实例都需要大量资源。

是否有内置方法告诉我这个 URL 是否属于某个 SPWeb 对象?

示例:

SPWeb 可能是

内容类型使用链接,例如:

  • /web2/Pages
  • /web2 /Lists/Tasks
  • /web2/Lists/Documents
  • /web2/subweb1/Lists/Tasks
  • ...

如您所见,对于前 3 个用法,我不需要打开新的 SPWeb

I have a list of urls (content type usage url's). Now, content types are deployed across web's. I need to loop those content types and do some actions in each list, but opening new SPWeb instance every loop is too resource intensive.

Is there built-in method to tell me if this URL belongs to certain SPWeb object?

Example:

SPWeb's may be

With content type usage links like:

  • /web2/Pages
  • /web2/Lists/Tasks
  • /web2/Lists/Documents
  • /web2/subweb1/Lists/Tasks
  • ...

As you can see, for first 3 usages i don't need to open up new SPWeb

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

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

发布评论

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

评论(3

掀纱窥君容 2024-09-01 13:55:37

只是猜测,但也许值得尝试 SPWeb.GetList(serverRelativeUrl) 在您已有的 SPWeb 实例上,如果没有匹配,则创建一个新的 SPWeb?

更新:另一个想法。假设所有 SPWeb 都位于同一个 SPSite 中,您不妨使用这个特定的SPSite.OpenWeb() 的重载(并将 requestExactUrl 标志设置为 false)。这将返回您需要的 SPWeb 对象。 在内部,打开的 SPWeb 对象会被重用(您可以在 Reflector 中的 SPWeb.SPWebConstructor 方法中看到),因此这不会对性能造成太大影响。 错误,SPweb对象只是存储在列表中,而不是重用。

Just a guess, but maybe it's worth trying SPWeb.GetList(serverRelativeUrl) on the SPWeb instanes you already have and, if none matches, then create a new SPWeb?

Update: another idea. Assuming that all the SPWebs live in the same SPSite, you might as well use this particular overload of SPSite.OpenWeb() (and set the requestExactUrl flag to false). This will return you the SPWeb object you need. Internally, the opened SPWeb objects are reused (which you can see in SPWeb.SPWebConstructor method in Reflector), so this won't hit performance too much. wrong, the SPweb objects are just stored in a list, not reused.

一个人的夜不怕黑 2024-09-01 13:55:37

您的代码将在什么上下文中运行?

我的意思是,您似乎想要编写一些将作为部署过程的一部分运行的代码,这意味着它只会偶尔运行。

如果是这种情况,您的代码是否针对性能进行了优化,这重要吗?在我看来,最好坚持使用开放每个 SPWeb 的简单解决方案,而不必过多担心性能。

如果您需要循环访问数百或数千个网站,则需要一些时间。无论您的代码需要十分钟还是十五分钟来完成此操作,都不重要。

如果要运行多次且经常运行,性能将成为一个更大的问题。

What context will your code be running in?

What I mean is, it looks like you want to write some code that will be run as part of a deployment process, meaning that its only going to be run occasionally.

If that is the case, does it matter whether or not your code is optimized for performance? In my opinion, it might be better to stick with a simple solution of opening up each SPWeb and not worry too much about the performance.

If you need to loop over hundreds or thousands of sites, its going to take some time. Whether your code takes ten minutes to do this, or fifteen, shouldn't really matter.

Performance would be more of an issue if this was going to be run many times, and often.

猫性小仙女 2024-09-01 13:55:37

据我所知,没有内置的方法可以处理这种事情。
我建议您为此创建一个 SPWeb 扩展方法。

下面是一个帮助您入门的代码片段(未经过 100% 测试):

public static class SPWebExtensions
{
  public static bool UrlBelongsToweb(this SPWeb web, string url)
  {
    Uri uriWeb = new Uri(web.Url);

    if (uriWeb.LocalPath.ToLower() == url.ToLower())
      return true;

    if (!url.ToLower().StartsWith(uriWeb.LocalPath.ToLower()))
      return false;

    foreach (SPWeb subWeb in web.Webs)
    {
      Uri uriSub = new Uri(subWeb.Url);
      if (url.ToLower().StartsWith(uriSub.LocalPath.ToLower()))
        return false;

      subWeb.Dispose();
    }
    return true;
  }
}

您可以在类似这样的情况下使用它:

 bool urlBelongsToWeb = web.UrlBelongsToweb("/web2/Lists/Documents");

其中 web 是您当前的 SPWeb 对象。

或者,如果您想要定位 SiteCollection 中的特定列表,请使用 SPSiteDataQuery 来获取它们并对它们施展魔法。

As far as I know, there is no built-in method for this kind of thing.
I would suggest that you create an SPWeb extension method for this.

Here's a code snippet to get you started (not tested 100%):

public static class SPWebExtensions
{
  public static bool UrlBelongsToweb(this SPWeb web, string url)
  {
    Uri uriWeb = new Uri(web.Url);

    if (uriWeb.LocalPath.ToLower() == url.ToLower())
      return true;

    if (!url.ToLower().StartsWith(uriWeb.LocalPath.ToLower()))
      return false;

    foreach (SPWeb subWeb in web.Webs)
    {
      Uri uriSub = new Uri(subWeb.Url);
      if (url.ToLower().StartsWith(uriSub.LocalPath.ToLower()))
        return false;

      subWeb.Dispose();
    }
    return true;
  }
}

You would use it in something like this:

 bool urlBelongsToWeb = web.UrlBelongsToweb("/web2/Lists/Documents");

Where web is your current SPWeb object.

Alternatively, if you want to target specific Lists in your SiteCollection, use the SPSiteDataQuery to get hold of them and do your magic work against them.

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