none
JavaScript runtime error: 'clientContext' is undefined

    Question

  •  Hi ,

    i try to get a List item with JS but i have this error : JavaScript runtime error: 'clientContext' is undefined

    script  type="text/javascript">
        
        SP.SOD.executeOrDelayUntilScriptLoaded(Initialize, 'SP.js');
    
    
         function Initialize() {
    
            var ID = "1";
            var SizeTiles;
            var WebPartNumber;
            var ListPerview;
         
            context = new SP.ClientContext.get_current();
            site = context.get_web();
            context.load(site);
            var targetList = clientContext.get_web().get_lists().getByTitle('TilesSet');
            targetListItem = targetList.getItemById(ID);
            clientContext.load(SizeTiles, 'Size');
            clientContext.load(WebPartNumber, 'WebPart Number');
            clientContext.load(ListPerview, 'List per View');
            alert(SizeTiles);
            function onQuerySucceeded() {
    
                alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('Title'));
    
            }
    
            function onQueryFailed(sender, args) {
                alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
            }
    
        }
    Any idea?

    Wednesday, December 11, 2013 10:16 AM

Answers

  • Hi gohberto,

    Generally this means your context is not able to find your min.js file.

    Try calling like below

     $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(sharePointReady, "sp.js"); });
    OR
    1. If you are writing this in page or web part user control, I would recommend to use this scrip from a seperate JS file.
    2. AND / OR
    3. Try to change order of <script> tags, such as your "min.js" should come first, then "SP.js" and then "yourCustom.js"
    4. if these functions are applicable through out pages, put script link tags in master page and then call these JS functions.

    Wednesday, December 11, 2013 11:17 AM
  • hi

    you define variable called "context", but then try to use another variable "clientContext". Instead of this code:

    context = new SP.ClientContext.get_current();
    site = context.get_web();
    context.load(site);

    use the following:

    var clientContext = new SP.ClientContext.get_current();
    site = clientContext.get_web();
    clientContext.load(site);


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, December 11, 2013 12:20 PM
  • Change all "clientContext" in your code to "context" as that's the variable you are initializing your ClientContext with.


    These postings are provided "AS IS" with no warranties, and confers no rights.

    Wednesday, December 11, 2013 8:15 PM

All replies

  • Hi gohberto,

    Generally this means your context is not able to find your min.js file.

    Try calling like below

     $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(sharePointReady, "sp.js"); });
    OR
    1. If you are writing this in page or web part user control, I would recommend to use this scrip from a seperate JS file.
    2. AND / OR
    3. Try to change order of <script> tags, such as your "min.js" should come first, then "SP.js" and then "yourCustom.js"
    4. if these functions are applicable through out pages, put script link tags in master page and then call these JS functions.

    Wednesday, December 11, 2013 11:17 AM
  • hi

    you define variable called "context", but then try to use another variable "clientContext". Instead of this code:

    context = new SP.ClientContext.get_current();
    site = context.get_web();
    context.load(site);

    use the following:

    var clientContext = new SP.ClientContext.get_current();
    site = clientContext.get_web();
    clientContext.load(site);


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, December 11, 2013 12:20 PM
  • if you do this

    SP.SOD.executeOrDelayUntilScriptLoaded(CallYourFunction, "sp.js");

    and it still does not load, then you need to make sure SP.js is being loaded or load it yourself. I ran into it where it wasn't loading on certain pages I forget if it was publishing or team site. I had to put this below into my custom master page

    <SharePoint:ScriptLink runat="server" Name="SP.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink>

    -tom daly

    Wednesday, December 11, 2013 8:03 PM
  • Change all "clientContext" in your code to "context" as that's the variable you are initializing your ClientContext with.


    These postings are provided "AS IS" with no warranties, and confers no rights.

    Wednesday, December 11, 2013 8:15 PM