4

I am trying to make an html page in an app, and I want to be able to get: SP.Utilities and _spPageContextInfo, which scripts do I need to add to my html in order to make it work?

I am in Office 365. I've tried the following scripts, but nothing worked:

<script type="text/javascript" src="/layouts/15/MicrosoftAjax.js"></script>

<script src="/_layouts/15/core.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.debug.js"></script>
<script type="text/javascript" src="/_layouts/15/init.js"></script>  
<script type="text/javascript" src="/_layouts/15/SP.Core.js" ></script>
<script type="text/javascript" src="/_layouts/15/SP.Debug.js" ></script>
<script type="text/javascript" src="/_layouts/15/SP.Runtime.Debug.js></script>

anyone tried it?

David Hemsey
  • 183
  • 2
  • 12

2 Answers2

3

_spPageContextInfo:

_spPageContextInfo have some useful values that added to SharePoint master page. But SharePoint apps built to be isolated and light.

You can’t access _spPageContextInfo form App pages, but you can access same values by different ways:

  1. URL strings and tokens in apps for SharePoint: http://msdn.microsoft.com/en-us/library/jj163816.aspx
  2. If then you need more values than not provided by tokens you can access what you want by load SP.ClientContext

Example: If you want to get the webServerRelativeUrl of the site on which you have installed your app, then simply

hostweburl = decodeURIComponent(getQueryStringParameter('HostUrl'));

Note: You don't need to add any JS files, you get this from query string

Refer _spPageContextInfo is not defined for more details.

SP.Utilities:

The SP.Utilities can be referred directly from code. For example if you want to send an email, the below snippet is used in the function.

appweburl + "/_api/SP.Utilities.Utility.SendEmail"; 
Asad Refai
  • 5,971
  • 8
  • 34
  • 57
  • thank you. but how do I load SP.ClientContext? which scripts are necessary for it? – David Hemsey Dec 22 '15 at 13:26
  • for example, I want to get the current webServerRelativeUrl, I used to use _spPageContextInfo.webServerRelativeUrl , what do I use now? – David Hemsey Dec 22 '15 at 13:27
  • 1
    @DavidHemsey Info on SP.ClientContext. Once you have a ClientContext object, you can get the current web and its server relative url. You won't need _spPageContextInfo. – wjervis Dec 22 '15 at 13:31
  • @wjervis is correct – Asad Refai Dec 22 '15 at 13:32
  • 1
    thank you guys, but. Again guys, what are the scripts required to get client context, as stated in my question it's not working for me. What scripts should I use to get client content, we can forget about spPageContextInfo now.. – David Hemsey Dec 22 '15 at 13:34
  • @DavidHemsey You probably need SP.SOD.executeOrDelayUntilScriptLoaded, as stated in this answer. Please add error details to your question so we can better help you. – wjervis Dec 22 '15 at 13:38
  • 1
    @DavidHemsey You get the web server relative url, from the query string. See the updated answer. – Asad Refai Dec 22 '15 at 13:44
  • hi all, I used these and they worked, but are they all necessary:
    <script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
    <script src="/_layouts/15/core.js"></script>
    <script type="text/javascript" src="/_layouts/15/init.js"></script>
    <script type="text/javascript" src="/_layouts/15/SP.Core.js"></script>
    
    
    

    I just want someone to tell me which files to add.

    – David Hemsey Dec 22 '15 at 13:45
  • I copied pasted your code, for the HostUrl, and it gave me Uncaught ReferenceError: getQueryStringParameter is not defined(…) – David Hemsey Dec 22 '15 at 13:46
  • Am I missing a script? – David Hemsey Dec 22 '15 at 13:46
  • Refer this article http://www.mavention.com/blog/sharePoint-app-reading-data-from-host-web . You will get this method. You write this function in your script. – Asad Refai Dec 22 '15 at 13:51
  • I am getting: Uncaught ReferenceError: IsNullOrUndefined is not defined on core.js and Uncaught ReferenceError: NotifyScriptLoadedAndExecuteWaitingJobs is not defined on sp.init.js. – David Hemsey Dec 22 '15 at 13:53
  • I don't see the getQueryStringParameter in either the app web, or the host web. It's not even loaded in the host web. Is it a valid sharepoint function? – David Hemsey Dec 22 '15 at 13:56
  • 1
    getQueryStringParameter is declared in some MSDN examples (and everyone copy/pastes those), Not necessary as multiple SP libraries provide the same functionality: http://sharepoint.stackexchange.com/questions/159289/what-does-this-code-getquerystringparameter-do/159290#159290 – Danny '365CSI' Engelman Dec 22 '15 at 13:59
  • Its not referenced in any file.. But u need to write it.. If you notice the article carefully, you will find this function. Just copy paste and start using it. – Asad Refai Dec 22 '15 at 13:59
  • one more question, any difference between sp.core.js, and core.js? – David Hemsey Dec 22 '15 at 14:10
0
  • Load a regular SP page with a ListView
  • Open F12 developer tools
  • Type the name of the function, without ()
  • click the function definition

F12 source panel will open with the file the function is defined in

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79