How to get the list of external Users shared for a particular document library using CSOM?

Hello SharePointers,

Below is the CSOM code to get list of external Users shared for a particular document library using CSOM.

string URL = “https://yourtenant.sharepoint.com/sites/”;
string userName = “user@yourtenant.onmicrosoft.com”;
string userPassword = “password”;
string fullDocumentUrl = “https://yourtenant.sharepoint.com/sites//Shared%20Documents/my_sample_document.xlsx”;

using (var cc = new ClientContext(Url))
{
var passWord = new SecureString();
foreach (char c in userPassword.ToCharArray()) passWord.AppendChar(c);
cc.Credentials = new SharePointOnlineCredentials(userName, passWord);

ObjectSharingSettings info = Web.GetObjectSharingSettings(cc, fullDocumentUrl, 0, true);

ObjectSharingInformationUserCollection sharedUsers = info.ObjectSharingInformation.SharedWithUsersCollection;

cc.Load(sharedUsers, su => su.Where(user => user.IsExternalUser == true).Include(user => user.LoginName));

cc.ExecuteQuery();

foreach (var username in sharedUsers)
{
Console.WriteLine(username.LoginName);
}
}

Happy SharePointing 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *