Forum Discussion

cglowe's avatar
cglowe
Cloud Apps
02-08-2022
Solved

Dynamic username on dashboard welcome message

I have a landing (homepage) dashboard that includes a text widget with welcome message.  I would like the welcome message to include the viewer's {username} or {first name}, but am not sure the best ...
  • mattmca's avatar
    mattmca
    02-09-2022

    I think your code should work if you add a couple pieces:

    widget.on('ready', function(){
    	
    	var eleExists = document.getElementById("myPara");
        if(!eleExists){
           	const para = document.createElement("p");
    		para.setAttribute("id", "myPara");
    		const node = document.createTextNode("Welcome " + prism.user.firstName + "!");
    		para.appendChild(node);
    		const element = document.getElementById("editor-3");
    		element.appendChild(para);
        }
    })

    It adds an id to your paragraph element and first checks to make sure that id doesn't exist before adding a new paragraph & text.

  • Alek_qbeeq's avatar
    Alek_qbeeq
    02-09-2022

    cglowe You can also play with blox to get the desired effect.

    Blox:

    {
        "style": "",
        "script": "",
        "title": "",
        "showCarousel": true,
        "body": [
            {
                "type": "Container",
                "items": [
                    {
                        "type": "ColumnSet",
                        "columns": [
                            {
                                "type": "Column",
                                "width": "50%",
                                "items": [
                                    {
                                        "type": "TextBlock",
                                        "text": "❤️️ Welcome ️",
                                        "style": {
                                            "text-align": "center",
                                            "font-weight": "bold",
                                            "font-size": "24px",
                                            "margin-top": "50px",
                                            "margin-bottom": "50px",
                                            "margin-left": "50%",
                                            "text-aligment": "right"
                                        }
                                    }
                                ]
                            },
                            {
                                "type": "Column",
                                "width": "30%",
                                "items": [
                                    {
                                        "type": "TextBlock",
                                        "text": "️test",
                                        "id": "myUser",
                                        "style": {
                                            "text-align": "center",
                                            "font-weight": "bold",
                                            "font-size": "24px",
                                            "margin-top": "50px",
                                            "margin-bottom": "50px",
                                            "margin-left": "-70%",
                                            "color": "blue"
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        "actions": []
    }

    widget script:

    widget.on("ready", function (w, args) {
    $("#myUser").text(prism.user.firstName + ' ' + prism.user.lastName)
    })

    This is just simple example but within column set you can put any TextBlock/data you want (static text, data from elasticube etc).