none
Getting Intermittent exceptions when reading data from picture library using JSOM RRS feed

  • Question

  • I have script I'm developing for a page. When a person's name is selected from a dropdown list, the javascript/jquery code is then supposed to read and get an image file path and put it into a hidden field on the page. This part of the script only seems to be working on every other time that I select an entry from the dropdown list. Here's a small snippet of where its failing:

      for(var i = 0; i < items.get_count(); i++)
      {
       var item = items.getItemAtIndex(i);   
       var fields = item.get_fieldValues(); 
       var title = fields.Title.toString();
        
       if(myval == title)
       {
        picPath = fields.FileRef.toString();      <----------- Fails on this statement on every other selection
        picPath = prtcl + "//" + host + picPath;
        jq("#slctdMembrHidn").val(picPath).trigger('change');
        filRefFnd = true;
        
        break;
       }  
      }
     }

    The exception I'm getting looks like this:

    Method: 'captureCorrectPicturePath' - Exception: 'TypeError' - Unable to get property 'toString' of undefined or null reference

    I've tried stepping through this code in the IE debugger, but so far I've not found the problem.

    In case it might help here's a complete copy of my current code:


    <script type="text/javascript">

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        console.log("Initiating SP.ClientContext") });
    ExecuteOrDelayUntilScriptLoaded(processSelectedTeamMember, "sp.js");
    ExecuteOrDelayUntilScriptLoaded(UpdateSpecificWebPart, "sp.js");
    ExecuteOrDelayUntilScriptLoaded(RetrieveTeamMemberTitle, "sp.js");

    var jq = jQuery.noConflict();

    var picPath = "";
    var filRefFnd = false;
    var host = location.hostname;
    var prtcl = location.protocol;
    var imgVwrDfltImgPath = "";
    var webPrtTitles = new Array(5);
    var webPrtTitl = "";
    var qryStrTmMmbr;
    var qryStrName;
    var slctdTmMmbr;
    var slctdName;
    var slctdTitle;
    var diagFldName = "";

    //********************************************************* START OF UPDATE IMAGE VIEWER WEBPART PROCESS ******************************************************//

    function UpdateWebPartProperties(webPartDef, properties, imgPath)
    {
     var chngMade = false;
     var hiddnProp;
     var imgLinkProp;
     var hghtProp;
     var widthProp;
     
     try
     {
      // Properties List
      
      hiddnProp = properties.get_item("Hidden");
      imgLinkProp = properties.get_item("ImageLink");
      hghtProp = properties.get_item("Height");
      widthProp = properties.get_item("Width");
      
      // generic height webpart property
      if((typeof hghtProp != 'undefined') && (hghtProp != null))
      {
       properties.set_item("Height","300");
       
       chngMade = true;
      }
     
      // generic width webpart property
      if((typeof widthProp != 'undefined') && (widthProp != null))
      {
       properties.set_item("Width","200");
       
       chngMade = true;
      }
      
      // generic webpart property
      if((typeof hiddnProp != 'undefined') && (hiddnProp != null))
      {
       if(hiddnProp)
       {
        properties.set_item("Hidden",false);
       
        chngMade = true;
       }
      }
      
      // image viewer property  
      if((typeof imgLinkProp != 'undefined') && (imgLinkProp != null))
      {  
       properties.set_item("ImageLink",imgPath);
       
       chngMade = true;
      }
      
      
      return chngMade;
     }
     catch(excptn)
     {
      alert("METHOD: 'UpdateWebPartProperties' - Exception: "+ excptn.name + " - " + excptn.message);
        }
        finally
     {

        }
    }

    function UpdateSpecificWebPart(webPrtTitl)

     try
     {
      if((typeof webPrtTitl != 'undefined') && (webPrtTitl != null))
      {
       var imgPath = jq("#slctdMembrHidn").val();
       var serverRelativeUrl = _spPageContextInfo.serverRequestPath;
       var myCtx = new SP.ClientContext.get_current();
       var web = myCtx.get_web();
       var oFile = web.getFileByServerRelativeUrl(serverRelativeUrl);
       var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
       var webPrtCnt = 0;
       var webPrtsStr = "";
       
       this.collWebPart = limitedWebPartManager.get_webParts();
       
       myCtx.load(collWebPart, 'Include(WebPart.Properties)');
       
       myCtx.executeQueryAsync(Function.createDelegate(this, function(){VerifyWebPartTitleAndUpdate(myCtx, collWebPart, webPrtTitl,imgPath);}),
        function()
        {
         alert('WEBPARTS COLLLECTION QUERY FAILURE!');
        }
       );
      }
     }
     catch(excptn)
     {
      alert("METHOD: 'UpdateSpecificWebPart' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    function GetWebPartDefinition(collWebPart, properties, webPrtTitl)
    {
     var trgtWebPartDef;
     
     try
     {
      for (var i = 0; i < collWebPart.get_count(); i++)
      {
       var webPartDef = collWebPart.getItemAtIndex(i);
       var properties = webPartDef.get_webPart().get_properties();
       var webPartTitle = properties.get_item("Title");
           
       if(webPartTitle == webPrtTitl)
       {
        trgtWebPartDef = webPartDef;
        return trgtWebPartDef;
       }
      }
      
      throw new Exception("TARGET WEB PART NOT FOUND ON PAGE!");
     }
     catch(excptn)
     {
      alert("METHOD: 'GetWebPartDefinition' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    function VerifyWebPartTitleAndUpdate(myCtx, collWebPart, webPrtTitl, imgPath)
    {
     try
     {  
      if (collWebPart.get_count())
      {   
       var webPartDef = GetWebPartDefinition(collWebPart, properties, webPrtTitl);
       var properties = webPartDef.get_webPart().get_properties();
       
       myCtx.load(properties);
       myCtx.executeQueryAsync(Function.createDelegate(this, function(){UpdateTargetWebPart(myCtx, webPartDef, properties, imgPath, webPrtTitl);}),
       function()
       {
        alert('PROPERTIES QUERY FAILURE!');
       });
        }
      else
      {
       alert("NO WEB PARTS FOUND ON PAGE!");
      }
     }
     catch(excptn)
     {
      alert("METHOD: 'VerifyWebPartTitleAndUpdate' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    function UpdateTargetWebPart(myCtx, webPartDef, properties, imgPath, webPrtTitl)
    {
     try
     {
      webPartDef.openWebPart();
      //webPartDef.moveWebPartTo("Zone 2",3);
      
      var webPrtUpdtd = UpdateWebPartProperties(webPartDef, properties, imgPath);
         
      if(webPrtUpdtd)
      {
       webPartDef.saveWebPartChanges();
       webPartDef.closeWebPart();
       myCtx.load(webPartDef);
       myCtx.executeQueryAsync(
       function()
       {
        console.log("'"+webPrtTitl+"' WEB-PART PROPERTIES UPDATE SUCCESSFUL!");
       },
       function(sender, args)
       {
        alert("'" + webPrtTitl + "' WEB-PART PROPERTIES UPDATE FAILED!\nERROR INFO ( MESSAGE-" +args.get_message() + ", \nSTACK-" + args.get_stackTrace() + ")."); 
       });
      }
      else
      {
       webPartDef.closeWebPart();
       alert("NO CHANGES MADE TO '" + webPrtTitl + "' WEB-PART.");
      }
     }
     catch(excptn)
     {
      alert("METHOD: 'UpdateTargetWebPart' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    //********************************************************* END OF UPDATE IMAGE VIEWER WEBPART PROCESS ******************************************************//

    //********************************************************* START OF DROPDOWN SELECTED VALUE PROCESS ******************************************************//

    function getListData(myval)
    {
     try
     {
      var serverRelativeUrl=_spPageContextInfo.serverRequestPath;
      var myCntxt = new SP.ClientContext.get_current();
      var oWeb = myCntxt.get_web();
      var picLibName = "Picture Library";
      var list = oWeb.get_lists().getByTitle(picLibName);
      
      if(list)
      {
       var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
         
       myCntxt.load(items);
       myCntxt.executeQueryAsync(
        Function.createDelegate(this,function(){captureCorrectPicturePath(items,myval);}),
        function(sendera,args)
        {
           console.log(args.get_message());
        }
       );
      }
      else
      {
       alert("PICTURE LIBRAYR '"+picLibName.toString()+"' NOT FOUND!"); 
      }
     }
     catch(excptn)
     {
      alert("METHOD: 'getListData' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    function captureCorrectPicturePath(items,myval)
    {
     try
     {
      filRefFnd = false;
       
      for(var i = 0; i < items.get_count(); i++)
      {
       var item = items.getItemAtIndex(i);   
       var fields = item.get_fieldValues();
       diagFldName = "fieldsTitle";
       var title = fields.Title.toString();
        
       if(myval == title)
       {
        diagFldName = "fieldsFileRef";
        picPath = fields.FileRef.toString();
        picPath = prtcl + "//" + host + picPath;
        jq("#slctdMembrHidn").val(picPath).trigger('change');
        filRefFnd = true;
        
        break;
       }  
      }
     }
     catch(excptn)
     {
      console.log("METHOD: 'captureCorrectPicturePath' - Exception: '"+ excptn.name + "'  -  " + excptn.message + "DIAG-FIELD-NAME=("+diagFldName+")");
      alert("METHOD: 'captureCorrectPicturePath' - Exception: '"+ excptn.name + "'  -  " + excptn.message + "DIAG-FIELD-NAME=("+diagFldName+")");
      diagFldName = "";
        }
        finally
     {

        }
    }

    function processSelectedTeamMember(myval)
    {
     try
     {
         getListData(myval);
     }
     catch(excptn)
     {
      console.log("METHOD: 'processSelectedTeamMember' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    //********************************************************* END OF DROPDOWN SELECTED VALUE PROCESS ******************************************************//

    //********************************************************* START OF RETRIEVE TITLE OF SELECTED MEMBER PROCESS *****************************************************//

    function RetrieveTeamMemberTitle(myVal)
    {
     try
     {
      var serverRelativeUrl=_spPageContextInfo.serverRequestPath;
      var myCntxt = new SP.ClientContext.get_current();
      var oWeb = myCntxt.get_web();
      var srchList = "NetOps Team Member Titles";
      var list = oWeb.get_lists().getByTitle(srchList);
      
      if(list)
      {
       var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
        
       myCntxt.load(items);
       myCntxt.executeQueryAsync(
        Function.createDelegate(this,function(){GetSelectedTitle(items,myVal);}),
        function(sendera,args)
        {
           console.log(args.get_message());
        }
       );
      }
      else
      {
       alert("SEARCH LIST '"+srchList.toString()+"' NOT FOUND!"); 
      }
     }
     catch(excptn)
     {
      alert("METHOD: 'RetrieveTeamMemberTitle' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    function GetSelectedTitle(items,myval)
    {
     try
     {   
      if(items.get_count())
      {
       for(var i = 0; i < items.get_count(); i++)
       {
        var item = items.getItemAtIndex(i);   
        var fields = item.get_fieldValues();
        var srchName = fields.Search.toString();
         
        if(myval == srchName)
        {
         slctdTitle = fields.Title.toString();
         break;
        }  
       }
      }
      else
      {
       console.log("NO 'NetOps Team Member Titles' ITEMS WERE RETRIEVED!");
       alert("NO 'NetOps Team Member Titles' ITEMS WERE RETRIEVED!");
      }
     }
     catch(excptn)
     {
      console.log("METHOD: 'GetSelectedTitle' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
      alert("METHOD: 'GetSelectedTitle' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    //********************************************************* END OF RETRIEVE TITLE OF SELECTED MEMBER PROCESS *****************************************************//

    //************************************************************** START OF PAGE REFRESH PROCESS ************************************************************//

    function refreshPage(slctdTmMmbr,slctdName,slctdTitle)
    {
     var rawUrl = window.location.toString(); 
     var slctdTmMmbrFnd = false;
     var slctdNameFnd = false;
     var slctdTitleFnd = false;
     
     try
     {
      if(rawUrl.indexOf('?') > -1)
      {
       rawUrl = rawUrl.substring(0,rawUrl.indexOf('?'));
      } 
      
      if((typeof slctdTmMmbr != 'undefined') && (slctdTmMmbr != null) && (slctdTmMmbr.length > 0))
      {
       slctdTmMmbrFnd = true;
      }
      
      if((typeof slctdTitle != 'undefined') && (slctdTitle != null) && (slctdTitle.length > 0))
      {
       slctdTitleFnd = true;
      }
      
      if((typeof slctdName != 'undefined') && (slctdName != null) && (slctdName.length > 0))
      {
       slctdNameFnd = true;
       
       if(slctdTmMmbrFnd)
       {
        if(slctdTitleFnd)
        {
         rawUrl = rawUrl + "?slctdTmMmbr="+slctdTmMmbr+"&amp;slctdName="+slctdName+"&amp;slctdTitle="+slctdTitle;
        }
        else
        {
         rawUrl = rawUrl + "?slctdTmMmbr="+slctdTmMmbr+"&amp;slctdName="+slctdName;
        }
       }
       else
       {
        if(slctdTitleFnd)
        {
         rawUrl = rawUrl + "?slctdName="+slctdName+"&amp;slctdTitle="+slctdTitle;
        }
        else
        {
         rawUrl = rawUrl + "?slctdName="+slctdName;
        }
       }
      }
      else
      {
       console.log("THE 'slctdName' DATA IS MISSING. MORE LOGIC NEEDED IN THE 'refreshPage' METHOD.");
      }
      
      window.location = rawUrl;
     }
     catch(excptn)
     {
      console.log("METHOD: 'refreshPage' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
      alert("METHOD: 'refreshPage' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    //************************************************************** END OF PAGE REFRESH PROCESS ************************************************************//

    //********************************************************* START OF QUERY STRING PARAMETER PROCESS *****************************************************//

    function processQueryStringParms()
    {
     var qryVarsInfo = "";
     var qryStr = document.URL.split('?')[1];
     var qryStrVars;
     var slctdName = "";
     var slctdTmMmbr = "";
     var slctdTitle = "";
     
     try
     {
      if((typeof qryStr != 'undefined') && (qryStr != null) && (qryStr.length > 2))
      {
       var qryStrVars = qryStr.split('&');
       
       for(indx in qryStrVars)
       {
        var qryVarStr = qryStrVars[indx];
        
        var myKey = "";
        var myKeyValue = "";
        var qryVar = qryVarStr.split('=');
        
        myKey = qryVar[0]; 
        myKeyValue = qryVar[1]; 
        
        if(myKey == "slctdTmMmbr")
        {
         slctdTmMmbr = myKeyValue.toString();
        }
        else if(myKey == "slctdName")
        {
         slctdName = myKeyValue.toString();
        }
        else if(myKey == "slctdTitle")
        {
         slctdTitle = myKeyValue.toString();
        }
        else
        {
         alert("UNKNOWN PARAMETER FOUND IN URL!");
        }
       } 
      }
      else
      {
       //alert("THE QUERY-STRING IS 'undefined', null, or TOO SHORT TO BE VALID!");
      }
      
      if(slctdName)
      {
       jq("#mmbrName").text(slctdName);
      }
      else
      {
       console.log("METHOD: 'processQueryStringParms' - THE 'slctdName' PARAMETER NOT FOUND!");
      }
       
      if(slctdTmMmbr)
      {
       jq("#slctdTeamMember").val(slctdTmMmbr);
      }
      else
      {
       console.log("METHOD: 'processQueryStringParms' - THE 'slctdTmMmbr' PARAMETER NOT FOUND!");
      }
      
      if(slctdTitle)
      {
       jq("#mmbrTitle").text(slctdTitle);
      }
      else
      {
       console.log("METHOD: 'processQueryStringParms' - THE 'slctdTitle' PARAMETER NOT FOUND!");
      }
     }
     catch(excptn)
     {
      console.log("METHOD: 'refreshPage' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
      alert("METHOD: 'processQueryStringParms' - Exception: '"+ excptn.name + "'  -  " + excptn.message);
        }
        finally
     {

        }
    }

    //********************************************************* END OF QUERY STRING PARAMETER PROCESS *****************************************************//

    //********************************************************* START OF JQAVASCRIPT/JQUERY MAINLINE PROCESS *****************************************************//

    jq(document).ready(function()

        jq("#slctdTeamMember").on("change", function(event)
        {
      slctdTmMmbr = jq("#slctdTeamMember").val().toString();
           
      if(slctdTmMmbr != " ")
            {   
                processSelectedTeamMember(slctdTmMmbr);
            }
      else
      {
       slctdName = "";
       slctdTmMmbr = "";
       slctdTitle = "";
       jq("#mmbrName").text("");
       jq("#mmbrTitle").text("");
       jq("#slctdMembrHidn").val(imgVwrDfltImgPath).trigger('change');
      }
      
      jq("#slctdMembrHidn").bind('change', function(event)
      {
       webPrtTitles[0] = "NetOps Team Member Image";
     
       webPrtTitl = webPrtTitles[0].toString();
      
          UpdateSpecificWebPart(webPrtTitl);
      
       // format name title
       slctdName = jq("#slctdTeamMember").find('option:selected').attr("name");
       jq("#mmbrName").text(slctdName);
       
       RetrieveTeamMemberTitle(slctdTmMmbr);
       
       jq("#mmbrTitle").text(slctdTitle.toString());
       
       setTimeout(refreshPage(slctdTmMmbr,slctdName,slctdTitle), 1500);
      });
        });
     
     processQueryStringParms();
    });

    </script>

    Thanks for the help.

    Henry

      
    Thursday, March 16, 2017 9:46 PM

Answers

  • Hi Henry,

    Before stepping into this statement each time in the loop:

    picPath = fields.FileRef.toString();     

    Watch the value of “fields.FileRef” and “fields”, what values in the two objects?

    Moreover, I notice that, in your script, there seems two entrances from where the script begins running:

    1,

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {

         console.log("Initiating SP.ClientContext") });

     ExecuteOrDelayUntilScriptLoaded(processSelectedTeamMember, "sp.js");

     ExecuteOrDelayUntilScriptLoaded(UpdateSpecificWebPart, "sp.js");

     ExecuteOrDelayUntilScriptLoaded(RetrieveTeamMemberTitle, "sp.js");

    2,

    jq(document).ready(function()

    Per my experience, it might easily lead to some unknown status due to the running sequence of different snippets, how about put all your functions into the document.ready in a specific order? It should make you easier to master the execution of the whole script.

    Best regards,

    Patrick

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

    Saturday, March 18, 2017 2:43 AM
    Moderator