This topic has not yet been rated - Rate this topic

WebPartPagesWebService.AddWebPart Method

Windows SharePoint Services 3
Adds a new Web Part to a Web Part Page.

Web Service: Web Part PagesWeb Reference: http://<Site>/_vti_bin/WebPartPages.asmx
[SoapDocumentMethodAttribute("http://microsoft.com/sharepoint/webpartpages/AddWebPart", RequestNamespace="http://microsoft.com/sharepoint/webpartpages", ResponseNamespace="http://microsoft.com/sharepoint/webpartpages", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public Guid AddWebPart (
	string pageUrl,
	string webPartXml,
	Storage storage
)

Parameters

pageUrl

A string containing the URL of the Web Part Page.

webPartXml

A string containing the XML of the Web Part.

storage

A Storage value indicating how the Web Part was stored.

Return Value

A GUID that identifies the newly created Web Part.

To access the WebPartPagesWebService service and its methods, set a Web reference to http://Virtual_Server_Name:Port_Number/_vti_adm/WebPartPages.asmx.

The following code example shows a locally defined AddWebPart method that adds a Web Part and then modifies it by calling the AddWebPart and SaveWebPart methods of the Web Part Pages service through a proxy. This code example and the proxy are part of a larger example provided for the Web Part Pages service.

private void AddWebPart()
{
    // NOTE: The Web Service we are using is defined on MyServer/_vti_bin
    // Declare and initialize a variable for the WebPartPages Web Service.
    WebpartpagesSvc.WebPartPagesWebService svc = new Microsoft.Samples.WebPartPagesSvcClient.WebpartpagesSvc.WebPartPagesWebService();

    // Authenticate the current user by passing their default
    // credentials to the Web Service from the system credential cache.
    svc.Credentials = System.Net.CredentialCache.DefaultCredentials;
    string pageUrl = "http://MyServer/Shared%20Documents/SampleStart.aspx";
        
    // Create Web Part XML inline. 
    string webPartXml = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n" +    "<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\">\n" +
    "<Title>Content Editor Web Part</Title>\n  " +
    "<FrameType>Default</FrameType>\n  " +
    "<Description>Use for formatted text, tables, and images.</Description>\n  " +
    "<IsIncluded>true</IsIncluded>\n  " +
    "<ZoneID>Header</ZoneID>\n  " +
    "<PartOrder>1</PartOrder>\n  " +
    "<FrameState>Normal</FrameState>\n  " +
    "<Height />\n  " +
    "<Width />\n  " +
    "<AllowRemove>true</AllowRemove>\n  " +
    "<AllowZoneChange>true</AllowZoneChange>\n  " +
    "<AllowMinimize>true</AllowMinimize>\n  " +
    "<IsVisible>true</IsVisible>\n  " +
    "<DetailLink />\n  " +
    "<HelpLink />\n  " +
    "<Dir>Default</Dir>\n  " +
    "<PartImageSmall />\n  " +
    "<MissingAssembly />\n  " +
    "<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>\n  " +
    "<IsIncludedFilter />\n  " +
    "<Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>\n  " +
    "<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>\n  " +
    "<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />\n  " +
    "<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\"><![CDATA[<P>Hello</P>]]></Content>\n  " +
    "<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />\n</WebPart>";

    // Add Web Part, store the new WebPart GUID into a variable.
    Guid newPartId = svc.AddWebPart(pageUrl, webPartXml, WebpartpagesSvc.Storage.Shared);           
            
    Console.WriteLine("A new Web Part with ID [{0}] was added to the page [{1}]", newPartId.ToString(), pageUrl);
    Console.WriteLine("-----Hit enter-----");
    Console.ReadLine();
}

SOAP Request Format   The following is a sample SOAP 1.1 request. The placeholders shown need to be replaced with actual values.

POST /_vti_bin/webpartpages.asmx HTTP/1.1
Host: Server_Name
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://microsoft.com/sharepoint/webpartpages/AddWebPart"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddWebPart xmlns="http://microsoft.com/sharepoint/webpartpages">
      <pageUrl>string</pageUrl>
      <webPartXml>string</webPartXml>
      <storage>None or Personal or Shared</storage>
    </AddWebPart>
  </soap:Body>
</soap:Envelope>

SOAP Response Format   The following is a sample SOAP 1.1 response. The placeholders shown will be replaced with actual values.

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddWebPartResponse xmlns="http://microsoft.com/sharepoint/webpartpages">
      <AddWebPartResult>guid</AddWebPartResult>
    </AddWebPartResponse>
  </soap:Body>
</soap:Envelope>

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