2013-05-17 19 views
1

有什麼方法可以參數化模板構建器中'源'字段的數據源?模板構建器中的Sitecore多站點管理器和'源'字段

我們有一個多站點設置。作爲其中的一部分,如果我們能夠將Droptrees和Treelist點指向適當位置而不是普通父母,那麼這將節省大量時間和煩惱。

例如:

Content 
    --Site1 
    --Data 
    --Site2 
    --Data 

而不必在我希望在單獨的數據文件夾指向它的根內容文件夾指向我們的網站,所以我想要做的事,如:

DataSource=/sitecore/content/$sitename/Data 

我在這找不到任何文章。這是可能的嗎?

回答

0

由於Sitecore的7需要VS 2012和我們公司不打算升級很快我被迫尋找一個Sitecore的任何時候6解決方案。

this articlethis one上畫圖我想出了這個解決方案。

public class SCWTreeList : TreeList 
{ 
    protected override void OnLoad(EventArgs e) 
    { 
     if (!String.IsNullOrEmpty(Source)) 
      this.Source = SourceQuery.Resolve(SContext.ContentDatabase.Items[ItemID], Source); 

     base.OnLoad(e); 
    } 
} 

這將創建一個自定義TreeList控制,並將它通過對一個類來處理它的源字段。該類需要做的就是將源字段中的所有內容解析爲sitecore查詢路徑,然後將其重新分配給源字段。這將繼續由Sitecore自己的查詢引擎處理。

所以對於我們的多站點解決方案啓用它的路徑像這樣:

{A588F1CE-3BB7-46FA-AFF1-3918E8925E09}/$sitename 

要解決到路徑,如這樣的:那麼

/sitecore/medialibrary/Product Images/Site2 

我們的控件將只顯示正確的項目現場。

這是處理解決的GUID和令牌的方法:

public static string Resolve(Item item, string query) 
{ 
    // Resolve tokens 
    if (query.Contains("$")) 
    { 
     MatchCollection matches = Regex.Matches(query, "\\$[a-z]+"); 
     foreach (Match match in matches) 
      query = query.Replace(match.Value, ResolveToken(item, match.Value)); 
    } 

    // Resolve GUIDs. 
    MatchCollection guidMatches = Regex.Matches(query, "^{[a-zA-Z0-9-]+}"); 
    foreach (Match match in guidMatches) 
    { 
     Guid guid = Guid.Parse(match.Value); 
     Item queryItem = SContext.ContentDatabase.GetItem(new ID(guid)); 

     if (item != null) 
      query = query.Replace(match.Value, queryItem.Paths.FullPath); 
    } 

    return query; 
} 

令牌下面的處理,你可以看到它需要使用該令牌$siteref任何產品,我們創建了一個Site Folder項目內。這允許我們使用包含我們所有多站點內容文件夾必須遵循的名稱的字段 - Site Reference。只要符合命名約定,它就允許我們引用媒體庫中的文件夾或Sitecore中的任何其他共享內容。

static string ResolveToken(Item root, string token) 
{ 
    switch (token) 
    { 
     case "$siteref": 
      string sRef = string.Empty; 

      Item siteFolder = root.Axes.GetAncestors().First(x => x.TemplateID.Guid == TemplateKeys.CMS.SiteFolder); 
      if (siteFolder != null) 
       sRef = siteFolder.Fields["Site Reference"].Value; 

      return sRef; 
    } 

    throw new Exception("Token '" + token + "' is not recognised. Please disable wishful thinking and try again."); 
} 

到目前爲止,這適用於TreeLists,DropTrees和DropLists。這將是很好,讓它與DropLinks工作,但這種方法似乎並沒有工作。

這感覺就像抓表面,我敢肯定還有很多你可以用這種方法做。

2

缺省情況下,但你可以使用這種技術來編寫你的數據源: http://newguid.net/sitecore/2013/coded-field-datasources-in-sitecore/

+0

偉大的文章,謝謝。看起來像Sitecore 7可能需要等待一週時間,這是新功能。 – Jon

+0

如果您可以等待Sitecore 7,那將是非常值得的! –

+0

Sitecore 7將於下週發佈!?我認爲這會花一點時間。 – Younes

1

你可能想看看使用Querable Datasource Location和堵到getRenderingDatasource管道。

這真的要取決於你的用例。我喜歡這個解決方案的東西是沒有必要創建一大堆控件來實現與默認的Sitecore相同的效果,而且您​​不必單獨編碼您需要的每個數據源 - 只需設置查詢你需要獲取數據。您也可以在模板的__standard values中設置數據源查詢。

這是非常相似的霍爾格的建議,我只是覺得這個代碼是整潔:)