2

I'm trying to query a folder under the media library which has a bunch of templates based on a custom File template. I've tried to play around with a couple approaches but none of them have been successful.

I have tried:

1) Querying the entire media folder and grabbing all files based on the File template using the following line:

var files = SitecoreContext.Database.SelectItems($"fast:/sitecore/media library/*#[@@id = '{dcdFilesFolderId}']#//**#[@@tid = '{templateId}']#");

but this results in the error:

Sitecore.Data.Query.ParseException: End of string expected at position 77.

2) Query for a single item using the file ID:

var file = SitecoreContext.Database.SelectItem( $"fast:/sitecore/media library/*#[@@id = '{dcdFilesFolderId}']#//**[@File ID = '{fileid}']");

and this returns no hits.

What else can I try or make these queries work?

7

First: Don't use fast query, use your index (Sitecore.ContentSearch). "fast" query is actually very slow by comparison.

The query you tried is malformed, that's why you see this message:

Sitecore.Data.Query.ParseException: End of string expected at position 77.

It should look like this when the string is formatted:

/sitecore/media library//*[@@id='{43440D8E-E6AC-4F36-A5A5-53DB5CEFE369}']//*[@@templateid='{F44A8BAC-17C7-4A34-A84A-9B5F8CBBCEDC}']
  1. //* will find all descendants
  2. /* will find children (direct descendants)
  3. '#' is used to escape item/field names. It is not necessary when you're using the attribute tokens (e.g.: @@id, @@templateid
  4. It's @@templateid, not @@tid

The Sitecore Query Cheat Sheet is handy (and still relevant)

There's also a Xpath Builder tool that you can use to test out your queries. This has been inexplicably removed from the the sitecore interface/navigation/control panel but you can get to it directly from here: http://[SiteUrl]/sitecore/shell/default.aspx?xmlcontrol=IDE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.