在使用SharePoint 2013的JOM时,出现以下问题:

ReferenceError: SP is not defined

经反复试验和搜索,得出以下两种方式:

一、直接引用JS文件,引用顺序很重要:

<script type="text/javascript" src="https://nn.sharepoint.com/teams/ap1/gct/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="https://nn.sharepoint.com/teams/ap1/gct/_layouts/15/SP.Runtime.js"></script>
<script type="text/javascript" src="https://nn.sharepoint.com/teams/ap1/gct/_layouts/15/SP.js"></script>
<script type="text/javascript" src="https://nn.sharepoint.com/teams/ap1/gct/_layouts/15/SP.RequestExecutor.js"></script>

<script src="https://nn.sharepoint.com/teams/ap1/gct/SiteAssets/jquery-1.9.1.js"></script>

<script type="text/javascript">
var hostweburl ="https://nn.sharepoint.com/teams/ap1/gct";

$(document).ready(function () {
    var scriptbase = hostweburl + "/_layouts/15/";
    //ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
initializePage();
});

function initializePage()
{
    var context = SP.ClientContext.get_current();
    var user = context.get_web().get_currentUser();

    // This code runs when the DOM is ready and creates a context object which 

is needed to use the SharePoint object model
    $(document).ready(function () {
        getUserName();
    });

    // This function prepares, loads, and then executes a SharePoint query to 

get the current users information
    function getUserName() {
        context.load(user);
        context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
    }

    // This function is executed if the above call is successful
    // It replaces the contents of the 'message' element with the user name
    function onGetUserNameSuccess() {
        $('#message').text('Hello ' + user.get_title());
    }

    // This function is executed if the above call fails
    function onGetUserNameFail(sender, args) {
        alert('Failed to get user name. Error:' + args.get_message());
    }
}
View Code

相关文章:

  • 2021-09-15
  • 2021-08-11
  • 2021-06-07
  • 2021-10-08
  • 2021-07-22
  • 2022-03-12
  • 2021-05-20
  • 2021-12-17
猜你喜欢
  • 2022-12-23
  • 2021-06-03
  • 2021-12-05
  • 2022-03-10
  • 2022-12-23
  • 2021-06-10
  • 2022-01-27
相关资源
相似解决方案