Recently, I ran into a very strange SharePoint Discussion Board bug: if you delete the discussion topic (the thread root message), SharePoint will tell 'Cannot complete this action. Please try again'. However, the topic did get deleted in spite of the error message.
Digging deeper into this, I found out that:
- the bug happens only if the discussion board resides on the sub-site, or the site collection under 'sites' folder. It does not happens on the root site of the root site collection.
- it happens only when you click on a discussion topic to view the thread (Flat or Threaded view) -> then click the root message's 'View Properties' button -> then click 'Delete Item' toolbar button.
- it does not happen if you choose to delete the topic from the context menu; or if you enter 'Properties' screen from 'View Item' context menu.
- it happens only when deleting the discussion root topic. Deleting the relies does not cause trouble.
Checking the URL carefully, I figured out the reason: the problem is because after deleting, it tries to redirect to the thread view (as specified by the 'Source' query string parameter). However, as the topic has been deleted, there's no place to go back. It is a redirecting problem, not a deleting problem.
The purpose is simple: if it is a discussion root, cleanup the URL. SharePoint will redirect to the list's default view (often AllItems.aspx) if it does not find the 'Source' query string parameter.
The script assume that 'ID' is the first query string parameter. It works on our environments. If you want a solution which does not depend on the order of the query string parameters, maybe you could try to parse the query string.