SP.Web (JS) is an object from SharePoint JavaScript API, which is used to retrieve the SharePoint website object and its various information.
There are lot of useful properties and methods available in SP.Web object, that can do some unique tasks in web scope level. Here, we are going to see how the property is used to allow or disallow the users from members group to share the site for the external users.
Get or set a Boolean value that specifies whether this site is enabled for members to share for external users.
SP.Web.get_membersCanShare() | Used to get the Boolean value, whether the site is enabled for external sharing by members |
SP.Web.set_membersCanShare(true) | Allows the members to share the site to external users |
SP.Web.get_membersCanShare(false) | Dis-allows the members to share the site to external users |
Note: Default value is true. And the site is enabled for external sharing by default.
Get external sharing status:
The below example identifies whether the site has external sharing permission for members.
- //Author: Shantha Kumar T
- //Property used: SP.Web.membersCanShare(sp.js)
- //Supports SharePoint 2013 + and SharePoint Online
- //Gets the external sharing status for the members
- function getmemberStatus(strUrl) {
- var clientContext = new SP.ClientContext();
- oSite = clientContext.get_site();
- oWeb = oSite.openWeb(strUrl);
- clientContext.load(oWeb, "MembersCanShare");
- clientContext.executeQueryAsync(
- Function.createDelegate(this, function() {
- //The below line returns the booelan value about the member status
- if (oWeb.get_membersCanShare()) {
- alert("Sharing the site to external users allowed for members");
- } else {
- alert("Sharing the site to external users disabled for members");
- }
- }),
- Function.createDelegate(this, function(sender, args) {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }));
- }
- function injectMethod() {
- var webServerRelativeUrl = '/';
- getmemberStatus(webServerRelativeUrl);
- }
- ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");
Disable the Sharing access:
The below example disable the external sharing to members group in the SharePoint site.
- //Author: Shantha Kumar T
- //Property used: SP.Web.membersCanShare(sp.js)
- //Supports SharePoint 2013 + and SharePoint Online
- //Disables the external sharing access for the members
- function getmemberStatus(strUrl) {
- var clientContext = new SP.ClientContext();
- oSite = clientContext.get_site();
- oWeb = oSite.openWeb(strUrl);
- oWeb.set_membersCanShare(false);
- oWeb.update()
- clientContext.load(oWeb, "MembersCanShare");
- clientContext.executeQueryAsync(
- Function.createDelegate(this, function() {
- //The below line returns the booelan value about the member status
- if (oWeb.get_membersCanShare()) {
- alert("Sharing the site to external users allowed for members");
- } else {
- alert("Sharing the site to external users disabled for members");
- }
- }),
- Function.createDelegate(this, function(sender, args) {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }));
- }
- function injectMethod() {
- var webServerRelativeUrl = '/';
- getmemberStatus(webServerRelativeUrl);
- }
- ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");
The screenshot below shows, when the site is disabled for the external access.

Enable sharing access:
The below example helps to enable the sharing permissions to site members.
- //Author: Shantha Kumar T
- //Property used: SP.Web.membersCanShare(sp.js)
- //Supports SharePoint 2013 + and SharePoint Online
- //Enables the external sharing access to members
- function getmemberStatus(strUrl) {
- var clientContext = new SP.ClientContext();
- oSite = clientContext.get_site();
- oWeb = oSite.openWeb(strUrl);
- oWeb.set_membersCanShare(true);
- oWeb.update()
- clientContext.load(oWeb, "MembersCanShare");
- clientContext.executeQueryAsync(
- Function.createDelegate(this, function() {
- //The below line returns the booelan value about the member status
- if (oWeb.get_membersCanShare()) {
- alert("Sharing the site to external users allowed for members");
- } else {
- alert("Sharing the site to external users disabled for members");
- }
- }),
- Function.createDelegate(this, function(sender, args) {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }));
- }
- function injectMethod() {
- var webServerRelativeUrl = '/';
- getmemberStatus(webServerRelativeUrl);
- }
- ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");
Below is the screenshot which we get after enabling the sharing access to site member users.

After Clicking Share button from the page, SharePoint shows the success message as a notification message in site.
Note:
To get the value of this property, we have to explicitly specify the property name MembersCanShare in ClientContext.Load() method.
Happy cracking the SharePoint world.

Shantha Kumar TPosted Jul 12, 2016, 11:34 AM
You will get this property available from SharePoint 2013 (On-Premise) latest update.
FredPosted Jul 12, 2016, 10:50 AM
Although it is mentioned in your script, it does NOT work in SharePoint 2013+. The property "MembersCanShare" does not exist in SharePoint 2013 (On-Premise), it is ony available in SharePoint Online.
Shantha Kumar TPosted May 20, 2016, 11:37 AM
Thanks Munesh and Debasis
Debasis SahaPosted May 20, 2016, 10:54 AM
Nice One..
Munesh SharmaPosted May 10, 2016, 1:38 PM
nice