Kumar K

Kumar K

  • NA
  • 6
  • 1k

SharePoint 2013 Timer- Remove user from SharePoint group not working

Feb 16 2021 7:07 AM

I have written a SP2013 timer job, which should remove user from SharePoint group when triggered. Written below piece of code in CustomTimerJob.cs. It updates the list item status to Active/Expired successfully on running the timer, but doesn't remove user from SharePoint group. Can let me know where I am wrong ?

 
  1. using Microsoft.SharePoint;  
  2.   
  3. using Microsoft.SharePoint.Administration;  
  4.   
  5. using System;  
  6.   
  7. using System.Collections.Generic;  
  8.   
  9. using System.Linq;  
  10.   
  11. using System.Text;  
  12.   
  13. using System.Threading.Tasks;  
  14.   
  15. namespace TimerJobApplication  
  16.   
  17. {  
  18. public class CustomTimerJob : SPJobDefinition  
  19.  {  
  20.      public CustomTimerJob() : base() { }  
  21.      public CustomTimerJob(string jobName, SPService service)  
  22.          : base(jobName, service, null, SPJobLockType.None)  
  23.      {  
  24.          this.Title = "Access Rights Timer";  
  25.      }  
  26.      public CustomTimerJob(string jobName, SPWebApplication webapp)  
  27.          : base(jobName, webapp, null, SPJobLockType.ContentDatabase)  
  28.      {  
  29.          this.Title = "Access Rights Timer";  
  30.      }  
  31.          
  32.      public void RemoveUser(String userLoginName, string groupName, string SiteURL)  
  33.      {  
  34.          SPSecurity.RunWithElevatedPrivileges(delegate()  
  35.          {  
  36.              using (SPSite site = new SPSite(SiteURL))  
  37.              {  
  38.                  using (SPWeb web = site.OpenWeb())  
  39.                  {  
  40.                      SPGroup group = web.SiteGroups[groupName];  
  41.                      SPUser userToRemove = web.EnsureUser(userLoginName);  
  42.                      group.RemoveUser(userToRemove);  
  43.                      group.Update();  
  44.                  }  
  45.              }  
  46.          });  
  47.      }  
  48.    
  49.      public override void Execute(Guid targetInstanceId)  
  50.      {  
  51.          var currentdate=DateTime.Now.Date;  
  52.          SPWebApplication webApp = this.Parent as SPWebApplication;  
  53.          SPList taskList = webApp.Sites[0].AllWebs["dev"].Lists["Limited_Access_To_Security_Groups"];  
  54.          SPView view = taskList.Views["All Items"];   //custom view name  
  55.          SPListItemCollection olistitems = taskList.GetItems(view);  
  56.          foreach (SPListItem item in olistitems)  
  57.          {  
  58.              if (Convert.ToDateTime(item["ExpiryDate"]) <= DateTime.Now.Date)  
  59.              {  
  60.                 RemoveUser(item["UserName"].ToString(), "Dev Visitors""http://devsite/");  
  61.                  item["Status"] = "Expired";  
  62.                  item.Update();                                   
  63.    
  64.              }  
  65.              else  
  66.              {  
  67.                  item["Status"] = "Active";  
  68.                  item.Update();  
  69.    
  70.              }  
  71.          }  
  72.      }  
  73.  } 
 

Answers (1)