Monthly Archives: September 2015

Share your SharePoint sites with external users using CSOM

Yesterday on the Office 365 Yammer Network, someone asked if it was possible to add external users to a group using CSOM.

After a quick research, I found that it was a recurrent question and there’s no clear answer to that question. So I decided to investigate and I found the answer 🙂

In CSOM, there’s a namespace called Microsoft.SharePoint.Client.Sharing in which there are two useful classes called WebSharingManager and DocumentSharingManager.

These two classes are used to share a site or a document with users (internal or external).

In my case, I want to share a site with an external user so I need to use WebSharingManager.

This class has a method called UpdateWebSharingInformation with the following signature :

public static IList<UserSharingResult> UpdateWebSharingInformation(
  ClientRuntimeContext context,
  Web web,
  IList<UserRoleAssignment> userRoleAssignments,
  bool sendServerManagedNotification,
  string customMessage,
  bool additivePermission,
  bool allowExternalSharing

As you can see, there are many parameters which are very easy to understand but the important parts are userRoleAssignments and allowExternalSharing.

The first is a collection of users with whom you want to share the resource. The second is to enable external sharing if the users are not employed by your company.

How to use it in practice ? It’s very easy.

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

  using(var ctx = new ClientContext(""))
    ctx.Credentials = new SharePointOnlineCredentials("", password);

    var users = new List<UserRoleAssignment>();
    users.Add(new UserRoleAssignment()
      UserId = "",
      Role = Role.View

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

When you execute the code above, the external user is invited as a viewer in the site associated to the CSOM context. He will receive an email to access to the site.

If you want to know if the invitation was sent successfully and if the user has accepted (or not), you just have to go to Access requests and invitations through the site settings.

External Sharing / Pending Requests

If you want more information about WebSharingManager and DocumentSharingManager, just go to the official documentation on MSDN.

How to know what file types are supported to generate previews on Office 365 ?

In our previous articles (here and here) we have seen how to generate a preview for a document stored in SharePoint / Office 365.

When you develop applications around Office 365 and because you can store potentially (there are some exceptions) any types of documents in a SharePoint site, it could be interested to know what file types are supported to generate a preview for those documents.

There’s no official documentation on how to achieve this operation but you have to know that it’s possible using the same handler as that described in our previous articles (GetPreview.ashx).

Indeed, that handler has some hidden features and one of them is the ability to get supported file types. To do that, you just have to call the following URL (don’t forget to pass a valid authentication token in HTTP headers, as you do with REST APIs) :


When you call that URL, the server return a JSON response such as the following :


You get an array of document types and for each item, you are able to know :

  • FileExtension => Does it really need to be explained ? 😉
  • Always => Does the handler is always able to generate a preview or can it fail (e.g. because the file contains unsupported features)
  • MultiPreview => Does the handler is able to generate multiple previews for the same document (e.g. slides on PowerPoint presentations)

As you can see, there’s only a few document types that are supported by Office 365 to generate previews. Now you can improve your applications to not try to generate previews for unsupported types 😉