18

I would like to use the SharePoint Javascript Object Model from a "plain" html page (ie, not an .aspx application page etc.) Is this even possible? If so, what scripts should I reference? I've added the following:

  <script type="text/ecmascript" src="/_layouts/SP.js" />
  <script type="text/ecmascript" src="/_layouts/SP.Core.js" />
  <script type="text/ecmascript" src="/_layouts/SP.Debug.js" />
  <script type="text/ecmascript" src="/_layouts/SP.Runtime.Debug.js" />

But when the page loads, I get a 'Type is not defined' error in SP.js and SP.Core.js. Note this is from within the SP site, so this isn't a cross-domain issue.

1

5 Answers 5

5

The SharePoint JavaScript client object model only works on pages that are a part of the SharePoint framework (i.e. in Web Parts, Application Pages, etc.). This tutorial makes mention of that: http://www.codeproject.com/Articles/60348/SharePoint-2010-Client-Object-Model-for-JavaScript.aspx

2
  • You could, however, write a custom REST or AJAX web service that does the calls for you and then you could access those from your JavaScript. Nov 6, 2011 at 1:08
  • I actually got it 'almost working', but SP.js definitely depends on some markup and other scripts only found in application pages. REST and Ajax against the built-in listdata.svc, etc. is what I landed on, though I'm not sure I'll be able to get at everything I would have liked (like SPWeb.Navigation).
    – Daniel
    Nov 7, 2011 at 2:34
4

This is what I used to fix this problem:

<SharePoint:ScriptLink Name="MicrosoftAjax.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.core.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.js" runat="server" Defer="True" Localizable="false"/>

I added the above ScriptLinks in this order, and defered both the MicrosoftAjax.js and SP.Core.js includes, and delayed the SP.js. The SP.js file has dependencies in the preceding includes that requires it to be delayed.

2
  • 1
    this would not work on a plain html page, just a .aspx
    – Daniel
    Mar 27, 2014 at 19:54
  • This works perfectly on custom aspx pages
    – Jake
    Nov 13, 2018 at 7:08
0
<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" Localizable="false" />

will do the work.

0

You are missing init.js.

Add it just like the rest of the scripts you have referenced and you should stop getting the dependency errors.

2
  • Can't seem to get this one to work, still getting the dependency errors on SP2010. Any tips? Should it be <script type="text/ecmascript" src="/_layouts/init.js" />?
    – MattV
    Dec 6, 2016 at 9:54
  • @MattV It's been a while since I was doing this work and I don't have access to it so I can't just look it up for you. That is generally the idea though. Sorry I can't be of more help. Maybe check to make sure that is the actual path of your init.js. Dec 11, 2016 at 21:49
0

The JSOM uses the Type.registerNamespace method in the Microsoft Ajax library to register the SP namespace. Use the following code to add a reference to the Microsoft Ajax library from your page: <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>

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.