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.
-
-
-
-
-
- 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() {
-
- 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.
-
-
-
-
-
- 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() {
-
- 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.
-
-
-
-
-
- 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() {
-
- 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.