0

I have a clickable image that exists on a classic sharepoint online team site that links to a page stored in the /SitePages libraray. This was integrated through a CEWP that points to a simple html file.

I would like to learn if there's a way to ensure that the hyperlink in the html file can be dynamic, always pointing to the latest page in the /SitePages library.

I'm not a developer but I was wondering if it's possible to write some javascript that gets the link of the page with the latest modified date and append this to the link in the <a href="">of the html file.

But perhaps you can think of a way easier and smarter way.

Thanks for your input.

1 Answer 1

0

You can add some small javascript code which will query to site pages library with Order by Modified time and with item limit 1.

Once you get item, you can get URL of that page easily. (You can use EncodedAbsUrl for that.

There are couple of way to query in SharePoint list i.e. CSOM or REST.

Below is the code for your requirement which you need to place it in CEWP HTML:

            <html>

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" x-undefined="" />
            <title></title>
            <script>
                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
                    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Websiteseiten')/Items?$select=EncodedAbsUrl&$orderby=Modified desc&$top=1"
                    jQuery.ajax({
                        url: requestUrl,
                        type: "GET",
                        async: false,
                        headers: {
                            "accept": "application/json;odata=verbose",
                        },
                        success: function (data, textStatus, jqXHR) {
                            if(data && data.d && data.d.results && data.d.results.length > 0){
                                jQuery("#latest-site-page-link").attr("href",data.d.results[0].EncodedAbsUrl);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        }
                    });
                });
            </script>
        </head>

        <body>
            <a href='' id='latest-site-page-link'>Latest Site Page Link</a>
        </body>

        </html>

Below is the place where you need to place that html:

enter image description here

Note that you need to load jquery as below code is using jquery. In my case jquery is already loaded so I haven't loaded it in CEWP.

Hope it will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.