0
votes

I have used the "sp.js" to access the sharepoint ClientContext in SharePoint 2010 and 2013 webparts. I want to use the SP ClientContext in my office 365 add-in.

The code i have used,

ExecuteOrDelayUntilScriptLoaded(function () {
    var context = new SP.ClientContext.get_current();
}, "sp.js");

It failed to load the "sp.js" file when i tried it in office 365 SharePoint add-in. How to resolve this issue? What is the way to use "sp.js" file in office 365 SharePoint add-in?

EDIT: I am creating the SharePoint hosted add-in.

EDIT:2 I have created a new SharePoint add-in project in VisualStudio 2015, and it has the html code. So, i am unable to use below code in my page,

<SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />

This is how my project looks like, This is how my project looks like

3

3 Answers

0
votes

Can you let us know if you're doing a Provider hosted or SharePoint hosted add-in?

0
votes

In your .aspx page that you are using for the add-in include the following snippet. <asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server"> <SharePoint:ScriptLink Name="sp.js" runat="server" LoadAfterUI="true" Localizable="false" /> </asp:Content>

0
votes

Add this to the top of the Default.aspx page:

<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Then within ContentPlaceHolderID="PlaceHolderAdditionalPageHead" add:

<SharePoint:ScriptLink name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />

Also, your screenshot is of a Provider hosted add-in, not a SharePoint hosted one. That's why SP.js references were not added automatically. You will likely want to recreate your visual studio solution using the correct add-in settings. A SharePoint Hosted add-in never has a separate web application project within the solution.