SharePoint OnPrem – Sept 2020 Safe Control Builder

After the September 2020 Security update was released, we have seen many people (including myself) writing the Safe Controls incorrectly. I still reference and read KB4572409 for advice on how to find the Type, Namespace, and Assembly strong name.

I decided to simplify the process. This PowerShell script creates a function Build-SafeControlFromEntry. This builds the Safe Control from the error in the ULS log. Look for EventID 9tmwc

Copy this over to $ULS and pipe it into the function as seen below:

If we’re unable to build the safe controls this message will appear:

PowerShell below:

##Build-SafeControlFromEntry
##Author:adamsor; adamsorenson.com
##Version: 1.0

###Example###
##$ULS = "The type Contoso.Customizations.WebPartControl, Contoso.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f8f11d50a3a could not be found or it is not registered as safe."
##$ULS | Build-SafeControlFromEntry


Function Build-SafeControlFromEntry
{
    param([Parameter(ValueFromPipeline=$true)]$string)
    
    #This code calls to a Microsoft web endpoint to track how often it is used. 
    #No data is sent on this call other than the application identifier
    Add-Type -AssemblyName System.Net.Http
    $client = New-Object -TypeName System.Net.Http.Httpclient
    $cont = New-Object -TypeName System.Net.Http.StringContent($null,[system.text.encoding]::UTF8,"application/json")
    $tsk = $client.PostAsync("https://msapptracker.azurewebsites.net/api/Hits/4b89c98c-473b-4fe3-8f8e-5552f13b0e30",$cont)
    #if you want to make sure the call completes, add this to the end of your code
    #$tsk.Wait()
    
    $wcSafeControl = $null
    $typeSafeControl = $null
    
    [int]$indexc = $string.IndexOf('The type')
    $indexTc = $indexc + 9
    $string2 = $string.Substring($indexTc)
    [int]$indexend = $string2.IndexOf("could not be found or it is not registered as safe.")
    $string3 = $string2.Substring(0, $indexend)
    $fcoma = $string3.IndexOf(",")
    $nt = $string3.Substring(0,$fcoma)

    $ASN = ($string3.Substring($fcoma+2)).Trim()

    $type = $nt.Substring($nt.LastIndexOf(".")+1)
    $namespace = $nt.Substring(0, $nt.LastIndexOf("."))
    
    If($ASN -ne $null -and $namespace -ne $null -and $type -ne $null)
    {
        $wcSafeControl = '<SafeControl Assembly="'+$ASN+'" Namespace="'+$namespace+'" TypeName="*" Safe="True" />'
        $typeSafeControl = '<SafeControl Assembly="'+$ASN+'" Namespace="'+$namespace+'" TypeName="'+$type+'" Safe="True" />'
        Write-host "SafeControls build successfully.  Use either Wildcard or Explicit in all of the web.configs from this web application" -ForegroundColor Green

        Write-host "Wildcard SafeControl" -ForegroundColor Yellow
        $wcSafeControl

        Write-Host "Explicit type SafeControl" -ForegroundColor Yellow
        $typeSafeControl
    }
    Else
    {
        Write-Host "Unable to find safe control from ULS entry.  Please look for 9tmwc for event ID.  Check KB" -ForegroundColor Red
        Write-Host "Example:" -ForegroundColor Red
        write-host "The type Contoso.Customizations.WebPartControl, Contoso.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f8f11d50a3a could not be found or it is not registered as safe."
    }
    
}

SharePoint – Security Trimmed list of sites

I was recently asked about getting a list of site collections that the users would have permissions to (security trimmed). The first thought was utilizing search since we would want to security trim the list.

1. Edit the Modern page

2. Add in the Highlighted Content Web part

3. Edit the Web Part

4. This will show many options on the right side of the page. We want to leave it on Filter. Next, we want to select Selected sites or All sites. Depending on if you would like to consider all the site collections in your tenant.

5. Change the Type from Documents to Pages.

6. The Filter selection needs to be changed to Managed property. More on managed properties here: https://docs.microsoft.com/en-us/sharepoint/manage-search-schema

7. In the Find a managed property, enter in content. Then select ContentClass, Equals, STS_Site. We could look for Subsites within site collection(s) by using STS_Web.

8. After this is done, you can see the web part update in real time. This will show the sites (items) only if that user has permission.

SharePoint Online – Unable to Change Site Logo

I was assisting with an issue where changing the Site Logo (Gears -> Site Information) would fail.

