2

This issue is with SharePoint Online.

I have the following PowerShell function to update the audience on a Content Editor Web Part. When I run the function, the audience is set correctly, however it doesn't seem to take effect on the page until I edit the page, edit the web part (making no changes), then click OK on the Edit box.

The function adds two SharePoint groups to the Audience of the Content Editor Web Part.

function UpdateAudience {

    Param(

        [Parameter(Mandatory=$true)][string]$SiteUrl,
        [Parameter(Mandatory=$true)][string]$AKPA,
        [Parameter(Mandatory=$true)]$SPOCredentials

    )

    $PageUrl = "$SiteUrl/SitePages/pagename.aspx"

    $ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $ClientContext.Credentials = $SPOCredentials
    $ClientContext.ExecuteQuery()

    $page = $ClientContext.Web.GetFileByUrl($PageUrl)
    $ClientContext.Load($page)
    $ClientContext.ExecuteQuery()

    $webpartmanager = $page.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared)

    $ClientContext.Load($webpartmanager)

    $webParts = $webpartmanager.WebParts
    $ClientContext.Load($webParts)
    $ClientContext.ExecuteQuery()

    # Note there are two CEWP on this page, one hidden, one not. The one to change is the non-hidden one, the below loop identifies this web part ID

    foreach($webpart in $webparts){
        $ClientContext.Load($webpart.WebPart.Properties)
        $ClientContext.ExecuteQuery()
        $propValues = $webpart.WebPart.Properties.FieldValues
        if($propvalues.Title -eq "Content Editor" -and $propValues.Hidden -eq $false){
            $webPartID = $webpart.ID
        }
    }


    $webpart = $webParts | Where-Object{$_.ID -eq $webPartID}

    $webpart.WebPart.Properties["AuthorizationFilter"] = "$AKPA Group 1;;$AKPA Group 2"

    $WebPart.SaveWebPartChanges()
    $ClientContext.Load($webpart)
    $ClientContext.ExecuteQuery()

}

It doesn't appear to be an audience compilation issue - it doesn't matter if I add my account to one of the groups before or after the CEWP is manually edited, it still won't appear on the screen until I edit page > edit web part > OK

Is there a way to force this webpart to update?

Thanks

1

I got to the bottom of this. It was a syntax error in the string used for AuthorizationFilter.

The following should be used instead:

$groups = "$AKPA Group 1, $AKPA Group 2"

$webpart.WebPart.Properties["AuthorizationFilter"] = ";;;;$groups"

$WebPart.SaveWebPartChanges()
$ClientContext.Load($webpart)
$ClientContext.ExecuteQuery()

AuthorizationFilter will take GUIDs, LDAP DNs or SharePoint groups. All three can be specified in one string, delimited with a double semi-colon (;;). First are Audience IDs, second are DNs, and third are SharePoint Groups.

I believe what was happening was the system was adding my two groups as a Audience ID and a DN. These obviously didn't work, but the text was still in the AuthorizationFilter. Going into the GUI and clicking OK caused them to be re-parsed, and recognised as SharePoint groups.

Note for multiple groups, they must be put into a string variable, separated by a comma (,).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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