Getting the URL parameter into Lightning component using the "{! paramName}" expression

  • I am new to communities in lightning and I am trying to figure out a way to directly fetch the URL parameters into the lightning component attributes using the {! urlParamName} expressions.

    for example :

    https://www.someurl.com/page?name=somename&surname=somesurname
    

    My lightning component will be :

    <aura:component>
    
        <!-- Parameters Available in URL-->
        <aura:attribute name="name" type="String">
        <aura:attribute name="surname" type="String">
    
        <!--It will get name parameter from URL directly-->
        Name is : {!v.name} 
    
        <!--It will get surname parameter from URL directly-->
        SurName is : {!v.surname}
    
    </aura:component>
    

    I am trying to figure out a way which cannot includes JS. Currently I am using Lightning JS to get these parameters.

    Attribute being value providers should be like :`{!v.attributeName}`.

    @Praveen Sorry My bad . Just Assume it like a value provider i.e {! v.urlParamName}.

    I'll add the comment as answer and accept it, so that we can close the question.

    @Praveen this can't be the answer. It doesn't works in lightning.

    Apart from the value provider, you are missing aura:application which you need to use as pointed by Santanu.

  • After Summer 18 release (API version 43 and up) we can do this.

    Implement lightning:isUrlAddressable interface and use pageReference attribute.

    Example. - Component Assume url is https://<instance>.lightning.force.com/lightning/cmp/<namespace>__componentName?testAttribute=abc

    <aura:component implements="lightning:isUrlAddressable">
       <aura:handler name="init" value="{!this}" action="{!c.doInit}" description="Handler for valueInit event fired when the component has been initialised"/>
            {!v.pageReference.state.testAttribute}
    </aura:component>
    

    Component Controller

    ({
        doInit : function(component, event, helper) {
            console.log(component.get("v.pageReference").state.testAttribute);
        }
    })
    

    Console output will look like: "abc"

    This worked for me - thank you! I found this link to be helpful: PageReference Types

    The original question asked about get params in a community context. `lightning:isUrlAddressable` does not work in communities.

    @TylerZika you are right. It is not working in communities.

    You now need to append namespace c__ to all parameters which you are including in url and component - so, state.c__testAttribute.

  • First thing first. You have written apex:attribute in your question which is not valid, you should use aura:attribute.

    Next, you have to declare the attributes in the app, not in the component in order to fetch them from the URL.

    <aura:application >
        <aura:attribute name="name" type="String"/>
        <aura:attribute name="surname" type="String"/>
        Name is : {!v.name} 
        SurName is : {!v.surname}
    </aura:application>
    

    This works perfectly fine.

    UPDATE: After coming to know that Tarique is looking for a solution in components in community, I did some research and this guy has done a good job for this.

    So basically you need to fetch the params using the JS controller. Handle the init event and then do the following to fetch the URL params:

    doInit : function(component, event, helper) {
            var sPageURL = decodeURIComponent(window.location.search.substring(1)); //You get the whole decoded URL of the page.
            var sURLVariables = sPageURL.split('&'); //Split by & so that you get the key value pairs separately in a list
            var sParameterName;
            var i;
    
            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split('='); //to split the key from the value.
    
                if (sParameterName[0] === 'firstName') { //lets say you are looking for param name - firstName
                    sParameterName[1] === undefined ? 'Not found' : sParameterName[1];
                }
            }
            console.log('Param name'+sParameterName[0]);
            console.log('Param value'+sParameterName[1]);
        }
    

    I have tried this in my org and it worked!

    I am using aura component and using it on community pages. So I have to fetch some parameters from URL. But the way you mentioned doesn't works for me.

    I didn't get that from your question. Anyway I have updated my answer.

    I want to get the Parameters from the URL directly without using any JS code.

    This code will give you the LAST parameter always. You need to break out of the loop when you find the one you are looking for Santanu.

  • It is actually much easier to pass URL parameters to Lighning components in a community page. The "trick" is to expose your attributes at design time to the page, so they show up in the Lightning page designer as component properties. You do that in the design definition (the .design file):

    <design:component label="Sample">
       <design:attribute name="someParam" label="Make Parameters great again" />
    </design:component>
    

    this presumes that you component has a parameter someParam in its attribute definition (in the .cmp file):

    <aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="someController">
        <aura:attribute name="someParam" type="String" />
        .... stuff here ...
    </aura:component>
    

    Let's presume your url looks like ....?color=blue then you add into the property value of your component, after you dragged it onto the lightning page in community builder:

     {!color}
    

    This will pull the value of the color URL parameter and hand it to the someParam attribute of your component. No JS required.

    Hope that helps

    Update:

    Passing URL parameters using component properties is not officially supported and only works on a full page load (which is slow).

    I have component from managed package which has attribute named Media Id. How can I get api name of that attribute to pass value?

    @pepper42 - ask a new question. Better odds to get it solved

    I found this answer worked only if I added access="global" to the individual aura:attribute tags... otherwise it did not work. But once I did this it worked a charm.

    Glad it worked out for you

  • Based on Damon Shaw's code, just add this helper method:

    getUrlParameter : function(sParam) {
        var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;
    
        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');
    
            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : sParameterName[1];
            }
        }
    }
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM