10
 $(document).ready(function () {
            var count = 0;

retrieveCurrentListProperties();


            function retrieveCurrentListProperties() {
                clientContext = new SP.ClientContext.get_current();
                web = clientContext.get_web();
                var list = web.get_lists().getByTitle("Urgent Alerts");
                var camlQuery = new SP.CamlQuery();
                var q =  "<View><Query><Where><Eq><FieldRef Name='End_x0020_Date'/><Value Type='DateTime'><Today/></Value></Eq></Where></Query></View>";
                camlQuery.set_viewXml(q);
                this.listItems = list.getItems(camlQuery);
                clientContext.load(this.listItems);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onCListItemsLoadSuccess), 
                Function.createDelegate(this, this.onQueryFailed));

            }

            function onCListItemsLoadSuccess(sender, args) {



                var listEnumerator = this.listItems.getEnumerator();
    //iterate though all of the items

                count = this.listItems.get_count();

alert(count);

            }
            function onQueryFailed(sender, args) {
                alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
            }
 });

Here is my code I get an error Unable to get property 'get_current' of undefined or null reference is there anything I need to import to get this to work?

dave
  • 195
  • 2
  • 3
  • 13

4 Answers4

15

If this is an app or anything outside of a SharePoint page you need to load

document.write('<script src="' + spHostUrl + '/_layouts/15/MicrosoftAjax.js"><\/script>')
document.write('<script src="' + spHostUrl + '/_layouts/15/sp.runtime.js"><\/script>')
document.write('<script src="' + spHostUrl + '/_layouts/15/sp.js"><\/script>')

where hostUrl is the path to your SharePoint site (not the app web).

You should also use

SP.SOD.executeFunc('sp.js', null, function(){
      retrieveCurrentListProperties();
})

to make sure that sp.js is loaded before executing your function

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
  • 2
    This is crazy! This question and answer each only have 1 vote but there have been over 3k views... I'll add some even though Robert doesn't need any more rep! – John-M Nov 19 '14 at 19:41
  • This was happening for me on clientContext = new SP.ClientContext.get_current();, too (which I agree with another poster here that new was not necessary) until I added that 2nd block in the answer. Corrected the issue for me in SP 2013. Good post. – vapcguy Jan 27 '16 at 20:39
3

[Applies to SP 2010]

You need to make sure that the SP.js file is loaded before executing the function.

Taken from the MSDN:

SP.SOD.execute

Executes the specified function in the specified file with the optional arguments.

SP.SOD.executeOrDelayUntilScriptLoaded

Executes the specified function after the file specified has been loaded; otherwise, adds it to the pending job queue.

In your case, you need to call the latter (executeOrDelayUntilScriptLoaded), as the function to be called is not contained within the SP.js file.

So your code should look like:

<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(retrieveCurrentListProperties, "SP.js");
function retrieveCurrentListProperties() { // code omitted for brevity }
function onCListItemsLoadSuccess(sender, args) {}
function onQueryFailed(sender, args) {}

</script>
MdMazzotti
  • 4,787
  • 3
  • 17
  • 35
  • This is dead wrong in SP 2013 at least – Robert Lindgren Apr 01 '14 at 09:49
  • @RobertLindgren I'm referring to SP2010, where this is certainly not wrong at all. Can you point me to an authoritative reference that proves that this is wrong in SP2013 ? Meanwhile, see here :http://sinclairinat0r.com/2014/02/05/make-sp-sod-executefunc-and-sp-clientcontext-function-properly-in-sharepoint-2013/ – MdMazzotti Apr 01 '14 at 10:10
  • 1
    If you have MDS enabled for example, then ExecuteOrDelayUntilScriptLoaded will not be executed in many cases and hence causing your code to fail. – Robert Lindgren Apr 01 '14 at 10:20
  • I had one typo (missed Func) but here is a great discussion rearding this topic: http://sharepoint.stackexchange.com/questions/58503/sp-sod-how-to-use-correctly – Robert Lindgren Apr 01 '14 at 10:23
  • Also here for comparing: http://www.ilovesharepoint.com/2010/08/sharepoint-scripts-on-demand-spsod.html – Robert Lindgren Apr 01 '14 at 10:25
  • Anyhow, "dead wrong" is still too peremptory to me (because it means it never works; in this case, it may or may not, depending on the settings). SP.js in 2010 is always there and executeOrDelayUntilScripLoaded works properly. I edited my answer to specify that it only applies to sp2010. – MdMazzotti Apr 01 '14 at 10:41
  • Uh, we have these as our top 2 lines in a JS file we're running from an SP 2013 page: var $j = jQuery.noConflict(); $j(document).ready(ExecuteOrDelayUntilScriptLoaded(initPage, "sp.js"));, where initPage is a separate function we run onload. "Wrong" or not, it does work. Sometimes "wrong" just means "legacy" or "outdated", and maybe that's what was meant. But this actually DOES work for us in SP 2013. – vapcguy Jan 27 '16 at 20:43
  • 1
    @vapcguy yep, that's what I meant with my previous comment. Glad it worked for you – MdMazzotti Jan 27 '16 at 21:51
0

dont use new keyword for the instation of sharepoint javascript object.

the below declaration is the correct

SP.ClientContext.get_current();

  • 1
    I cringe every time I see new SP.ClientContext.get_current(), although it does actually work, and is probably not a problem here. Also, there are several places where new operator should be used in combination with "SharePoint JavaScript objects" – eirikb Nov 19 '14 at 18:41
  • 1
    While the above is correct usage, it's not what will fix the issue. The issue is making sure sp.js loads first, before calling SP.ClientContext.get_current() in the first place. – vapcguy Jan 27 '16 at 20:47
0

This happens only when you refresh the opened page more than once. Try opening the record again newly and you will not see this issue.