AddWebPart

Function

$().SPServices

Web Service

WebPartPages

Operation

AddWebPart

Example

Here's an example provided by nrahlstr. Thanks, Nathan!

The "str" variable below is the full XML text of a content editor web part encoded using this tool: http://coderstoolbox.net/string/

var str = "<?xml version="1.0" encoding="utf-16"?><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>Custom Part</Title><FrameType>Default</FrameType><Description>Use for formatted text, tables, and images.</Description><IsIncluded>true</IsIncluded><ZoneID>Left</ZoneID> <PartOrder>6</PartOrder><FrameState>Normal</FrameState><Height /><Width /> <AllowRemove>true</AllowRemove><AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize><IsVisible>true</IsVisible><DetailLink /><HelpLink /><Dir>Default</Dir><PartImageSmall /><MissingAssembly /><PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter /><Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly><TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName> <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" /><Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><![CDATA[<P>" + new Date() + "</P>]]></Content> <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" /></WebPart>";

$().SPServices({
    operation: "AddWebPart",
    webPartXml: str,
    pageUrl: "http://.../sandbox1/default.aspx",
    storage: "Shared",
    async: true,
    completefunc: function (xData, Status) {
        alert("Status: " + Status + " xData: " + xData.responseText);
    }
});

Back to top