Wednesday, April 13, 2022

Resolve “Everyone except external users” group using PnP.PowerShell

In my previous posts I showed several ways to resolve special group in Sharepoint Online "Everyone except external users" which represents all users in organization except external users:

In this post I will show how to do that with PnP.PowerShell. Simplest way which will work on most tenants is the following:

$authRealm = Get-PnPAuthenticationRealm
$everyOneExceptExternals = Get-PnPUser -Id "c:0-.f|rolemanager|spo-grid-all-users/$authRealm"

But on some tenants (e.g. old tenants) it may not work because this special group was created with different naming convention there (see link above). For such tenants we may use the following additional step:

if (!$everyOneExceptExternals) {
	$everyOneExceptExternals = Get-PnPUser | Where-Object { $_.LoginName.StartsWith("c:0-.f|rolemanager|spo-grid-all-users/") }
}

Here we try to find user which login name starts with special "c:0-.f|rolemanager|spo-grid-all-users/" prefix. This prefix is used in login name of "Everyone except external users" group. With this approach you may resolve this special group both on new and old tenants. Hope it will help someone.

Friday, April 8, 2022

Create folders with special characters in Sharepoint Online programmatically via CSOM

If you worked with SP on-prem you probably know that some special characters are not allowed in folders names there. In Sharepoint Online however it is possible to use some special characters in folders names:

How to create such folders with special characters programmatically via CSOM? If we will try to do it using the same approach which we used for SP on-prem it will implicitly remove all parts in folder names which come after special characters i.e. in above example abc, def, ghi:

public static ListItem AddFolder(ClientContext ctx, List list, string parentFolderUrl, string folderName)
{
    var lici = new ListItemCreationInformation
    {
        UnderlyingObjectType = FileSystemObjectType.Folder,
        LeafName = folderName.Trim(),
        FolderUrl = parentFolderUrl
    };

    var folder = list.AddItem(lici);
    folder["Title"] = lici.LeafName;
    folder.Update();
    ctx.ExecuteQueryRetry();

    return folder;
}

If we want to create folders with special characters we need to use another CSOM method Folder.AddSubFolderUsingPath:

public static void AddFolderUsingPath(ClientContext ctx, Folder parentFolder, string folderName)
{
    parentFolder.AddSubFolderUsingPath(ResourcePath.FromDecodedUrl(folderName));
    ctx.ExecuteQueryRetry();
}

With this method folders with special characters will be created successfully in Sharepoint Online.