0

how do I set audience targeting for webparts in share point online using CSOM/JSOM

Any help is appreciated

  • If you think the answer helps you, "accept" this answer as the best answer. – JoannaW_MSFT Jul 10 '18 at 1:16
0

You can refer to the following JSOM to change target audience for the web part on the page.

This demo is used to get all web parts from the page and change target audience for the first web part by setting WebPart.AuthorizationFilter Property. If you want to change all web parts on the page, you can iterate web parts from web part collection.

<script language="javascript" type="text/javascript"> 

var siteUrl = '/sites/<site>';

var serverRelativeUrl = '/sites/<site>/<page location>/<page>.aspx';   

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updateWebPart);

function updateWebPart() {

    this.clientContext = new SP.ClientContext(siteUrl);

    var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);

    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);

    this.collWebPart = limitedWebPartManager.get_webParts();

    clientContext.load(collWebPart);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.changeAuthorization), Function.createDelegate(this, this.onQueryFailed));

} 

function changeAuthorization() {

    if (!collWebPart.get_count()) {

        alert('No Web Parts on this page.');

    }

    // change the first web part on the page

    var oWebPartDefinition = collWebPart.get_item(0);

    this.oWebPart = oWebPartDefinition.get_webPart();

    oWebPart["AuthorizationFilter"] = "<SharePoint Group>"

    oWebPartDefinition.saveWebPartChanges();

    clientContext.load(oWebPart, 'TitleUrl');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded() {

    alert('Authorization changed for Web Part: ' + this.oWebPart.get_titleUrl());

    alert(oWebPart["AuthorizationFilter"]);

}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}   


</script>​

How to: Work with Web Parts on a Page Using JavaScript

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.