DeleteWeb XML Error

Oct 27, 2013 at 10:05 PM
I tried writting a function to delete sites in my collection. However I get this response whenever I try:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.</faultstring><detail><errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">"https://hosted.compulite.ca/company/hr/b1/b2/b3" contains illegal character ':'.</errorstring><errorcode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x80131600</errorcode></detail></soap:Fault></soap:Body></soap:Envelope>
This is the function:
function deleteSite(){
  console.log(siteUrl+" - "+siteName);
  var removeConfirm = confirm('Are you sure you want to delete the "'+siteName+'" site?');
  if(removeConfirm){
    $().SPServices({
      operation:"DeleteWeb",
      url: siteUrl,
      completefunc: function (){
        alert("The site has been deleted.");
      }
    });    
  populateSites();
  }
}
Coordinator
Oct 28, 2013 at 3:54 PM
Edited Oct 28, 2013 at 3:55 PM
I just got this to work after some fiddling. In my test environment, I couldn't get the operation to work when the url had a leading or training slash. When I did, I got an error.

The first way I got it to work was to go to the parent site (though a parent of a parent, etc. ought to work as well) and run this:
$().SPServices({
  operation: "DeleteWeb",
  url: "test"
});
where "test" was a subsite of the current site. Since I wasn't allowed to use leading or training slashes, that was the only direct way for it to work.

The other approach that worked was to use the webURL parameter to change the context:
$().SPServices({
  operation: "DeleteWeb",
  webURL: "/sites/demos",
  url: "test"
});
Using this approach, I think we can make the call from anywhere in the Site Collection.

M.
Oct 28, 2013 at 4:05 PM
Hey Marc,
Let's say I have this site here: <li value="https://hosted.demo.ca/company/hr/b1/b2/b3"><span class="siteTitle">branch3</span></li>
Based on what you've stated above, if I want to delete it I'd have to do something like this:
$().SPServices({
  operation: "DeleteWeb",
  webURL: "/company/hr/b1/b2/b3",
  url: "https://hosted.demo.ca"
});
Coordinator
Oct 28, 2013 at 4:07 PM
Nope. It would be this:
$().SPServices({
  operation: "DeleteWeb",
  webURL: "/company/hr/b1/b2",
  url: "b3"
});

M.
Oct 28, 2013 at 4:36 PM
Oh man, that's going to be a little tricky. Any idea how I could get that information from one url using javascript or jquery?
Coordinator
Oct 28, 2013 at 5:14 PM
What's going to make it tricky?

M.
Oct 28, 2013 at 5:59 PM
Edited Oct 28, 2013 at 5:59 PM
Well it's a bit more work. I have to get two variables now but the url size changes depending on which site I'm trying to delete.

I have something like this that gives me a tree of all my sites: http://imgur.com/MxwTyZu

Depending on which one I click on I need to parse it's url to give me webUrl and url. I'm not really sure how to do that just yet.
Coordinator
Oct 28, 2013 at 6:28 PM
You can use the substring functions to parse things out.

You should also be able to do this:
$().SPServices({
  operation: "DeleteWeb",
  webURL: "/",
  url: "company/hr/b1/b2/b3"
});
but I haven't tested it.

M.
Oct 28, 2013 at 8:08 PM
@SimK,

Here's something I quickly threw together your specific purpose.

http://jsfiddle.net/Rgc8f/
Oct 28, 2013 at 8:16 PM
Edited Oct 28, 2013 at 8:45 PM
Hey Guys, thanks for the replies and iOnlnine247 for the code, it works perfectly.

However, I'm getting and error in my XML still:
function deleteSite(){
  var pSiteUrl = siteUrl.slice(0,siteUrl.lastIndexOf("/"));
  var pSiteName = siteUrl.slice(siteUrl.lastIndexOf("/")+1);
  console.log(pSiteUrl+" - "+pSiteName);
  var removeConfirm = confirm('Are you sure you want to delete the "'+siteName+'" site?');
  if(removeConfirm){
    $().SPServices({
      operation:"DeleteWeb",
      webURL: pSiteUrl,
      url: pSiteName,
      completefunc: function (){
        alert("The site has been deleted.");
      }
    });    
  populateSites();
  }
}
XML Post:
<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><DeleteWeb xmlns='http://schemas.microsoft.com/sharepoint/soap/'><url>b3</url></DeleteWeb></soap:Body></soap:Envelope>
XML Response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.</faultstring><detail><errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.</errorstring><errorcode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x8102006d</errorcode></detail></soap:Fault></soap:Body></soap:Envelope>
I tried refreshing the page like it suggested but that had no effect. I'm on SP 2010 Foundation
Coordinator
Oct 30, 2013 at 5:37 PM
I'm not sure what's going on here. The security validation error could be a red herring. I'd check the webURL and url options and make sure that they are valid.

M.