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();

    }
}
5
  • you can use _spPageContextInfo.userDisplayName or _spPageContextInfo.userEmail or other info from _sppagecontextinfo. Commented Sep 6, 2018 at 8:47
  • can you give me an exemple please?
    – Jihen
    Commented Sep 6, 2018 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. Commented Sep 6, 2018 at 9:27
  • Thnx Bhaskar It work :) can you make your comment as answer?
    – Jihen
    Commented Sep 6, 2018 at 9:42
  • Great !! Sure I will do :) Commented Sep 6, 2018 at 9:44

2 Answers 2

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>
2
  • _spPageContextInfo.userDisplayName - will this not give you similar result ? Commented Sep 6, 2018 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
    Commented Sep 7, 2018 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 and acknowledge you have read our privacy policy.

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