Question

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

    }
}
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top