• Provider Hosted App中使用JOM问题


    在使用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

    二、使用Jquery 的$.getScript 方法

    <script type="text/javascript">
    var hostweburl ="https://nike.sharepoint.com/teams/ap1/gctech";
    
    $(document).ready(function () {
        var scriptbase = hostweburl + "/_layouts/15/";
        var scriptBase = hostweburl + "/_layouts/15/";
    $.getScript(scriptBase + "MicrosoftAjax.js").then(function (data) {
        return $.getScript(scriptbase + "SP.Runtime.js");
    }).then(function (data) {
        return $.getScript(scriptbase + "SP.js");
    }).then(function (data) {
        $.getScript(scriptBase + "SP.RequestExecutor.js");
    }).then(function (data) {
        alert("Load as order");
    /*
    var ctx = new SP.ClientContext(appWebUrl),
            factory = new SP.ProxyWebRequestExecutorFactory(appWebUrl),
            web;
    
        ctx.set_webRequestExecutorFactory(factory);
        web = ctx.get_web();
        ctx.load(web);
        ctx.executeQueryAsync(function() {
            // log the name of the app web to the console
            console.log(web.get_title());
        }, function(sender, args) {
            console.log("Error : " + args.get_message());
        });
    */
    });
    </script>
    View Code
  • 相关阅读:
    python数据精度问题
    python之数学函数应用
    grep使用常用操作十五条
    python3之35个关键字详解
    Golang基础教程
    python之数据类型转换
    python随机值生成的常用方法
    losf命令详解
    python推导式与海象运算符
    即时通讯安全篇(十):IM聊天系统安全手段之通信连接层加密技术
  • 原文地址:https://www.cnblogs.com/windy2008/p/5796107.html
Copyright © 2020-2023  润新知