动态给Sharepoint的列表的三个Form添加Webparts

来源:互联网 发布:香港陈枝记铁锅淘宝网 编辑:程序博客网 时间:2024/05/06 11:23


function AddWebParts() {
        var newUrl = '/dev/dev1';//你的网站集
        var i = 0;
        $('#formname tr').each(function () {
         
            var relativeUrl = $(this).children('td:eq(1)').html();//第二个td
            var clientContext = new SP.ClientContext(newUrl);
            var oFile = clientContext.get_web().getFileByServerRelativeUrl(relativeUrl);

            var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
            var myForms = new Array("../../Pages/mDisPlayForm.aspx", "../../Pages/mEditForm.aspx", "../../Pages/mNewForm.aspx");//需要的ContentLink
            var webPartXml = '<?xml version=\"1.0\" encoding=\"utf-8\"?>' +
                '<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"' +
                ' xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"' +
                ' xmlns=\"http://schemas.microsoft.com/WebPart/v2\">' +
                '<Title>My Web Part'+i+'</Title><FrameType>Default</FrameType>' +
                '<Description>Use for formatted text, tables, and images.</Description>' +
                '<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</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/images/mscontl.gif</PartImageLarge><IsIncludedFilter />' +
                '<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, ' +
                'PublicKeyToken=94de0004b6e3fcc5</Assembly>' +
                '<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>' +
                '<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" >'+myForms[i]+'</ContentLink>' +
                '<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">' +
                '<![CDATA[This is a first paragraph!<DIV>&nbsp;</DIV>And this is a second paragraph.]]></Content>' +
                '<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>';
         
            var oWebPartDefinition = limitedWebPartManager.importWebPart(webPartXml);
            var oWebPart = oWebPartDefinition.get_webPart();

            limitedWebPartManager.addWebPart(oWebPart, 'Left', 1);
            i++;

            clientContext.load(oWebPart);
       
        

            clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
            function onQuerySucceeded() {

                alert('Web Part added: ' + oWebPart.get_title());

            }

            function onQueryFailed(sender, args) {

                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        })
       
     


    }

0 0
原创粉丝点击