2

How can I check if a webpart is of type XsltListViewWebPart in Sharepoint 2013 using JSOM

I'm trying to return a list XsltListViewWebParts on a page this is as far as I have gone

function get_webpart_info(callback) {            
        var pageFile = context.get_web().getFileByServerRelativeUrl(_spPageContextInfo.serverRequestPath);
        var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
        var webPartDefs = webPartManager.get_webParts();
        context.load(webPartDefs, 'Include(WebPart)');
        context.executeQueryAsync(
            function () {
                console.log('Number of webparts ' + webPartDefs.get_count());
                var wp_xslts = [];
                for (var i = 0; i < webPartDefs.get_count() ; i++) {
                    var webPartDef = webPartDefs.getItemAtIndex(i);
                    var webpart = webPartDef.get_webPart();
                    //I'm stuck here coz I don't know how to check  the type of webpart


                }
                return callback(wp_xslts);
            }, function onError(sender, args) {
                console.log(args.get_message());
                return callback(null);
            }
        );
    }
4
  • 1
    what code have you tried? Sep 3, 2015 at 8:52
  • I don't think this is possible via client script... i think i tried something like this via powershell once.
    – Nils
    Sep 3, 2015 at 12:45
  • 1
    This might be useful to you: sharepoint.stackexchange.com/questions/86157/…. Once you get the typename, you can check if it is XsltListViewWebPart.
    – Akhoy
    Sep 3, 2015 at 18:15
  • @Akhoy this also works. The authenticated user calling this method should have the necessary permissions to import webparts.
    – Mafura
    Sep 6, 2015 at 19:37

1 Answer 1

3

You can't explicitly check the type of the web part, but what you can do is load the properties and see if ListName is set. That should be enough to tell you if it's an XSLTListViewPart, and if you hit a conflict there are a bunch of other properties that might help you qualify it. Here's an example:

var ctx = new SP.ClientContext();
var pageFile = ctx.get_web().getFileByServerRelativeUrl(pageUrlGoesHere);
var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var webPartDefs = webPartManager.get_webParts();
ctx.load(webPartDefs,'Include(WebPart)');
ctx.executeQueryAsync(
  function () {
    for(var i = 0;i < webPartDefs.get_count();i++) {
       var webPartDef = webPartDefs.getItemAtIndex(i);
       var webPart = webPartDef.get_webPart();
ctx.load(webPart,'Properties');
ctx.executeQueryAsync(
  function () {
    var properties = webPart.get_properties();
    console.log(properties.get_fieldValues());
   var fieldValues = properties.get_fieldValues();
   if (fieldValues.ListName)
   {
        //Add to list, etc. May want to check more properties or change conditional.
   }
  },
  function(sender,args){
     console.log(args.get_message());
  });
    }
  },
  function(sender,args){
     console.log(args.get_message());
  });

Hope this helps!

4
  • 1
    This works for authenticated users with permissions to import webparts on the page. Otherwise no properties are available.
    – Mafura
    Sep 6, 2015 at 19:34
  • Hmm, good to know! For an approach that works with non-authenticated users, you could potentially check the Title URL of the web part - this is defined for list views but rarely for other types of webparts. Unfortunately, it may not be an exact listing.
    – Ted N.
    Sep 8, 2015 at 14:29
  • does this work in Add-in ?
    – codemirror
    Jul 27, 2017 at 14:03
  • What type of add-in, and what are you trying to get information about? SharePoint add-ins are typically not able to access any information about the page that contains them, so you wouldn't be able to use this to look at web parts on the same page as the Add-In. However, this code works in a standard Script Editor web part.
    – Ted N.
    Jul 28, 2017 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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