We would see “We experienced a problem updating the icon. Please try again in a few minutes.” Or “Check to see if group property HiddenFromAddressListsEnabled is set to true, or if Address Book Policies are set on the group. Otherwise please try again later.”

The site was based off a O365 group and Team. Anytime the site logo is changed, this would make a call to Exchange to change the logo of the group.

The O365 Group had HiddenFromExchangeClientsEnabled set to True. This would make these calls fail and revert the change.

We used the PowerShell below to change the property of the group to False to resolve the issue.

$displayName = “Display name of group” 
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
 
#Get all groups then filter them based on displayname
$groups = Get-UnifiedGroup
$group = $groups | ? {$_.displayname -eq $displayName}

#Display Current value
$group.HiddenFromExchangeClientsEnabled

#changing the value for the group to False. 
Set-UnifiedGroup -Identity $group -HiddenFromExchangeClientsEnabled:$false

SharePoint – Hybrid Following – 401

SharePoint 2019, 2016, and 2013 all support hybrid following. If you’re running into a few issues with getting Hybrid Following working

These articles go into detail on how to setup it up:

 

With hybrid following we have the My Sites in the cloud while having content with On-Prem. This sometimes doesn’t work.

 

 

A few things to check

In the ULS logs we’re likely to see something about unable to follow the item

04/15/2020 15:23:45.55    w3wp.exe (0x4C220)    0x501F4    SharePoint Portal Server    Content Following    afilq    Unexpected    FollowedContent.FollowItem:Exception:System.Net.WebException: The remote server returned an error: (401) Unauthorized.

at System.Net.HttpWebRequest.GetResponse()

at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()

at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)

at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

at Microsoft.Office.Server.UserProfiles.FollowedContentProxy.Execute(String methodName)

at Microsoft.Office.Server.UserProfiles.FollowedContentProxy.FollowItem(FollowedItem item)

at Microsoft.Office.Server.UserProfiles.FollowedContent.DoHybridFollow(String scopeName, FollowedItem item)

at Microsoft.Office.Server.UserProfiles.FollowedContent.FollowItem(FollowedItem item, Boolean isInternal)

 

This means the web application made a call to your tenant to follow this item but failed authentication.

  1. Server to Server authentication is not working/configured. The Hybrid Wizard will configure this. Make sure to select Hybrid Sites. See https://docs.microsoft.com/en-us/SharePoint/hybrid/configure-server-to-server-authentication?redirectedfrom=MSDN
  2. The Azure Access Control Service Application Proxy is not associated with the web application.

 

Check the Service Application Associations in Central Administration, Application Management

Remember that the WFEs will need access to the internet to make these calls.

SharePoint – Scheduling the Profile Picture Import Script

One of the most popular script on this blog is the Profile Picture Import. I’ve helped a few people configure this to running daily or weekly depending on how many frequently you would like the profiles to be updated from Active Directory.

Identify the service account

The easiest way to have this script to run is by the Sync account. We can find this in Configure Synchronization Connections your User Profile Service Application. We will need to give account additional permissions to the farm to allow it run SharePoint commandlets.

This account will need Admin Shell access. We will need to enter in the My Site Host site collection URL to give service account permission to run Powershell.

$site = get-spsite "http://mysitehostURL"
Add-SPShellAdmin -UserName contoso\spsync -database $site.ContentDatabase

Adding permission to the UPA

The account running this script will also need access to the UPA. This access will allow the account to run PowerShell without getting Access Denied failures. The easiest way to add the permissions is through Central Admin.

Application Management à Manage Service Applications

Click on the line for the UPA(not the blue link). This will select this line and the ribbon will light up. Then click on the Permissions button in the ribbon.


Type in the User account and click Add. Then click on Full Control and OK.


Setting up Task Scheduler

  1. Open Task Scheduler

  2. Click on Create Task

  3. Change to Run whether user is logged on or not. Click on Change User or Group. Enter in the service account from above.

  4. Select the Triggers tab. Click New. Select Daily or Weekly depending on how often you would like it to run. Click OK.

  5. Click on Actions and the New button. We will want to add “-ExecutionPolicy Bypass -File c:\ProfilePicture\PictureUpload1.3.ps1” to the arguments and just “PowerShell.exe” to the Program/Script. Click OK twice to create the task.

  6. Right Click on the task and select Run. Then check the log file status of the test run.

After the test run works, the scheduled runs should run without issue. The log file will provide a good reference of failures or successes. Just a reminder, any account running SharePoint commandlets(Powershell), they need Shell access.