本文介绍了修改PageView WebPart链接值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Javascript修改PageView Web部件链接值.我应该能够从字段中读取现有的值(链接),并在其中附加文本,然后将其保存回Web部件.请问我能帮我编写脚本吗?

I need to modify PageView web part link value using Javascript. I should be able to read existing value(link) from the field and append with my text to it and save it back to the web part. Can you help me with script, please.

谢谢Alex

推荐答案

目前尚无 OOB api来更新我所知的属性.

There is no  OOB api to update the property per my knowledge currently.

https://msdn.microsoft. com/zh-CN/library/office/jj246541.aspx

您可以删除旧的并添加新的,这是我的测试代码供您参考.

<script type="text/javascript">
        var serverRelativeUrl = '/SitePages/PageViewer1.aspx';
        var webPartXml = '<?xml version="1.0" encoding="utf-8"?>' +
                        '<WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">' +
                          '<Title>Page Viewer</Title>' +
                          '<FrameType>Default</FrameType>' +
                          '<Description>Displays another Web page on this Web page. The other Web page is presented in an IFrame.</Description>' +
                          '<IsIncluded>true</IsIncluded>' +
                          '<ZoneID>wpz</ZoneID>' +
                          '<PartOrder>1</PartOrder>' +
                          '<FrameState>Normal</FrameState>' +
                          '<Height />' +
                          '<Width />' +
                          '<AllowRemove>true</AllowRemove>' +
                          '<AllowZoneChange>true</AllowZoneChange>' +
                          '<AllowMinimize>true</AllowMinimize>' +
                          '<AllowConnect>true</AllowConnect>' +
                          '<AllowEdit>true</AllowEdit>' +
                          '<AllowHide>true</AllowHide>' +
                          '<IsVisible>true</IsVisible>' +
                          '<DetailLink />' +
                          '<HelpLink />' +
                          '<HelpMode>Modeless</HelpMode>' +
                          '<Dir>Default</Dir>' +
                          '<PartImageSmall />' +
                          '<MissingAssembly>Cannot import this Web Part.</MissingAssembly>' +
                          '<PartImageLarge>/_layouts/15/images/mscntvwl.gif</PartImageLarge>' +
                          '<IsIncludedFilter />' +
                          '<Assembly>Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>' +
                          '<TypeName>Microsoft.SharePoint.WebPartPages.PageViewerWebPart</TypeName>' +
                          //update the new url
                          '<ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/PageViewer">/Lists/Supersotres/AllItems.aspx</ContentLink>' +
                          '<SourceType xmlns="http://schemas.microsoft.com/WebPart/v2/PageViewer">URL</SourceType>' +
                        '</WebPart>';
        var limitedWebPartManager;
        function RemoveOldWebPart() {
            this.clientContext = new SP.ClientContext.get_current();
            var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
            limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
            this.collWebPart = limitedWebPartManager.get_webParts();
            clientContext.load(collWebPart);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.AddNewWebPart), Function.createDelegate(this, this.onQueryFailed));
        }

        function AddNewWebPart() {
            if (!collWebPart.get_count()) {
                alert('No Web Parts on this page.');
            }
            //delete old one, update based on your webpart count, here i have two and the first one is page viewer web part
            var oWebPartDefinition = collWebPart.get_item(0);
            oWebPartDefinition.deleteWebPart();
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }

        function onQuerySucceeded() {
            var NewWebPartDefinition = limitedWebPartManager.importWebPart(webPartXml);
            this.NewWebPart = NewWebPartDefinition.get_webPart();
            // second parameter is zone ID
            limitedWebPartManager.addWebPart(NewWebPart, 'Top', 1);

            clientContext.load(NewWebPart);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onAddSucceeded), Function.createDelegate(this, this.onQueryFailed));
        }

        function onAddSucceeded() {
            alert('complete');
        }
        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    </script>
    <input id="Button1" onclick="RemoveOldWebPart()" type="button" value="button" />

最好的问候,

Lee


这篇关于修改PageView WebPart链接值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:09