0

我正在向 sitecore 添加一个管道。我要做的是获取所有媒体项目,并更改 url,因为我们会将所有媒体资产放入 CDN。而且,问题很简单,如何获取所有媒体项?有谁知道?

4

2 回答 2

2

在您的自定义程序集中执行以下操作:

Sitecore.Data.Items.Item home = db.GetItem("/sitecore/media library");
using (new Sitecore.SecurityModel.SecurityDisabler())
    {
        alterItem(home);
    }

protected void alterItem(Sitecore.Data.Items.Item root)
{       

    foreach (Sitecore.Data.Items.Item itm in root.Children)
    {        

      //follow "absolute url" example below and edit each media item as you enumerate

      //Use recursion to loop through the entire tree under the root item
      alterItem(itm);
    }
}
于 2012-04-12T21:44:56.270 回答
0

You cannot walk the Sitecore media library tree and change URLs of items. The main reason is that a MediaItem (or any Item for that matter) doesn't have a property for URL. The URL that points to an item is determined by many configuration settings (the site the item is in, link prefixes, current language, 'file' extensions, etc...).

Being that there are many factors that control the actual URL of an item there are a few methods that you leverage to get the URL of an item at render time. The methods you should be using to get a URL to an item would be:

  • Sitecore.Links.LinkManager.GetItemUrl()
  • Sitecore.Resources.Media.MediaManager.GetMediaUrl()

You can do something that foxtrotZulu is suggesting. Essentially, add your own field to each MediaItem that stores the URL to the item on the CDN. You would need to either manually, or programtically, set the value of your custom URL field for each media item. Then, at render time you would use that field value rather than a call to the MediaManager.

You may also want to look at leveraging various configuration settings to control the URL returned via a call to Sitecore.Resources.Media.MediaManager.GetMediaUrl()

于 2012-04-13T13:39:48.960 回答