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?

  • Is this an app? – Robert Lindgren Apr 1 '14 at 8:46
  • It's on my sharepoint site, I created a page and added embedded code and linked a JS file to it where I have this code – dave Apr 1 '14 at 8:49
  • Ok, then I think this: SP.SOD.execute('sp.js', function() from my answer below is the key (to wait for sp.js to load) – Robert Lindgren Apr 1 '14 at 9:09
  • I know get the error object does not support method or property split – dave Apr 1 '14 at 9:19
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

  • 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>
  • This is dead wrong in SP 2013 at least – Robert Lindgren Apr 1 '14 at 9: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 :sinclairinat0r.com/2014/02/05/… – MdMazzotti Apr 1 '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 1 '14 at 10:20
  • I had one typo (missed Func) but here is a great discussion rearding this topic: sharepoint.stackexchange.com/questions/58503/… – Robert Lindgren Apr 1 '14 at 10:23
  • 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.