31
May 2018

Quick & dirty way to bulk delete project sites

I had a requirement to bulk delete 2000 project sites. This used to be much easier in good old PSI days. Project Server CSOM couldn't do it and PSI support has been removed from Project Online & Project Server 2016.

To solve this problem, I wrote a script that basically simulates going to "Connected SharePoint Sites" page and click "Delete Site" button for each Project :) This is tested on Project Server 2016 only.

Here are the steps to apply this script.

1- In Chrome, go to PWA Settings -> Connected SharePoint Sites
2- Press F12 to get in developer mode
3- In Console window, copy paste the script provided at the end of this article and press Enter
4- Type DeleteSites() and press Enter
5- Watch the magic in Console :)



The JavaScript code is as following


var script = document.createElement('script');script.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js";document.getElementsByTagName('head')[0].appendChild(script);
function retrieveData(url) {
var dfd = jQuery.Deferred();
console.log('Data url is + ' + url)
jQuery.ajax({url:url,
type: "GET",
dataType: "json",
headers: {Accept: "application/json;odata=verbose" }
, success: function (data, textStatus, XmlHttpRequest) {
if (null != data.d.__next) {
retrieveData(data.d.__next).done(function (results) {
dfd.resolve(results.concat(data.d.results));
});
}
else {
dfd.resolve( data.d.results );
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('Unable to retrieve data:' + textStatus); }
});
return dfd.promise();
}
function DeleteSites() {
var url = _spPageContextInfo.siteAbsoluteUrl + "/_api/ProjectData/[en-US]/Projects()?"
+ "$filter=ProjectWorkspaceInternalUrl%20ne%20null"
+ "&$select=ProjectId,ProjectName,ProjectWorkspaceInternalUrl"
+ "&$orderby=ProjectName";
retrieveData(url).done (function (projects) {
console.log ("Retreived projects count " + projects.length);
for (var i = 0; i < projects.length; i++) {
var projectId = projects[i].ProjectId;
var projectName = projects[i].ProjectName;
console.log ("Deleting Project Site for Project: " + projectName);
$get('idOperation').value = "DeleteWeb";
$get('idProjectUID').value = projectId;
$get('idProjectName').value = projectName;
DoCallback();
}
});
};
function PJ_ServerCallback_Complete(retString, context)
{
var oReturnData = new PJServerCallBackReturnData(retString);
console.log("Return Message = " + oReturnData.Message);
}

0 comments:

Post a Comment