Search code examples
jquerysharepointeditorspservices

SP routine ClientContext fails except in Edit Mode


I have an issue with Javascript errors (Object Expected) in normal mode while it works in Edit mode. Its failing on the last line shown below.

 $(document).ready(function() { updateListItem(); });

      function updateListItem() {

        var siteUrl = ‘/sites2/sppwgrqy/DashboardTest/’; alert(‘now to get siteUrl’ + siteUrl );

        var clientContext = new SP.ClientContext(siteUrl ); //fails hereNo hidden components.

   }

works fine in Edit mode. I upgraded to jquery 1.8.3 and then 1.9.0 with no change.


Solution

  • Ok so honestly, I never found out why this approach only works in Edit mode. so I used a SPServices option:

    <script type="text/javascript">
    //Wrapping your script in $(document).ready(function()means 
    //that the calls will be made once the page is fully loaded,
    // i.e., the page is "ready". 
    
    $(document).ready(function() {
       updateReleaseSelected();
    });
    
    function updateReleaseSelected()
    {    
       var relID = document.getElementById('ReleaseID').value;
       //  alert('RelID:' +  relID );
       updateItems(relID );
    };
    
    function updateItems(relID)
    {  
      $().SPServices(
      {
          operation: 'UpdateListItems',
          webURL: '/sites2/sppwgrqy/DashboardTest/',
          listName: 'ReleaseSelected',
          updates: '<Batch OnError="Continue" PreCalc="True">' + 
             '<Method ID="1" Cmd="Update">' +
             '<Field Name="ReleaseValue">' + relID + '</Field>' +
             '<Field Name="ID">2</Field>' +
             '</Method>' +
             '</Batch>',
          completefunc: function(xData, Status)
          {
              //alert('successfully updated');      
          }
      });
    }
    </script>