How to access or read the properties collection and individual properties of a web part RRS feed

  • Question

  • I have had some minor success using some code I got from another member of this forum(Dennis), but now I think I'm stuck, because I'm not sure if I understand completely when I need to use the ctx.load, and ctx.exeutequeryAsync. In my code below I have been able to I think, get into an individual web part. Now however when I've tried to pull individual properties out of he properties collection I'm not able to, when I try to access them directly. So at this point I'm not sure if I can do it directly with the webpart(s) I seem to now be able to access, or if I first need to access the entire properties collection, and/or individual properties values using ctx.Load and ctx.executeQueryAsync again to get them. If someone could please clarify how to know when to use these two ctx methods I would very much appreciate it.. Here's my current code:

    <script type="text/ecmascript">
    ExecuteOrDelayUntilScriptLoaded(RetrieveWebParts, "sp.js");
    ExecuteOrDelayUntilScriptLoaded(RetrieveWebParts, "sp.runtime.js");

    var serverRelativeUrl=_spPageContextInfo.serverRequestPath;
    ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var oFile = web.getFileByServerRelativeUrl(serverRelativeUrl);
    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);

    this.collWebPart = limitedWebPartManager.get_webParts();


    ctx.executeQueryAsync(onSuccess, onFailure);
    function onSuccess() 
    //alert('Web Part Count: ' + collWebPart.get_count());

    var collWebPartsEnum = collWebPart.getEnumerator();
    var propsCnt = 0;
    var propertiesCnt = 0;
    var size = 0;

    var webPartDef = collWebPartsEnum.get_current();

    if(webPartDef != null)
    var webPart = webPartDef.get_webPart();

    if(webPart != null)
    //if(propertiesCnt < 1)
    properties =  webPart.get_properties();

    ctx.executeQueryAsync(onPropertiesQuerySuccess, onPropertiesQueryFailure);

    //propertiesCnt = propertiesCnt + 1;

    var hidden = properties.get_item('Hidden');
    var title = properties.get_item('Title');

    alert("CURRENT WEB-PART TITLE IS '"+title.toString()+"', ITS HIDDEN FIELD VALUE IS '"+hidden.toString()+"'.");

    function onFailure() 

    Thanks for the help.


    Monday, March 6, 2017 2:47 AM


  • Hi Henry,

    If want to loop the web part collection and then get some properties of a specific web part, we can use the code snippet below:

    <script type="text/ecmascript">
    ExecuteOrDelayUntilScriptLoaded(RetrieveWebParts, "sp.js");
    function RetrieveWebParts(){		 
    var serverRelativeUrl=_spPageContextInfo.serverRequestPath;
    var ctx = new SP.ClientContext.get_current();
    var pageFile = ctx.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
    var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    var webPartDefs = webPartManager.get_webParts();
      function () {
        for(var i = 0;i < webPartDefs.get_count();i++) {
           var webPartDef = webPartDefs.getItemAtIndex(i);
           var webPart = webPartDef.get_webPart();


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Tuesday, March 7, 2017 8:32 AM