WebPartPagesWebService.DeleteWebPart Method

Deletes a dynamic Web Part from a Web Part Page.

Web Service: Web Part PagesWeb Reference: http://<Site>/_vti_bin/WebPartPages.asmx

Syntax

<SoapDocumentMethodAttribute("http://microsoft.com/sharepoint/webpartpages/DeleteWebPart", RequestNamespace:="http://microsoft.com/sharepoint/webpartpages", ResponseNamespace:="http://microsoft.com/sharepoint/webpartpages", Use:=SoapBindingUse.Literal, ParameterStyle:=SoapParameterStyle.Wrapped)> _
Public Sub DeleteWebPart ( _
    pageUrl As String, _
    storageKey As Guid, _
    storage As Storage _
)

Dim instance As WebPartPagesWebService
Dim pageUrl As String
Dim storageKey As Guid
Dim storage As Storage

instance.DeleteWebPart(pageUrl, storageKey, storage)
[SoapDocumentMethodAttribute("http://microsoft.com/sharepoint/webpartpages/DeleteWebPart", RequestNamespace="http://microsoft.com/sharepoint/webpartpages", ResponseNamespace="http://microsoft.com/sharepoint/webpartpages", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public void DeleteWebPart (
    string pageUrl,
    Guid storageKey,
    Storage storage
)

Parameters

  • pageUrl
    A string containing the URL of the page containing the Web Part.
  • storageKey
    A GUID that identifies of the Web Part.
  • storage
    A Storage value indicating how the Web Part was stored.

Remarks

The DeleteWebPart method is used to delete a Web Part. Deleting a Shared Web Part, when storage is set to Personal, will throw the exception "Cannot delete Shared 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.

Example

The following code example shows a locally defined DeleteWebPart method that takes a GUID of a Web Part as an argument and deletes the Web Part from a Web Part Page by calling the DeleteWebPart method 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 Sub DeleteWebPart(guidVal As String)
    ' NOTE: The Web Service we are using is defined on MyServer/_vti_bin
    ' Declare and initialize a variable for the WebPartPages Web Service.
    Dim 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

    Dim pageUrl As String = "http://MyServer/Shared%20Documents/SampleStart.aspx"
    ' The GUID represents the Web Part ID being deleted
    Dim storageKey As New Guid(guidVal)
    Try
        svc.DeleteWebPart(pageUrl, storageKey, WebpartpagesSvc.Storage.Shared)
            Console.WriteLine("Part [{0}] has been deleted from page [{1}]" + ControlChars.Lf, guidVal, pageUrl)
        Catch er As Exception
            Console.WriteLine("Error: Part [{0}] was not deleted." + ControlChars.Lf + " {1}", guidVal, er.Message)
    End Try
End Sub 'DeleteWebPart
private void DeleteWebPart (string guidVal)
{
    // 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";

    // The GUID represents the Web Part ID being deleted.
    Guid storageKey = new Guid(guidVal);  
    try 
    {
        svc.DeleteWebPart(pageUrl, storageKey, WebpartpagesSvc.Storage.Shared);
        Console.WriteLine("Part [{0}] has been deleted from page [{1}]\n", guidVal, pageUrl);
    }
    catch(Exception er)
    {
        Console.WriteLine("Error: Part [{0}] was not deleted.\n {1}", guidVal, er.Message);
    }
}

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/DeleteWebPart"

<?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>
    <DeleteWebPart xmlns="http://microsoft.com/sharepoint/webpartpages">
      <pageUrl>string</pageUrl>
      <storageKey>guid</storageKey>
      <storage>None or Personal or Shared</storage>
    </DeleteWebPart>
  </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>
    <DeleteWebPartResponse xmlns="http://microsoft.com/sharepoint/webpartpages" />
  </soap:Body>
</soap:Envelope>

See Also

Reference

WebPartPagesWebService Class
WebPartPagesWebService Members
Web Part Pages Web Service