javascript 为什么 SP.js 没有加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16463496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:45:53  来源:igfitidea点击:

Why is SP.js not loading?

javascriptsharepointsp.js

提问by user2366475

I have researched this pretty thoroughly and everyone says that the code I have below should load SP.js, but I cannot get it to load.

我对此进行了非常彻底的研究,每个人都说我下面的代码应该加载 SP.js,但我无法加载它。

Debugging I get:

调试我得到:

NewForm.aspx, line 1667 character 5
SCRIPT5009: 'PeoplePicker' is undefined 

and do not see SP.JS under view sources.

并且在查看源代码下看不到 SP.JS。

<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" 
    Localizable="false" /> 
<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(SetWebUserData(), "SP.js");

function SetWebUserData() {
    var pplPicker = new PeoplePicker();
    // Set the parent tag id of the people the picker.
    pplPicker.SetParentTagId('Main_x0020_Contact');
    pplPicker.SetLoggedInUser();
    };
</script>

Any assistance greatly appreciated.

非常感谢任何帮助。

回答by Anders Aune

You are using ExecuteOrDelayUntilScriptLoaded wrong. You should only pass the function name, it should look like this:

您使用 ExecuteOrDelayUntilScriptLoaded 是错误的。您应该只传递函数名称,它应该如下所示:

ExecuteOrDelayUntilScriptLoaded(SetWebUserData, "sp.js");

Without the ()

没有 ()

回答by Frébo

The first parameter of ExecuteOrDelayUntilScriptLoadedhas to be a function. This function is called after the requested script file has been loaded.

ExecuteOrDelayUntilScriptLoaded的第一个参数必须是一个函数。在加载请求的脚本文件后调用此函数。

<SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" 
    Localizable="false" /> 
<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(SetWebUserData, "SP.js");

function SetWebUserData() {
    var pplPicker = new PeoplePicker();
    // Set the parent tag id of the people the picker.
    pplPicker.SetParentTagId('Main_x0020_Contact');
    pplPicker.SetLoggedInUser();
    };
</script>

With (), you calla function. This means, your error was to pass the resultof your function as parameter, not the function itself.

使用 ()调用函数。这意味着,您的错误是将函数的结果作为参数传递,不是函数本身。

Example for better understanding:

更好理解的示例:

function helloFunction() {
    return 42;
}

var myHelloFunction = helloFunction; // Function is passed
var myHelloFunctionResult = helloFunction(); // Result of your function (42) is passed

回答by user2366475

I was able to resolve the issue and have people picker populated with the current user by using code found here: http://vbcity.com/blogs/skullcrusher/archive/2008/11/04/set-sharepoint-peoplepicker-field-mark-2.aspx

我能够通过使用此处找到的代码解决该问题并使用当前用户填充人员选择器:http: //vbcity.com/blogs/skullcrusher/archive/2008/11/04/set-sharepoint-peoplepicker-field-标记-2.aspx

This code does not require SP.js

此代码不需要 SP.js

I was never able to get sp.js to load properly, but this solution fixes my problem.

我永远无法让 sp.js 正确加载,但此解决方案解决了我的问题。

回答by Christopher

I know that it is a little late, but I think this is the answer you are looking for.

我知道现在有点晚了,但我认为这就是您正在寻找的答案。

SP.SOD.executeFunc('sp.js', 'SP.ClientContext',function(){
  var pplPicker = new PeoplePicker();
  // Set the parent tag id of the people the picker.
  pplPicker.SetParentTagId('Main_x0020_Contact');
  pplPicker.SetLoggedInUser();
};