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.
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>