1

In SharePoint Online, is there a way I can create a dynamic link based on the current user?

For example: I have a list called Staff Directory with the following columns:

  • Email - first.last@company.com (Single Line of Text)
  • Display Name - last, first@company (people/group)

On my home page, I want to add a link for Update your Profile that opens the Staff Directory list item matching the Email or Display Name for the current user.

enter image description here

Hopefully, above make sense. Thank you.

EDIT Below is the final code I used thanks to Venkat's input to open the specific list item based on current user and in dialog/modal:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#hlnkUpdateProfile").on("click", function() {
    var currentUserEmail = _spPageContextInfo.userEmail;
    var requestUrl = "https://xxx.sharepoint.com/teams/xxx/_api/web/Lists/getByTitle('Staff Directory')/Items?$filter=Title eq '" + currentUserEmail + "'&$select=Id";
    $.ajax({
        url: requestUrl,
        method: "GET",
        async: false,
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            if(data.d.results.length > 0) {
                var editFormUrl= "https://xxx.sharepoint.com/teams/xxx/Lists/Staff%20Directory/EditForm.aspx?ID=" + data.d.results[0].Id;
        var options = {url: editFormUrl, width: 700, height:600, title: "Edit your profile"};
        SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
            }else {
                alert("Your profile not found. Create a profile.");
            }                   
        },
        error: function (data) {
            alert("Failed to load your profile");
        }
    });
});

});
</script>
  • It is not clear for me. Could you provide more info about what are you trying to do... – Juan Pablo Pussacq Laborde Jun 22 '17 at 18:57
  • Hi jpussacq, I modified my initial question. Hopefully I made it a little clearer. thanks. – Tamras Merin Jun 22 '17 at 19:52
  • You can use somo client object model inside your CEWP... – Juan Pablo Pussacq Laborde Jun 22 '17 at 20:11
2

Here is an example which can help you to solve the problem.

HTML

<a href="https://site/Lists/StaffDirectory/NewForm.aspx" id="hlnkCreateProfile">Create you profile</a>
<a href="javascript:" id="hlnkUpdateProfile">Update you profile</a>

JavaScript

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#hlnkUpdateProfile").on("click", function() {
        var currentUserEmail = _spPageContextInfo.userEmail;
        var requestUrl = "https://site/_api/web/Lists/getByTitle('Staff Directory')/Items?$filter=Email eq '" + currentUserEmail + "'&$select=Id";
        $.ajax({
            url: requestUrl,
            method: "GET",
            async: false,
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                if(data.d.results.length > 0) {
                    var editFormUrl= "https://site/Lists/StaffDirectory/EditForm.aspx?ID=" + data.d.results[0].Id;

                    window.location.href = editFormUrl
                }else {
                    alert("Your profile not found. Create a profile.");
                }                   
            },
            error: function (data) {
                alert("Failed to load your profile");
            }
        });
    });

});
</script>

As per this example,

  • when you click Create your profile it will redirect to the Staff Directory list new form.
  • When you click on Update your profile link it will fetch the profile by email in the list and get the ID. If it finds then redirects to edit form.

You can embed this script on the page where you want to execute this logic.

  • Is there I way I can combine this with another script that opens the link in modal or dialog? For my Create a Profile, I'm using <a href="javascript:displayLayover('Lists/Staff%20Directory/Item/newifs.aspx?IsDlg=1')">CREATE YOUR PROFILE</a> I tried adding the displayLayover in the HTML your provided for UPDATE YOUR PROFILE but I got an error message. Thanks again. – Tamras Merin Jun 22 '17 at 21:33
  • You can combine with other scripts. You can open a modal dialog as well. Example var pageUrl = "your page url"; var options = { url: pageUrl, width: 700, height: 600, title: " " }; SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options); – Venkat Konjeti Jun 22 '17 at 21:41
  • OMG I got it to work :) Thank you so much Venkat! I edited my original post to include the final script I used. Thanks again. – Tamras Merin Jun 22 '17 at 21:54

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.