Tridion 2009 SP1经纪人未返回结果 [英] Tridion 2009 SP1 Broker not returning results
问题描述
我在基于如下所示的相当简单的查询从Broker加载动态组件表示时遇到麻烦,在该示例中,我试图基于被特定关键字标记的组件来加载组件:
I am having trouble loading a Dynamic Component Presentation from the Broker based on a fairly simple query like the below, where I am trying to load the Component based on being tagged with a specific Keyword:
private string GetComponentPresentations()
{
Logger.Log.Info("Entered GetComponentPresentations");
var publicationCriteria = new PublicationCriteria(_publicationId);
int schemaId = int.Parse(SchemaId.Split('-')[1]);
// Is it the correct content type (Schema)
var isSpecifedSchema = new ItemSchemaCriteria(schemaId);
// Type of the item is 16 (Component).
var isComponent = new ItemTypeCriteria(16);
// All of the above conditions must be true
Criteria isCorrectComponent = CriteriaFactory.And(isSpecifedSchema, isComponent);
var publicationAndIsComponent = CriteriaFactory.And(publicationCriteria, isCorrectComponent);
//Only get components tagged with the specified keyword
var keywordCriteria = new KeywordCriteria(_productsCategoryTcmId, ProductFilter, Criteria.Equal);
//Only get Components of the correct type from the correct publication
Criteria fullCriteria = CriteriaFactory.And(publicationAndIsComponent, keywordCriteria);
using (var query = new Query(fullCriteria))
{
string[] results = query.ExecuteQuery();
using (var cpf = new ComponentPresentationFactory(_publicationId))
{
if(results != null)
{
var resultString = new StringBuilder();
foreach (string componentTcmId in results)
{
Logger.Log.Info("Looping over results");
int componentId = int.Parse(componentTcmId.Split('-')[1]);
int templateId = int.Parse(TemplateId.Split('-')[1]);
ComponentPresentation cp = cpf.GetComponentPresentation(componentId, templateId);
if (cp != null && !string.IsNullOrEmpty(cp.Content))
{
resultString.Append(cp.Content);
Logger.Log.InfoFormat("Appended Content {0}",cp.Content);
}
}
Logger.Log.Info("Returning");
return resultString.ToString();
}
Logger.Log.Info("Results was null.");
return string.Empty;
}
}
}
我可以使用希望的关键字在Broker数据库的ITEMS_CATEGORIES_AND_KEYWORDS表中看到该项目,并且如果我将查询注释掉并在其中以TCM ID进行硬编码,则可以手动加载CP.
I can see the item in the ITEMS_CATEGORIES_AND_KEYWORDS table in the Broker database with the keyword I expect and I can load the CP manually if I comment out the query and hardcode the TCM ID in.
我确保发布了类别,并且所有变量的值都是正确的.
I have made sure the Category is published and that all the variables' values are correct.
我确保关键字具有一个值,并且已将关键字设置为适当的值.
I have ensured the Keyword has a value and a key set to the appropriate value.
我还能检查什么?
推荐答案
我设法使用以下代码使它正常工作:
I have managed to get this working using the following code:
private string GetComponentPresentationsUsingFilter()
{
//RSL: Had to use the obsolete filtering API because could not get anything back from the Broker.
var filter = new SearchFilter("tcm:0-" + _publicationId + "-1");
var query = new Query();
string schemaId = SchemaId.Split('-')[1];
query.AddCriteria("schema", "=", schemaId);
query.AddCustomMetaQuery(string.Format("KEY_NAME = 'product' AND CAST(KEY_STRING_VALUE as nvarchar(100)) = '{0}'", ProductFilter));
string[] results = filter.Match(query, new Sorting("title=asc"), MaxItems);
if (results == null)
{
Logger.Log.Info("Results was null.");
return string.Empty;
}
using (var cpf = new ComponentPresentationFactory(_publicationId))
{
var resultString = new StringBuilder();
Logger.Log.InfoFormat("Got {0} Results", results.Length);
foreach (string componentTcmId in results)
{
int componentId = int.Parse(componentTcmId.Split('-')[1]);
Logger.Log.InfoFormat("Got componentId as {0}", componentId);
int templateId = int.Parse(TemplateId.Split('-')[1]);
Logger.Log.InfoFormat("Got templateId as {0}", templateId);
ComponentPresentation cp = cpf.GetComponentPresentation(componentId, templateId);
if (cp != null && !string.IsNullOrEmpty(cp.Content))
{
resultString.Append(cp.Content);
Logger.Log.InfoFormat("Appended Content {0}", cp.Content);
}
}
return resultString.ToString();
}
}
不知道为什么我可以通过这种方式获得结果,但是使用Criteria API却一无所获吗?
No idea why I can get results this way but nothing using the Criteria api?
这篇关于Tridion 2009 SP1经纪人未返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!