none
Getting errors in code that seems to work sometimes, as well as code in sp.runtime.js RRS feed

  • Question

  • I'm getting two errors in IE developer tools when trying to debug. The first error is:

    SCRIPT5007: Unable to get property 'get_current' of undefined or null reference

    and refers to this line in my code:

    var ctx = new SP.ClientContext.get_current();

    and getting this error:

    SCRIPT5022: The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

    for this code reference:  sp.runtime.js (2, 31624)

    which is not even in my code.

    I'm not sure why.

    Here's a listing of the existing code I have which is getting these errors:

    <script type="text/ecmascript">

    ExecuteOrDelayUntilScriptLoaded(RetrieveWebParts, "sp.js");
    ExecuteOrDelayUntilScriptLoaded(RetrieveWebParts, "sp.runtime.js");

    function processWebPartPropertyData(webPrtArr,webPrtTitl)
    {
     var webPrtsInfo = "";
     //var webPrtTitl = "";
     var webPrtTitlPrts;
     
     for(var indx=0; indx < webPrtArr.length; indx++)
     { 
      var webPrtProps = webPrtArr[indx].split(",");
      
      for(var jndx=0; jndx < webPrtProps.length; jndx++)
      {
       var webPrtProp = webPrtProps[jndx];
       
       webPrtTitlPrts = webPrtProp.split(":");

       webPrtsInfo = webPrtsInfo + webPrtTitlPrts[0].toString() + webPrtTitlPrts[1].toString()
       
       if((webPrtProps.length - jndx) > 1)
       {
        webPrtsInfo = webPrtsInfo + ",\n";
       }
      }
      
      alert("THE '" + webPrtTitl.toString() + "' WEB-PART HAS THE FOLLOWING PROPERTIES \n("+webPrtsInfo.toString()+").");
      
      webPrtsInfo = "";
      webPrtTitlPrts = [];
     }
    }


    function RetrieveWebParts()
    {  
     var serverRelativeUrl=_spPageContextInfo.serverRequestPath;
     var ctx = new SP.ClientContext.get_current();
     var web = ctx.get_web();
     var oFile = web.getFileByServerRelativeUrl(serverRelativeUrl);
     var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
     var webPrtCnt = 0;
     var webPrtArr;
     
     this.collWebPart = limitedWebPartManager.get_webParts();
     
     ctx.load(collWebPart, 'Include(WebPart.Properties)');
     
     ctx.executeQueryAsync(
      function()
      {
        webPrtArr = new Array(collWebPart.get_count());
       
        if (collWebPart.get_count())
        {
               for (var i = 0; i < collWebPart.get_count(); i++)
        {
         var webPartDef = collWebPart.getItemAtIndex(i);
                        var webPart = webPartDef.get_webPart();
                        var webPrtTitl = webPart.get_title();
                  //var webPartDef = collWebPart.getItemAtIndex(i);
                  //var webPart = webPartDef.get_webPart();
                  var properties = webPart.get_properties();
         
         //saves all properties of individual webpart as a collection in JSON/String format into an array entry
                  webPrtArr[i] = JSON.stringify(properties.get_fieldValues());
        }
        
        processWebPartPropertyData(webPrtArr,webPrtTitl);
        }
           else
        {
               alert("No web parts found.");
        }
      },
      function()
      {
       alert('Failure!');
      }
     );
    }

    RetrieveWebParts();

    </script>

    I would appreciate help in understanding why I'm getting the errors and how to resolve them.

    Thanks.

    Henry

     

     

    Tuesday, March 7, 2017 6:11 PM

All replies

  • Here is the link below

    http://sharepoint.stackexchange.com/questions/130512/uncaught-error-the-property-or-field-has-not-been-initialized-i-have-tried-man

    http://sharepoint.stackexchange.com/questions/130512/uncaught-error-the-property-or-field-has-not-been-initialized-i-have-tried-man


    Please remember to click Mark as Answer on the answer if it helps you

    Tuesday, March 7, 2017 7:51 PM
  • Hi Henry,

    This error usually happened when the property or field not loaded in ClientContext, please check if there is some operation which will read list and load the title field like below:

    context.load(collListItems,'Include(Title,Value)');

    Here is a similiar thread for your reference:

    Uncaught Error: The property or field has not been initialized

    Thanks

    Best Regards


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

    Wednesday, March 8, 2017 9:05 AM
  • Jerry, In this code snippet below my first async query has an include statement, but the second query does not have an include statement in it in the success handler for the first query. could this be the problem and if so should the second one also have the same include as the first one, since it just updates the webpart after i did a couple of property.set_item calls and and a webPartdef.saveWebPart() call?

    Here's the code:


    ctx.load(collWebPart, 'Include(WebPart.Properties)');
    ctx.executeQueryAsync(
    function() 
    {
    if (collWebPart.get_count()) 
    {
            for (var i = 0; i < collWebPart.get_count(); i++) 
    {
                var webPartDef = collWebPart.getItemAtIndex(i);
                var webPart = webPartDef.get_webPart();
                var properties = webPart.get_properties();

    //saves all properties of individual webpart as JSON/String format  
    webPrtsStr = JSON.stringify(properties.get_fieldValues()); 

    var webPrtInfo = verifyWebPart(webPrtsStr,webPrtSrchTitl);
    var rtnTitl = webPrtInfo[0];
    var webPrtFnd = webPrtInfo[1];

    if(webPrtFnd)
    {
    changeAndSaveWebPartProperties(webPartDef, properties);

    ctx.load(collWebPart);
    ctx.executeQueryAsync(function(){......

    Let me know when you can.

    Thanks.

    Henry

      
    Wednesday, March 8, 2017 5:24 PM