15

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.

  • 1
    Attribute being value providers should be like :{!v.attributeName}. – Praveen Mar 23 '17 at 6:37
  • @Praveen Sorry My bad . Just Assume it like a value provider i.e {! v.urlParamName}. – Tarique Shamim Mar 23 '17 at 8:25
  • I'll add the comment as answer and accept it, so that we can close the question. – Praveen Mar 23 '17 at 9:04
  • @Praveen this can't be the answer. It doesn't works in lightning. – Tarique Shamim Mar 23 '17 at 9:22
  • Apart from the value provider, you are missing aura:application which you need to use as pointed by Santanu. – Praveen Mar 23 '17 at 9:42
22

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"

  • 1
    This worked for me - thank you! I found this link to be helpful: PageReference Types – Brian Miller Jun 11 '18 at 13:35
  • 10
    The original question asked about get params in a community context. lightning:isUrlAddressable does not work in communities. – Tyler Zika Jul 28 '18 at 6:24
  • @TylerZika you are right. It is not working in communities. – Tarique Shamim Sep 24 '18 at 16:36
11

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!

  • 2
    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. – Tarique Shamim Mar 24 '17 at 3:39
  • I didn't get that from your question. Anyway I have updated my answer. – Santanu Halder Mar 24 '17 at 9:12
  • I want to get the Parameters from the URL directly without using any JS code. – Tarique Shamim Mar 29 '17 at 6:37
4

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? – patryk Nov 30 '17 at 13:38
  • @pepper42 - ask a new question. Better odds to get it solved – stwissel Dec 1 '17 at 0:22
  • 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. – bpilling Jan 19 '18 at 0:25
  • Glad it worked out for you – stwissel Jan 19 '18 at 0:27
0

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];
        }
    }
}

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.