Archive

Archive for December, 2010

“The My Site of xxx is scheduled for deletion”

December 15, 2010 3 comments

We had strange issue (actually it is a feature of SharePoint) that when a user is moved out of organization (when his profile is removed by profile import) a mail is sent out to the manager of the user that ‘The My Site of xxx is scheduled for deletion’ and also it provide site collection admin access to that user’s personal site. And the issue is ‘Privacy’, user my site are personal site and should never be assigned to manager.

So, what we will attempt here is to stop

-> Stop the email sent to the manager

-> Stop the personal from being deleted

-> Stop the assigning the permission to the manager of the user

So, all the above activities are done through a Timer job ‘MySiteCleanUpJob’, which actually deletes the user profile and perform other activities including the above.  And thank fully this can be overwritten through code. Read the below link

http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.mysiteprofilehandler.aspx

So, we can write a class which inherits IProfileEventInterface and overloads the method ‘PreProfileDeleted‘.  If this method returns true then the profile will be deleted in profile store, and let’s assume we need that to happen and we will return true always.

Once the class is written, what to do next, how to plug in that. Put the following code in a feature receiver

SPService service = SPFarm.Local.Services.GetValue<SPWebService>(string.Empty);
MySiteCleanupJob job = service.JobDefinitions.GetValue<MySiteCleanupJob>(MySiteCleanupJob.DefaultJobName);
job.ProfileDeleteEventHandler = typeof(<<ClassName>>);
job.Update()

So far, everything explained is theoretical, so please try this in your test environment.  Will update the post once this is experimented.

[Update 2] New class cannot be hooked with the above specified way. It can be hooked through “stsadm -o profiledeletehandler” i could not get the equivalent powershell command.

Watch this space.. will update once deployed and tested

[Update 3] Here is the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server.Diagnostics;

namespace MyCompanyMySiteProfileHandler.MySiteProfileHandler
{
public class MyCompanyMySiteProfileHandler : IProfileEventInterface
{
public virtual bool PreProfileDeleted(UserProfile profile)
{
if (profile == null)
{
return false;
}
else
{
try
{
using (SPSite site = profile.PersonalSite)
{
if (site != null)
{
Microsoft.Office.Server.UserProfiles.MySiteProfileHandler handler = new Microsoft.Office.Server.UserProfiles.MySiteProfileHandler();
//By passing null we do not set the owner for personal site, but still schedule the site for deletion.
//this way reminder email will also not be sent, but the site will be deleted on 14th day from now
handler.SetMySiteOwner(profile, null);
}
}
}
catch(Exception oe)
{
//do something
}
}

//In any case return ture, to make sure the profiles does not remain unattended.
return true;
}

public virtual bool PreProfileDeleted(ServerContext context, UserProfile profile)
{
return true;
}

}
}