4

I am building a site with Sitecore 7.5 and Glass Mapper 3.2.3. When I add a sublayout to the presentation details of a page, Glass Mapper will assume that the data source for the sublayout should be the current page. I can also explicitly set the data source for the rendering by selecting a different page on the site in the presentation details dialog box.

enter image description here

Glass Mapper still works fine and uses the other selected page as the data source for the rendering. However if I attempt to use the Build Query dialog box and build a query for my data source, Glass Mapper seems to ignore that and just use the current page as the data source. Is there some way to use a query that I have built using the Build Query dialog?

enter image description here

EDIT: Here is some more information about my setup. I have a single data template in Sitecore called ContentPage. It has one field: Content. I have multiple pages in the site that use this data template. I have a sublayout called sublayoutPageContent. It inherits from GlassUserControl. All it does is display the Content field. If I add this sublayout to the presentation details for Page A and don't specify a data source it works fine. When I bring up Page A, it displays the Content field from Page A perfectly. I can also go in to the presentation details for the sublayout and tell it to use a different page as the data source - say Page B. If I do that and then I go view Page A, I will see the Content field from Page B. Great - that is working as expected. Now instead of selecting a page for the data source, I use a Query and in that Query I select Page B. Now when I view Page A, I see the Content field for Page A - NOT Page B. Hope that explains it better.

Corey Burnett
  • 7,312
  • 10
  • 56
  • 93
  • Can you please post the part of the code as well? – Aman B Jan 28 '15 at 15:45
  • There's not really much code to post, Glass handles a lot of this for you. But I can add some basic code I suppose. – Corey Burnett Jan 28 '15 at 15:56
  • One thing to try is using IEnumerable as your model for glass – Aman B Jan 28 '15 at 16:18
  • I'm not sure I understand what you mean. The models for my data templates are auto-generated by TDS using code generation templates from Glass. The sublayout is a simple sublayout that displays a single text field. – Corey Burnett Jan 28 '15 at 16:49

2 Answers2

5

If you're supporting the Page Editor and these sources are going to vary regularly, you might want to use a Datsource Template and Datasource Location instead of using the Datasource. Then, when you add the item via the Page Editor you'll be prompted to create or choose a datasource item. This makes it really easy, especially in the scenario you're describing. You can use static or relative paths for the datasource location. I often use a similar construct, although with more than 1 field, and it works very well. You can use this even if you are not using the Page Editor, but best practices are to support the Page Editor and DMS and this allows you the flexibility of both.

If you need multiple items, this will not work, but based on your description it seems you just need to be able to choose between them. Also, in scenarios like this you can group a number of items under a folder or other item and use the [SitecoreChildren] attribute and just get the items that way. It really all depends on how flexible you need to be and what your requirements are.

Jon Upchurch
  • 450
  • 4
  • 12
  • Thanks. I have no idea what a Datasource Template and a Datasource Location is. Is this something custom you built or a built-in feature of Sitecore? – Corey Burnett Jan 28 '15 at 18:13
  • 1
    It's built into sitecore. It's in the definition for your sublayout/rendering in the section just below the one you were using. Here's a good article that goes over how they work: https://markstiles.net/Blog/2013/09/20/Designing-a-Page-Editor-Experience-2-Sublayout-fields.aspx – Jon Upchurch Jan 28 '15 at 18:15
  • Also, I will have some sublayouts that will need to have multiple items as the data source. I am really struggling as to how best to use Glass Mapper and also support Page Editor. I have not found the tutorials on the Glass Mapper website to be very helpful. They are assuming that you are building your site in a certain way. I am trying to build my site so that content authors can add renderings to a page and set data sources on those renderings. As much as possible I don't want to hard code where data is coming from. – Corey Burnett Jan 28 '15 at 18:15
  • Ok, if you are going to want to use multiple items for a rendering, I guess the question would be how are they related? Here are some links to the powerpoints from some talks I've given video I did that explains how you can use the datasource template and such with your information architecture. – Jon Upchurch Jan 28 '15 at 18:21
  • (Response was too long so had to double comment) https://dl.dropboxusercontent.com/u/27360037/JonUpchurchRoundTable.pptx https://dl.dropboxusercontent.com/u/27360037/Latest%20Changeset.pptx https://dl.dropboxusercontent.com/u/27360037/ParagonIA.mp4 You definitely don't want to hardcode anything as you defeat the purpose of the Page Editor and the DMS. You're on the right track. If you need any clarification on any of what I linked I'm glad to elaborate. – Jon Upchurch Jan 28 '15 at 18:21
  • Thanks I will look at those links right now. A good example is a Carousel sublayout. I want the datasource for the Carousel to be a list of Slide content items. And when the user adds the Carousel to a page I want to give them the ability to select multiple slides to serve as the datasource. This is where I am struggling to understand how to do this. – Corey Burnett Jan 28 '15 at 18:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69764/discussion-between-corey-burnett-and-jon-upchurch). – Corey Burnett Jan 28 '15 at 18:47
0

Because a query can return multiple items you should change your code to expect multiple items instead of single item. Not sure how it works in webforms but in Razor you do something like this:

Change

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<MyEntity>

To

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<IEnumerable<MyEntity>>
Liam
  • 27,717
  • 28
  • 128
  • 190
Aman B
  • 2,276
  • 1
  • 18
  • 26