{"id":900,"date":"2012-03-05T19:21:55","date_gmt":"2012-03-05T19:21:55","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=900"},"modified":"2012-06-20T00:40:01","modified_gmt":"2012-06-19T23:40:01","slug":"quick-tip-5","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2012\/03\/05\/quick-tip-5\/","title":{"rendered":"Disable SharePoint PeopleEditor"},"content":{"rendered":"<p><strong>SharePoint Short #5<\/strong><br \/>\nIf you have a requirement to disable the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.webcontrols.peopleeditor.aspx\" title=\"People Editor Control\" target=\"_blank\">PeopleEditor<\/a> in SharePoint through JavaScript, the following script will do this for you:<\/p>\n<pre lang=\"javascript\">\r\nfunction togglePeopleEditor(editor, enable) {\r\n    \/\/ Disables the control but still allows the user to type into it.\r\n    editor.attr('disabled', enable ? '' : 'disabled');\r\n\r\n    \/\/ Previous control is div element, used to control the textarea control\r\n    var editorDiv = editor.find('textarea').prev();\r\n\r\n    \/\/ When not enabling, ensure text shown in people picker has been resolved, otherwise clear it\r\n    if (!enable) {\r\n        var entityData = editorDiv.find('#divEntityData');\r\n        if (entityData != undefined) {\r\n            if (entityData.attr('isresolved') == undefined || entityData.attr('isresolved') == 'False') {\r\n                editorDiv.html('&nbsp;');\r\n            }\r\n        }\r\n        \/\/ Hide any error messages\r\n        editor.find('.ms-error').css('display', 'none');\r\n    }\r\n\r\n    \/\/ Hide any errors from view when the people editor is disabled\r\n    if (!enable) {\r\n        editor.find('.ms-error').css('display', 'none');\r\n    }\r\n\r\n    \/\/ Set text colour to Grey so that the control appears disabled in Firefox\r\n    editorDiv.css('color', enable ? 'windowtext' : 'Grey');\r\n    editorDiv.attr('contentEditable', false);\r\n\r\n    \/\/ Ensure all child span elements display correct text colour and disable or enable content editing\r\n    editorDiv.find('span').each(function () {\r\n        $(this).css('color', enable ? 'windowtext' : 'Grey');\r\n        $(this).attr('contentEditable', enable);\r\n    });\r\n\r\n    \/\/ The following will enable or disable the 'Check Name' and 'Browse' links.\r\n    editor.find('a').attr('disabled', enable ? '' : 'disabled').css('cursor', enable ? 'pointer' : 'default');\r\n    \/\/ Toggle the child anchor element event handlers\r\n    editor.find('a').each(function () {\r\n        if (enable) {\r\n            if ($(this).attr('onclick') == null)\r\n                this.onclick = $(this).data('onclick_data'); \/\/ Restore previously deleted click event\r\n        }\r\n        else {\r\n            if ($(this).data('onclick_data') == undefined)\r\n                $(this).data('onclick_data', this.onclick); \/\/ Store click data before removing the current event\r\n            $(this).removeAttr('onclick');\r\n        }\r\n    });\r\n}<\/pre>\n<p>Important aspects of the script are:<\/p>\n<ul>\n<li>Setting the <em>contenteditable<\/em> attribute of the div (id is postfixed <em>_upLevelDiv<\/em>) that immediately precedes the textarea control to false will disable the control and not allow the user to type directly into it.<\/li>\n<li>Removing un-resolved text from the people picker when disabling &#8211; this stops validation errors<\/li>\n<li>Removing the <em>onclick<\/em> event handlers when disabled and restoring when re-enabling &#8211; this is only required for Firefox as other browsers do not fire the event as the parent control is set to disabled.<\/li>\n<\/ul>\n<p>It should be noted that the people picker control this script assumes only one user or group, if allowing more you&#8217;ll need to update the logic for clearing the content when disabling to handle multiple entities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SharePoint Short #5 If you have a requirement to disable the PeopleEditor in SharePoint through JavaScript, the following script will do this for you: function togglePeopleEditor(editor, enable) { \/\/ Disables the control but still allows the user to type into &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2012\/03\/05\/quick-tip-5\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[44],"tags":[81,45],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-ew","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/900"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=900"}],"version-history":[{"count":14,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":1051,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/900\/revisions\/1051"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}