SP.Ribbon.WebPartComponent.getWebPartAdder() returns undefined

喜你入骨 提交于 2019-12-12 04:44:27

问题


I am using the source at http://blog.symprogress.com/2010/11/ribbon-insert-any-web-part-using-javascript/ to handle user web part button click event.

The function 'addWebPart()' calls a function 'SP.Ribbon.WebPartComponent.getWebPartAdder()' which is supposed to return adder instance but sometimes it returns undefined.

If I add a while loop to wait for the instance value to return correctly, the browser in my VM stalls for some time. When an instance is returned, the browser becomes responsive again. This only happens in some instances.

I am using SharePoint 2013 and the section of code I am referring to is:

addWebPart = function (wpCategory, wpTitle) {

            var webPartAdder = SP.Ribbon.WebPartComponent.getWebPartAdder();

            while (webPartAdder == undefined)
                webPartAdder = SP.Ribbon.WebPartComponent.getWebPartAdder();

            // ... Other stuff ...
}

How can this issue be resolved?


回答1:


For anyone looking for an answer to this question, turns out you have to call 'LoadWPAdderOnDemand()' function then wait for the event '_spEventWebPartAdderReady'. Then query for 'window.WPAdder':

    addWebPartDelayed = function (webPartAdder, wpCategory, wpTitle) {

        var webPart = findWebPart(webPartAdder, wpCategory, wpTitle);
        if (webPart) {
            var zone = WPAdder._zones[0];
            var wpid = WPAdder._createWebpartPlaceholderInRte();
            WPAdder.addItemToPageByItemIdAndZoneId(webPart.id, zone.id, 0, wpid);
        }
        else
            alert('ERROR: Web part not found! Please try again after sometime.');
    },

    addWebPart = function (wpCategory, wpTitle) {

        var webPartAdder = window.WPAdder;

        if (webPartAdder == undefined) {
            LoadWPAdderOnDemand();
            ExecuteOrDelayUntilEventNotified(
                function () {
                    var webPartAdder = window.WPAdder;

                    addWebPartDelayed(webPartAdder, wpCategory, wpTitle);
                },
                "_spEventWebPartAdderReady");
        }
        else
            addWebPartDelayed(webPartAdder, wpCategory, wpTitle);            
    };


来源:https://stackoverflow.com/questions/22318388/sp-ribbon-webpartcomponent-getwebpartadder-returns-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!