当前位置: 首页>>技术教程>>正文


未捕获的TypeError:无法读取未定义的属性’get_current’

webfans 技术教程 , , , 去评论

问题描述

这个很奇怪我希望有人可以帮助我。

我为文档库中的标注创建了自定义操作。我在文档库默认视图顶部的代码段webpart中添加了javascript代码。

当我单击左侧导航菜单转到库时,代码会在我点击3个点时抛出此异常。

Uncaught TypeError: Cannot read property 'get_current' of undefined 

如果我按Ctrl + F5,代码工作正常。

<script type="text/javascript">
    SP.SOD.executeFunc("callout.js", "Callout", function () {
        IsCurrentUserMemberOfGroup("Approvers", function(isSuccess){
            if(isSuccess){
                var itemCtx = {};
                itemCtx.Templates = {};
                itemCtx.BaseViewID = 'Callout';
                // Define the list template type
                itemCtx.ListTemplateType = 101;

                itemCtx.Templates.Footer = function (itemCtx) {             
                  return CalloutRenderFooterTemplate(itemCtx, AddCustomCompleteAction, true);
                };  

                SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);
            }               
        });
    });


    function AddCustomCompleteAction (renderCtx, calloutActionMenu) {  
        var Status = renderCtx.CurrentItem.Status;

        if(renderCtx.CurrentItem.ContentType=='Bill Cycle'  && Status=='') {
                calloutActionMenu.addAction (new CalloutAction ({
                text: 'COMPLETE',
                tooltip: 'This action will set status to Complete, this action cant be rolled back. Only click once the action can take a few seconds to complete.',
                onClickCallback: function() {UpdateBillCycleStatusToCompleted(renderCtx.CurrentItem.ID);}
            }));            
        }

        // Show the default document library actions
        CalloutOnPostRenderTemplate(renderCtx, calloutActionMenu);

         // Show the follow action
          calloutActionMenu.addAction(new CalloutAction({
            text: Strings.STS.L_CalloutFollowAction,
            tooltip: Strings.STS.L_CalloutFollowAction_Tooltip,
            onClickCallback: function (calloutActionClickEvent, calloutAction) {
              var callout = GetCalloutFromRenderCtx(renderCtx);
              if (!(typeof(callout) === 'undefined' || callout === null))
                callout.close();
              SP.SOD.executeFunc('followingcommon.js', 'FollowSelectedDocument', function() { FollowSelectedDocument(renderCtx); });
            }
          }));
    }   

    function UpdateBillCycleStatusToCompleted(itemId) {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Cycles');
        this.oListItem = oList.getItemById(itemId);
        oListItem.set_item('Status', 'Completed');
        oListItem.update();
        clientContext.executeQueryAsync(Function.createDelegate(this, this.StatusCompletedSucceeded), Function.createDelegate(this, this.StatusCompletedFailed));
    }

    function StatusCompletedSucceeded() {
        alert(' set to completed succeeded');
        window.location.reload(true);
    }

    function StatusCompletedFailed(sender, args) {
        alert(' update failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    function IsCurrentUserMemberOfGroup(groupName, callback) {
        var currentContext = new SP.ClientContext.get_current();
        var currentWeb = currentContext.get_web();

        var currentUser = currentContext.get_web().get_currentUser();
        currentContext.load(currentUser);

        var allGroups = currentWeb.get_siteGroups();
        currentContext.load(allGroups);

        var group = allGroups.getByName(groupName);
        currentContext.load(group);

        var groupUsers = group.get_users();
        currentContext.load(groupUsers);

        currentContext.executeQueryAsync(OnSuccess,OnFailure);

        function OnSuccess(sender, args) {
            var userInGroup = false;
            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) {
                var groupUser = groupUserEnumerator.get_current();
                if (groupUser.get_id() == currentUser.get_id()) {
                    userInGroup = true;
                    break;
                }
            }  
            callback(userInGroup);
        }

        function OnFailure(sender, args) {
            callback(false);
        }        
    }
</script>

最佳解决方法

由于您在CSR中使用SP.ClientContext,因此请确保在渲染代码运行之前加载SharePoint脚本文件’sp.js’:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
 //Your code goes here...
});

次佳解决方法

修好了

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(CustomAction, "sp.js"); });

    function CustomAction()

    {
           //Your Code
    }

第三种解决方法

当我使用WorkflowServices时,这对我有用。它也适用于你的情况:

ExecuteOrDelayUntilScriptLoaded(function () {
    ExecuteOrDelayUntilScriptLoaded(function () {
        SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
        SP.SOD.registerSod('SP.WorkflowServices.WorkflowServicesManager', SP.Utilities.Utility.getLayoutsPageUrl('SP.WorkflowServices.js'));
        SP.SOD.loadMultiple(['SP.ClientContext', 'SP.WorkflowServices.WorkflowServicesManager'], function () {
            var context = SP.ClientContext.get_current();
            var web = context.get_web();
            var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
            var instanceService = servicesManager.getWorkflowInstanceService();
        });
    }, "sp.js");
}, "sp.runtime.js");

参考资料

本文由朵颐IT整理自网络, 文章地址: https://duoyit.com/article/2690.html,转载请务必附带本地址声明。