0

I have created a list for Managers to submit change requests to HR. We would like for certain people to be able to open the list item. Those people will be included in 4 fields. I would like to use JavaScript to check to see if the current logged in user is in any of those fields is the user tries to open the form. If not, we want to redirect them elsewhere. If they are, allow them to see the page. I am using SharePoint Online and Infopath to create the form.

1 Answer 1

0

Use JSOM to generate the path based on the conditions and overwrite the column value. Use _spPageContextInfo to get the current user name. Check if the current user is in the People or Group groups.

<script type="text/javascript">
(function () {
    var overrideNameField = {}; 
    overrideNameField.Templates = {};
    // This action will edit the “Title” field. You can change the “Title” to another custom field. 
    //"LinkTitle" is the internal name of the "Title" field
    overrideNameField.Templates.Fields = {
        "LinkTitle": { "View": overrideNameFieldTemplate }        
    };    
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideNameField);
})();

function overrideNameFieldTemplate(ctx) { 
    //get current user userLoginName 
    var loginName = _spPageContextInfo.userLoginName.toLowerCase();
    console.log(loginName);

    //get the current item id
    var itemId = ctx.CurrentItem.ID;
    //get the current item title
    var content = ctx.CurrentItem["Title"];

    //get the user from a People or Group column
    // People or Group column will actually returns multiple values, using ctx.CurrentItem.User[i] is the correct way to get the column value.
    //"user1" is the internal name of my people column
    var user1=ctx.CurrentItem["user1"];
    var userName1 = user1[0].email.toLowerCase();
    console.log(userName1);
    //If the column contains multiple value, we can use:
    //for (var i = 0; i < currentItem.get_item("User").length; i++) {user1[i].email;}

    //get other users from other People or Group columns
    //var user2=ctx.CurrentItem["user2"];
    //......

    var url = "";
    if (loginName != userName1){
    // If (loginName != userName1 && loginName != userName2 && loginName != userName3 && loginName != userName4){
        //if the user does not exist in the People or Group columns
        url = "<page>";

    } else{
        // open the form
        // the url of the InfoPath form. E.g. https://company.sharepoint.com/sites/team/Lists/testList/Item/displayifs.aspx?ID=1
        url = _spPageContextInfo.webAbsoluteUrl +"/Lists/cl_user/Item/displayifs.aspx?ID="+ itemId;    
    }

    return "<a href='" + url+ "'>" + content + "</a>";  
}
</script>

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.