2

I want to check if current user = "xxx" and make the field "yyy" as Read Only.

$(document).ready(function () {
    var userName = $().SPServices.SPGetCurrentUser();

    if(userName == "xxx") {
        SPUtility.GetSPField('yyy').MakeReadOnly();

    }
}
  • you can use _spPageContextInfo.userDisplayName or _spPageContextInfo.userEmail or other info from _sppagecontextinfo. – Bhaskar Dhone Sep 6 '18 at 8:47
  • can you give me an exemple please? – Jihen Sep 6 '18 at 9:24
  • var userName = $().SPServices.SPGetCurrentUser(); instead of this you can simply use var userName = _spPageContextInfo.userEmail; if your username field expects email address. – Bhaskar Dhone Sep 6 '18 at 9:27
  • Thnx Bhaskar It work :) can you make your comment as answer? – Jihen Sep 6 '18 at 9:42
  • Great !! Sure I will do :) – Bhaskar Dhone Sep 6 '18 at 9:44
2

@bhaskar : For this Solution we can use only UserId and it work well :)

But if we have to use a loginName we can do This :

<script src="/SiteAssets/jquery-1.12.3.min.js"></script>
<script src="/SiteAssets/sputility.js"></script>
<script>
    $(document).ready(function () {
        var userid= _spPageContextInfo.userId;
        var requestUri = _spPageContextInfo.webAbsoluteUrl +"/_api/web/getuserbyid("+ userid + ")";
        var requestHeaders = { "accept" : "application/json;odata=verbose" };
        $.ajax({
            url : requestUri,
            contentType : "application/json;odata=verbose",
            headers : requestHeaders,
            success : onSuccess,
            error : onError
        });

        function onSuccess(data, request){
            var loginName = data.d.Title;
            if(loginName == "xxx") {
                SPUtility.GetSPField('yyy').MakeReadOnly();
            }
        }

        function onError(error) {
            alert("error");
        }   
    });
</script>
  • _spPageContextInfo.userDisplayName - will this not give you similar result ? – Bhaskar Dhone Sep 6 '18 at 12:33
  • there are no userDisplayName. Only userId. If I have the IDs of the other users it will be perfect and short. blah.winsmarts.com/… – Jihen Sep 7 '18 at 7:38
2

You can get user information from _spPageContextInfo to validate eg.

$(document).ready(function () {
    var userName = _spPageContextInfo.userEmail;

    if(userName == "xxx") {
        SPUtility.GetSPField('yyy').MakeReadOnly();

    }
}

like email you can also get userDisplayName , userLoginId, userId etc.

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.