Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Sep 25th, 2018 69 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. using System;
  2. using System.Linq;
  3. using Telerik.Sitefinity.Data;
  4. using Telerik.Sitefinity.Scheduling;
  5. using Telerik.Sitefinity.Security;
  6.  
  7. namespace SitefinityWebApp
  8. {
  9.     public class DeleteOrphanedUserProfilesTask : ScheduledTask
  10.     {
  11.         public DeleteOrphanedUserProfilesTask()
  12.         {
  13.             this.Key = TaskKey;
  14.         }
  15.  
  16.         public override void ExecuteTask()
  17.         {
  18.             var transactionName = string.Concat("DeleteOrphanProfiles", Guid.NewGuid());
  19.  
  20.             var hasDeletionOccured = false;
  21.  
  22.             var providers = UserProfileManager.ProvidersCollection;
  23.  
  24.             foreach (var provider in providers)
  25.             {
  26.                 var profileManager = UserProfileManager.GetManager(provider.Name, transactionName);
  27.  
  28.                 var profiles = profileManager.GetUserProfiles();
  29.  
  30.                 foreach (var profile in profiles)
  31.                 {
  32.                     if (profile.User == null)
  33.                     {
  34.                         profileManager.Delete(profile);
  35.  
  36.                         hasDeletionOccured = true;
  37.                     }
  38.                 }
  39.             }
  40.  
  41.             if (hasDeletionOccured)
  42.             {
  43.                 TransactionManager.CommitTransaction(transactionName);
  44.             }
  45.         }
  46.  
  47.         public static void SheduleTask(DateTime executeTime)
  48.         {
  49.             var schedulingManager = SchedulingManager.GetManager();
  50.  
  51.             var existingTask = schedulingManager.GetTaskData().FirstOrDefault(x => x.Key == TaskKey);
  52.  
  53.             if (existingTask == null)
  54.             {
  55.                 var newTask = new DeleteOrphanedUserProfilesTask()
  56.                 {
  57.                     ExecuteTime = executeTime
  58.                 };
  59.  
  60.                 schedulingManager.AddTask(newTask);
  61.             }
  62.             else
  63.             {
  64.                 existingTask.ExecuteTime = executeTime;
  65.             }
  66.  
  67.             SchedulingManager.RescheduleNextRun();
  68.             schedulingManager.SaveChanges();
  69.         }
  70.  
  71.         public override string TaskName
  72.         {
  73.             get
  74.             {
  75.                 return this.GetType().FullName;
  76.             }
  77.         }
  78.  
  79.         private static readonly string TaskKey = "8278E45E-9EBA-4EB9-9EE5-3201415A0AED";
  80.  
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top