Enable Or Disable External Sharing For The SharePoint Site Using JSOM

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.

Properties Used: SPWeb.membersCanShare (SP.js)
Supports: SharePoint Online, SharePoint 2013+

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.

  1. //Author: Shantha Kumar T  
  2. //Property used: SP.Web.membersCanShare(sp.js)  
  3. //Supports SharePoint 2013 + and SharePoint Online  
  4. //Gets the external sharing status for the members  
  5.   
  6. function getmemberStatus(strUrl) {  
  7.     var clientContext = new SP.ClientContext();  
  8.     oSite = clientContext.get_site();  
  9.     oWeb = oSite.openWeb(strUrl);  
  10.   
  11.     clientContext.load(oWeb, "MembersCanShare");  
  12.   
  13.     clientContext.executeQueryAsync(  
  14.         Function.createDelegate(thisfunction() {  
  15.             //The below line returns the booelan value about the member status               
  16.             if (oWeb.get_membersCanShare()) {  
  17.                 alert("Sharing the site to external users allowed for members");  
  18.             } else {  
  19.                 alert("Sharing the site to external users disabled for members");  
  20.             }  
  21.         }),  
  22.         Function.createDelegate(thisfunction(sender, args) {  
  23.             alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  24.         }));  
  25. }  
  26.   
  27. function injectMethod() {  
  28.     var webServerRelativeUrl = '/';  
  29.     getmemberStatus(webServerRelativeUrl);  
  30. }  
  31. ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");  

Disable the Sharing access:

The below example disable the external sharing to members group in the SharePoint site. 

  1. //Author: Shantha Kumar T  
  2. //Property used: SP.Web.membersCanShare(sp.js)  
  3. //Supports SharePoint 2013 + and SharePoint Online  
  4. //Disables the external sharing access for the members  
  5.   
  6. function getmemberStatus(strUrl) {  
  7.     var clientContext = new SP.ClientContext();  
  8.     oSite = clientContext.get_site();  
  9.     oWeb = oSite.openWeb(strUrl);  
  10.   
  11.     oWeb.set_membersCanShare(false);  
  12.     oWeb.update()  
  13.      
  14.  clientContext.load(oWeb, "MembersCanShare");  
  15.   
  16.     clientContext.executeQueryAsync(  
  17.         Function.createDelegate(thisfunction() {  
  18.             //The below line returns the booelan value about the member status               
  19.             if (oWeb.get_membersCanShare()) {  
  20.                 alert("Sharing the site to external users allowed for members");  
  21.             } else {  
  22.                 alert("Sharing the site to external users disabled for members");  
  23.             }  
  24.         }),  
  25.         Function.createDelegate(thisfunction(sender, args) {  
  26.             alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  27.         }));  
  28. }  
  29.   
  30. function injectMethod() {  
  31.     var webServerRelativeUrl = '/';  
  32.     getmemberStatus(webServerRelativeUrl);  
  33. }  
  34. 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.

  1. //Author: Shantha Kumar T  
  2. //Property used: SP.Web.membersCanShare(sp.js)  
  3. //Supports SharePoint 2013 + and SharePoint Online  
  4. //Enables the external sharing access to members  
  5.   
  6. function getmemberStatus(strUrl) {  
  7.     var clientContext = new SP.ClientContext();  
  8.     oSite = clientContext.get_site();  
  9.     oWeb = oSite.openWeb(strUrl);  
  10.   
  11.     oWeb.set_membersCanShare(true);  
  12.     oWeb.update()  
  13.      
  14.  clientContext.load(oWeb, "MembersCanShare");  
  15.   
  16.     clientContext.executeQueryAsync(  
  17.         Function.createDelegate(thisfunction() {  
  18.             //The below line returns the booelan value about the member status               
  19.             if (oWeb.get_membersCanShare()) {  
  20.                 alert("Sharing the site to external users allowed for members");  
  21.             } else {  
  22.                 alert("Sharing the site to external users disabled for members");  
  23.             }  
  24.         }),  
  25.         Function.createDelegate(thisfunction(sender, args) {  
  26.             alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  27.         }));  
  28. }  
  29.   
  30. function injectMethod() {  
  31.     var webServerRelativeUrl = '/';  
  32.     getmemberStatus(webServerRelativeUrl);  
  33. }  
  34. 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. 

Up Next
    Ebook Download
    View all
    Learn
    View all