Transformations for a custom web part

Tracey Penberthy asked on April 20, 2017 13:29

Hi

I have a content only page type for which I am trying to create a view for. The link listings for these pages will appear in multiple areas of the website. I want the user to be taken to the same page when they click to view.

I have created a custom web part which gets the document id from the querystring, gets the document data and then binds this to a FormView control and sets the item template to the transformation that the editor has selected.

I am having real problems getting the transformation to show the custom data on the page (it can show things like DocumentName, DocumentID fine).

Here's my code:

In the custom web part (where "pageView" is the FormView control)

 protected void SetupControl()
    {
        if (this.StopProcessing)
        {
            // Do not process
        }
        else
        {
            var tree = new TreeProvider();
            // get doc id
            var docid = ValidationHelper.GetInteger(Request.QueryString["DocumentID"], 0);
            // get the document
            var p = tree.SelectSingleDocument(docid, true);
            // get the transformation
            var transformation = ValidationHelper.GetString(GetValue("Transformation"), string.Empty);
            if(null != p)
            {
                // p in a list (required format for the formviews datasource)
                var ds = new List<TreeNode>() { p };
                // apply the datasource
                pageView.DataSource = ds;
                // apply the template
                pageView.ItemTemplate = TransformationHelper.LoadTransformation(this, transformation);
                // data bind
                pageView.DataBind();
            }
        }
    }

The Transformation

<p>1. <%# Eval("DocumentName") %></p>
<p>2. <%# Eval("Title") %></p>
<p>3. <%# DocumentCustomData["title"] %>
<p>4. <%# DocumentCustomData["Title"] %>
<p>5. <%# NodeCustomData["title"] %>
<p>6. <%# NodeCustomData["Title"] %>
<p>7. <%#CurrentDocument.GetValue("Title") %></p>

Number 1 displays correctly but I cannot get the Title field using any of the other methods (in the custom web part itself I can get Title fine using p.GetValue("Title"))

Any help with this would be greatly appreciated

Thanks Tracey

Recent Answers


Trevor Fayas answered on April 20, 2017 16:33

That catches me sometimes! When you call the "SelectSingleDocument" it only returns the base data. You'll need to use CMS.DocumentEngine.TreeHelper.GetDocument() and pass the class name, then it will give you the class data!

0 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on April 20, 2017 17:10

Thanks Trevor

I am using version 10 and it is saying TreeHelper is obselete.

But I have managed to get the page this way passing in the class name

tree.SelectSingleNode(CurrentSiteName, p.NodeAliasPath, CurrentDocument.DocumentCulture, false, "site.NewsPage", null, null, -1, true);

Unfortunately the results are the same (i.e. the Title is not getting shown in the transformation)

Is this because it is still using the SelectSingleNode method?

Is there another method I can call to get the document details that will return the class data?

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 20, 2017 17:16

Could be because you're using the Select Node, because Class-level content is tied to the Document, not the Tree Node.

Try this one:

var TheDocumentInfo = CMS.DocumentEngine.DocumentHelper.GetDocuments("my.ClassName").WhereEquals("DocumentID", 123).FirstObject;
TheDocumentInfo .GetValue("Title");

I know that gets the custom data.

0 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on April 20, 2017 18:40

Thanks Trevor

Unfortunately that doesn't seem to work either.

I wonder if it is because I am trying to databind to a FormView?

I am looking at repeaters and selected item transformations to do what I want to do.

Having said that if anyone knows or has any theories as to why this isn't working for me I would be really interested to find out.

Thanks again for your help.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on April 20, 2017 18:53

I didn't test this, but it might work:

tree.SelectNodes(className).TopN(1).Where("DocumentID",QueryOperator.Equals, docId);
0 votesVote for this answer Mark as a Correct answer

Oakwood Admin answered on April 21, 2017 10:57

Thanks Zac

That doesn't work either I am afraid.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.