I have a PowerShell script that sends a user an invitation email. Which works. The user I'm testing with gets an email invitation to access the SharePoint site each time I run it.

I got the script from https://itgroove.net/mccalec/2016/01/19/sharepoint-online-invite-external-users-with-powershell/

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


[string[]]$users = "userToInvite@domain.com"
$Secure_String_Pwd = ConvertTo-SecureString "password" -AsPlainText -Force

# Establish Connection to SharePoint Online
$siteUrl = "https://tenant.sharepoint.com/sites/site1"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("user@domain.com", $Secure_String_Pwd)
$ctx.Credentials = $credentials
# Create request list
$userList = New-Object "System.Collections.Generic.List``1[Microsoft.SharePoint.Client.Sharing.UserRoleAssignment]"
# For each user, set role
# THIS ASSUMES YOU HAVE A POWERSHELL COLLECTION OF USERS YOU WANT TO INVITE
ForEach($user in $users)
{
$userRoleAssignment = New-Object Microsoft.SharePoint.Client.Sharing.UserRoleAssignment
$userRoleAssignment.UserId = $user
$userRoleAssignment.Role = [Microsoft.SharePoint.Client.Sharing.Role]::View
$userList.Add($userRoleAssignment)
}
try
{
# Send invites
$message = "Please accept this invite to our SharePoint site. Thanks!"
[Microsoft.SharePoint.Client.Sharing.WebSharingManager]::UpdateWebSharingInformation($ctx, $ctx.Web, $userList, $true, $message, $true, $true)
$ctx.ExecuteQuery()
}
catch
{
$hasError = ($error[0] | out-string)
}

For the C# version, I looked here https://blog.beecomedigital.com/2015/09/17/share-your-sharepoint-sites-with-external-users-using-csom/

using(var password = new SecureString())
{
  foreach(var c in "myPassword".ToCharArray())
    password.AppendChar(c);

  using(var ctx = new ClientContext("https://tenant.sharepoint.com/"))
  {
    ctx.Credentials = new SharePointOnlineCredentials("user@tenant.onmicrosoft.com", password);

    var users = new List<UserRoleAssignment>();
    users.Add(new UserRoleAssignment()
    {
      UserId = "username@externaldomain.com",
      Role = Role.View
    });

    WebSharingManager.UpdateWebSharingInformation(ctx, ctx.Web, users, true, null, true, true);
    ctx.ExecuteQuery();
  }
}

The C# version never sends an invitation. The PowerShell version sends it and it shows up in the user's mailbox a handful of seconds later.

WebSharingManager.UpdateWebSharingInformation is passed one user but returns nothing. Not sure if it is supposed to return anything on success though. (new to the CSOM)

The C# version doesn't throw any errors either.

Am I missing something? Why would the PowerShell version work, but not the C# version?

Tested the C# code in my side and it works as expected: enter image description here

enter image description here

You can add a IList object and trace the Status, see if it is true or if there is any exception.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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