SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

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

});
share|improve this answer
    
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 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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