2

I can retrieve Shared With info from a SharePoint 2013 (On Premisses) List/Library (not an item) using Javascript?

I thought it could be available through SP.ObjectSharingInformation.getObjectSharingInformation method, but I´m stucked on getSharedWithUsers() method throwing an error like The collection has not been initialized.

Should I load other properties in context?

My bitcode, trying to get a simple count of shared users/groups:

var ctx = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = ctx.get_web().get_lists().getById(_spPageContextInfo.pageListId);
ctx.load(list);
var su = SP.ObjectSharingInformation.getObjectSharingInformation(ctx, list);
console.log(su.getSharedWithUsers().get_count());

Error:

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

4

Few things are missing

  1. Loading su.getSharedWithUsers()
  2. Calling ctx.executeQueryAsync()

Modified code

var ctx = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = ctx.get_web().get_lists().getById(_spPageContextInfo.pageListId);
var su = SP.ObjectSharingInformation.getObjectSharingInformation(ctx, list);

var sharedWithUsers = su.getSharedWithUsers();
ctx.load(sharedWithUsers);

ctx.executeQueryAsync(function() {
    console.log(sharedWithUsers.get_count());
}, function() {

});
  • Atish, thank you very much. With your help I could understand better how it works and move forward to loop through those items, retrieving all values I needed. Thanks! :) ctx.executeQueryAsync(function() { var sharedEnumerator = sharedWithUsers.getEnumerator(); while (sharedEnumerator.moveNext()) { var usr = sharedEnumerator.get_current(); console.log(usr.get_name()); } }, function (sender, args){ alert('Request failed retrieving Shared Users. ' + args.get_message() + '\n' + args.get_stackTrace()); }); – Edson Bassani Feb 2 '16 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.