locked
Disable/Enable person or group column in a list for a particular person in sharepoint 2013 RRS feed

  • Question

  • Hello All,

    I have a list with columns say Title, Description,Approver1 and Approver2.

    Requirement are as below:

    • Title, Description and Approver1 should be editable by initiator
    • Approver2 should be editable only by Approver1
    • Approver2 should be disabled for others (but the value should be seen)
    • Approver2 and Approver1 are person or group type field which allows multiple persons
    • this needs to be implemented using javascript or jquery

    I tried various functions like hide() disable and prop(). Nothing seems to be working. It would be of real help if someone could help me with this. 


    Mitz Sanil

    Thursday, October 18, 2018 7:04 AM

Answers

  • Hi,

    For new form, just hide Approver2 by jQuery.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(function () {
                $("nobr:contains('Approver2')").closest('tr').hide();
            }, "sp.js");
        </script>


    For edit form, disable Approver2 by check if current user belongs Approver1.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(function () {
                var currentItemID = SP.ScriptHelpers.getDocumentQueryPairs()["ID"];
                var clientContext = new SP.ClientContext.get_current();
                var listId = _spPageContextInfo.pageListId;
                var web = clientContext.get_web();
                var currentUser = web.get_currentUser();
                var currentList = web.get_lists().getById(listId);
                var oListItem = currentList.getItemById(currentItemID);
                clientContext.load(currentUser);
                clientContext.load(oListItem);
                clientContext.executeQueryAsync(function () {
                    var members = oListItem.get_item('Approver1');
                    var _perCheck = false;
                    for (i = 0; i < members.length; i++) {
                        var member = members[i];
                        if (currentUser.get_id() == member.get_lookupId())
                            _perCheck = true;
                    }
                    if (!_perCheck) {
                        var dispTitle = "Approver2";
                        var pickerDiv = $("[id$='ClientPeoplePicker'][title='" + dispTitle + "']");
                        var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[pickerDiv[0].id];
                        //disable the field
                        peoplePicker.SetEnabledState(false);
                        //hide the delete/remove use image from the people picker
                        $(pickerDiv).find('.sp-peoplepicker-delImage').css('display', 'none');
                    }
                },
                function (sender, args) {
                    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
                });
            }, "sp.js");


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Proposed as answer by Dennis Guo Monday, October 22, 2018 9:35 AM
    • Marked as answer by MSanil Wednesday, October 24, 2018 11:52 AM
    Friday, October 19, 2018 2:44 AM
  • Hi,

    The script should works for hiding choice field which display as checkbox.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(function () {
                $("nobr:contains('CheckboxOptions')").closest('tr').hide();
            }, "sp.js");
        </script>

    While if you need future help, expecting you could post a new thread as a new topic and we could discuss there so this would help other community members find the helpful information quickly based on topic.

    Thanks for your understanding.



    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by MSanil Thursday, October 25, 2018 3:54 AM
    Thursday, October 25, 2018 1:40 AM

All replies

  • Hi

    You may have a try the field permission solution.

    https://sharepoint.stackexchange.com/questions/73106/field-level-permission-development


    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Thursday, October 18, 2018 7:07 AM
  • Or you can write js code in the new&edit form to control that behavior.

    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Thursday, October 18, 2018 7:08 AM
  • Hi,

    For new form, just hide Approver2 by jQuery.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(function () {
                $("nobr:contains('Approver2')").closest('tr').hide();
            }, "sp.js");
        </script>


    For edit form, disable Approver2 by check if current user belongs Approver1.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(function () {
                var currentItemID = SP.ScriptHelpers.getDocumentQueryPairs()["ID"];
                var clientContext = new SP.ClientContext.get_current();
                var listId = _spPageContextInfo.pageListId;
                var web = clientContext.get_web();
                var currentUser = web.get_currentUser();
                var currentList = web.get_lists().getById(listId);
                var oListItem = currentList.getItemById(currentItemID);
                clientContext.load(currentUser);
                clientContext.load(oListItem);
                clientContext.executeQueryAsync(function () {
                    var members = oListItem.get_item('Approver1');
                    var _perCheck = false;
                    for (i = 0; i < members.length; i++) {
                        var member = members[i];
                        if (currentUser.get_id() == member.get_lookupId())
                            _perCheck = true;
                    }
                    if (!_perCheck) {
                        var dispTitle = "Approver2";
                        var pickerDiv = $("[id$='ClientPeoplePicker'][title='" + dispTitle + "']");
                        var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[pickerDiv[0].id];
                        //disable the field
                        peoplePicker.SetEnabledState(false);
                        //hide the delete/remove use image from the people picker
                        $(pickerDiv).find('.sp-peoplepicker-delImage').css('display', 'none');
                    }
                },
                function (sender, args) {
                    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
                });
            }, "sp.js");


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Proposed as answer by Dennis Guo Monday, October 22, 2018 9:35 AM
    • Marked as answer by MSanil Wednesday, October 24, 2018 11:52 AM
    Friday, October 19, 2018 2:44 AM
  • Hi Lee,

    I am sorry for the late reply it has been of real help. This worked for me like a charm. Thank you :)


    Mitz Sanil

    Wednesday, October 24, 2018 11:52 AM
  • Hello Lee,

    How do we hide a field of type choice (but display choices using checkboxes). I was able to hide a field of type choice when displayed as dropdown but unable to do so for checkboxes. Could you guide me with this.

    Your help will be very much appreciated.

    Thank You

    Mithali


    Mitz Sanil

    Wednesday, October 24, 2018 12:09 PM
  • Hi,

    The script should works for hiding choice field which display as checkbox.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            ExecuteOrDelayUntilScriptLoaded(function () {
                $("nobr:contains('CheckboxOptions')").closest('tr').hide();
            }, "sp.js");
        </script>

    While if you need future help, expecting you could post a new thread as a new topic and we could discuss there so this would help other community members find the helpful information quickly based on topic.

    Thanks for your understanding.



    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by MSanil Thursday, October 25, 2018 3:54 AM
    Thursday, October 25, 2018 1:40 AM
  • Hi

    As you mentioned above in your post, I have created a new post for this topic. I actually wanted to disable it and not hide.

    here is the link of the post : https://social.msdn.microsoft.com/Forums/office/en-US/94e12dd3-902c-4d4f-91f7-a9c25b66f3cc/disable-a-choice-field-which-display-as-checkbox?forum=sharepointdevelopment

    Thank you


    Mitz Sanil

    Thursday, October 25, 2018 4:31 AM