3

Context : SharePoint Online, using CSOM C# or Powershell

I have a content search webpart (CSWP) on the homepage of a website. I need to edit the query of my CSWP programmatically.

Why programmatically ? Because I have the same WebPart on more than 80 websites and I can't take three days going on each and everyone of them...

So, I "only" need to find a way to edit the query of an existing CSWP without recreating it. If you have some ideas, anything could help !

Thank you.

1

I think the easier approach would be to delete and reprovision the web part in this case. You don't have a strongly typed representation of CSWP in CSOM, so there's no straightforward way to just update the query.

Technically, it might be possible to update the query via a Properties dictionary, but it'd still be easier to delete/reprovision.

1

If you want to go the road with the props Derek is mentioning you could do something like this:

var clientContext = new SP.ClientContext(siteRelUrl);
var file = clientContext.get_web().getFileByServerRelativeUrl( siteRelUrl + "/SitePages/Home.aspx");

var limitedWebPartManager = file.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var collWebPart = limitedWebPartManager.get_webParts();

clientContext.load(collWebPart, 'Include(WebPart.Properties, WebPart.Title)');

clientContext.executeQueryAsync(() =>  {

    if (collWebPart.get_count()) {

        var webPartEnumerator = collWebPart.getEnumerator();
        while (webPartEnumerator.moveNext()) {
            var webPartDef = webPartEnumerator.get_current();
            var webPart = webPartDef.get_webPart();                 
            console.log(webPart.get_title())
            if(webPart.get_title() == "Todo") {

                var webPartProperties = webPart.get_properties();

                // Take a look the props here, QueryTemplate maybe
                console.log( JSON.parse(webPartProperties.get_item("DataProviderJSON"))  )

                //webPartProperties.set_item("DataProviderJSON", newDataProviderJSONval);
                //webPartDef.saveWebPartChanges();  
            }
        }

        clientContext.executeQueryAsync(function() {            
            // Success
        }, (s,a) => { console.log(a.get_message()) });  
    }

}, (s,a) => { console.log(a.get_message()) });

I'm not sure of the props you'll need to change, never done it, only for other web parts.

  • Thanks for both of your answers ! I may give it a go, will keep you updated if I do ! (or if I find a way to update my WP) – M. Jouot Aug 31 '17 at 13:45
  • 1
    There is also a PnP PowerShell Command: Set-PnPWebPartProperty that makes updating individual properties super simple. – theChrisKent Aug 31 '17 at 18:40
  • @theChrisKent nice, that's probably better than using JavaScript indeed :) – Anders Aune Sep 1 '17 at 5:14

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.