Hi @Omri Kaufman ,
We need to check “Limited-access user permission lockdown mode“ is deactivate. Otherwise the folder is unable to be shared with external users.
Go to site Contents>Site Settings>Site Collection Features
And Change your site sharing configuration to be "Anyone – Users can share files and folders using links that don’t require sign-in"
Here is code for sample:
var users = new List<string>() { "user1", "user2 " };
var userRoles = new List<UserRoleAssignment>();
foreach (var user in users)
{
clientContext.Web.EnsureUser(user);
UserRoleAssignment role = new UserRoleAssignment();
role.UserId = user;
role.Role = Role.View; //Role.Edit
userRoles.Add(role);
}
string absoluteFileUrl = "folderurl";
DocumentSharingManager.UpdateDocumentSharingInfo(clientContext, absoluteFileUrl, userRoles, true, true, true, "Test Folder", true, true);
clientContext.ExecuteQuery();
Console.WriteLine("success");
Console.ReadLine();
And the code will send an email to user’s mailbox. But it can only add permission to existing external users.
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.