I just trying to get few information from a user (like if the mysite web exits) and working with that. The main problem is understand if the user has a mysite web and delete it if exists but to do that the only possible way seems using CSOM. Also I should create a Powershell script for that. Did anyone never do that?
1 Answer
Did you tried this yet ?
//get userProfile...
if (userProfile.PersonalSite != null)
{
using (SPSite personalSite = userProfile.PersonalSite)
{
if (System.Web.HttpContext.Current != null)
{
Microsoft.SharePoint.SPWeb web = (SPWeb)System.Web.HttpContext.Current.Items["HttpHandlerSPWeb"];
SPUtility.ValidateFormDigest();
web.AllowUnsafeUpdates = true;
personalSite.Delete();
web.AllowUnsafeUpdates = false;
}
}
}
}
You can do it through Central Admin as well as stated in this post How to delete a my site for a single user - SharePoint 2013 on premises
If you really want to use powershell then go through all profiles and find one for the given username and delete it ? e.g.
#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$site = new-object Microsoft.SharePoint.SPSite("http://skvkfm-it01/");
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
#Get UserProfileManager from the My Site Host Site context
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()
foreach($profile in $AllProfiles)
{
$DisplayName = $profile.DisplayName
$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value
#Do not delete setup (admin) account from user profiles. Please enter the account name below
if($AccountName -ne "Domain\MySiteSVApp")
{
$ProfileManager.RemoveUserProfile($AccountName);
write-host "Profile for account ", $AccountName, " has been deleted"
}
}
write-host "Finished."
$site.Dispose()