我无法根据下面的相当简单的查询从Broker加载动态组件演示文稿,我试图根据标记的特定关键字加载Component:
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。
我确保发布了类别并且所有变量的值都是正确的。
我已确保关键字的值和键设置为适当的值。
我还能检查什么?
答案 0 :(得分:4)
我建议逐个删除查询中的每个条件,并检查每个条件返回的结果。
要检查的另一件事是您使用的是您认为自己的API。 Tridion有两个非常相似的用于Broker查询的API。仔细检查您是否链接到正确的装配体。
答案 1 :(得分:2)
您是否尝试过对查询使用SetCriteria方法?例如:
query.SetCriteria(multipleCombinedFacetCriteria);
String[] itemURIS = query.ExecuteQuery();
答案 2 :(得分:2)
查看Java API时,我可以看到这个重载:
KeywordCriteria(java.lang.String categoryName, java.lang.String keyword, FieldOperator operator)
_productsCategoryTcmId可能只需要是Category的名称而不是URI吗?
答案 3 :(得分:2)
我已设法使用以下代码实现此工作:
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?
答案 4 :(得分:2)
您是否检查过您要查询的类别是否已发布?如果您使用较新的“标准”机制,则需要执行此操作。它总是让我那个!
谢谢,Jonathan