Get Document using page url

Murugesan Manokaran asked on May 13, 2019 20:13

I have a url selector as a field in a page type.

need to display the document name with that field url in the page transformation

possible?

Recent Answers


Mike Wills answered on May 14, 2019 01:41

Hi Murugesan,

It's possible. If using Text/XML transformations, you would need to add a macro that queries the document to get the name. Be aware that this would cause a database round trip. If the transformation is used in a repeater, it could have a performance impact.

Mike

0 votesVote for this answer Mark as a Correct answer

Murugesan Manokaran answered on May 14, 2019 14:46

Thank you, Mike. I am planning to create a custom macro to achieve this.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on May 14, 2019 15:15 (last edited on May 14, 2019 15:23)

No need for macro. if you are using transformation, which is ascx control, you use pure c#: should be something like:

<script runat="server">
  string docName = "";

  protected override void OnInit(EventArgs e) 
  {
    var docURL = CMS.Helpers.ValidationHelper.GetString(Eval("URLselectorFieldName"), "");
    // you get something like ~/News/Apple-iPad-2-In-Stock.aspx - you need strip  some parts 
    // in order to get '/News/Apple-iPad-2-In-Stock'
    docURL = docURL.Replace("~","").Replace(".aspx","")

    if (docURL != "") 
    {
            docName = CMS.DocumentEngine.DocumentHelper.GetDocuments("your doc type")
            .WhereEquals("NodeAliasPath", docURL)
            .FirstOrDefault()?.GetValue("DocumentName");
    }
  }
</script>

<%=docName%><br>

It is not tested - it just to give you the direction. Don't forget about performance probably your transformation will be called many times and each time it will produce a call to resolve a document name. I dont really know you set up, and how the url are defined.

You can go with macro but DocumentHelper can let do more efficient query than {% Documents %} macro.

0 votesVote for this answer Mark as a Correct answer

Murugesan Manokaran answered on May 14, 2019 15:22

we need this function in multiple places with repeater. So, created a macro as below and call it when the input data string is not null or empty: [MacroMethod(typeof(string), "Returns Page Name for the given NodeAliasPath", 1)] [MacroMethodParam(0, "pageUrl", typeof(string), "Page Url (string)")] public static string GetPageNameByUrl(EvaluationContext context, params object[] parameters) { string pageUrl = ValidationHelper.GetString(parameters[0], ""); if (string.IsNullOrEmpty(pageUrl)) { return ""; } TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

        // Gets the page that will be deleted
        TreeNode page = tree.SelectNodes()
            .Path(pageUrl.Replace("~",""))  
            .OnCurrentSite()
            .Culture("en-us")
            .FirstObject;
        if(page != null)
        {
            return page.DocumentName;
        }
        return "";
    }
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on May 14, 2019 15:55

It could be performance issue resolving documentname for each row in repeaters in multiple places. You probably need to use dictionary to cache this. You are producing an extra query for each row in repeater.

0 votesVote for this answer Mark as a Correct answer

Murugesan Manokaran answered on May 14, 2019 15:59

I understood your concern and we had enough check before using this macro function in sync with our existing architecture

0 votesVote for this answer Mark as a Correct answer

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