Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Tuesday, May 28, 2013

Programatically Expiring Users from Groups in MOSS 2007

Ok,  I was reviewing my posts and found this bit of code I created in my drafts; not sure why I never posted it, but here it is as is.  If you ever wanted to have a list of users that automatically removed user access after a period of time, this code will do it.  I haven't parsed through the code again, but most of this looks good for SharePoint 2010 and should work for 2013.  Let me know if anything has been deprecated and I will recreate and post an update.  The code itself is an example of the actual logic that would be placed in a timer job, looks like I never finished the solution beyond this test, but the logic is there...

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

public partial class Pull : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)

{
//Define variables and objects

SPWeb web = SPContext.Current.Web;
SPSite site = web.Site;
string siteUrl = web.Url;
SPList splExpiryList = web.Lists["User Expiration"]; //A Table I created
DataTable dtExpiryList = splExpiryList.Items.GetDataTable();
SPGroup grp = web.Groups[1];int ID=0;
string username = "";
DataTable dtExpired = new DataTable();

//Fields Used in the Table I created

dtExpired.Columns.Add("ID");
dtExpired.Columns.Add("User");
dtExpired.Columns.Add("Group");

foreach (DataRow drExpiry in dtExpiryList.Rows)
{ //"Expiry_x0020_Date" is how sharepoint stores the field "Expiry Date" because there is a space
if (DateTime.Parse(drExpiry["Expiry_x0020_Date"].ToString()) == DateTime.Now)

//Compare the expiry date to current {
username = drExpiry["User"].ToString();

splExpiryList.Items.GetItemById(int.Parse(drExpiry["ID"].ToString()));

ID = int.Parse(drExpiry["ID"].ToString());

try //Let the User know what is being Deleted
{ grp = web.Groups[drExpiry["Group"].ToString()];
Response.Write("Identified and will affect " + drExpiry["Group"].ToString() + " As the Group containing " + drExpiry["User"].ToString() + " to be deleted. . .");
}
catch (Exception ex)
{ Response.Write(ex.Source + "
"
+ ex.Message + "
Stack Trace
"
+ ex.StackTrace + "
+ ex.HelpLink + "\">Help
"); }
try //Create the Expired Users Table
{ DataRow drExpired = dtExpired.NewRow();
drExpired["ID"] = ID;
drExpired["User"] = username;
drExpired["Group"] = grp;
dtExpired.Rows.Add(drExpired);
dtExpired.AcceptChanges();
}

catch (Exception ex)
{ //This should never fail
Response.Write(ex.Source + "
"
+ ex.Message + "
Stack Trace
"
+ ex.StackTrace + "
+ ex.HelpLink + "\">Help

");
}
}
}
foreach (DataRow row in dtExpired.Rows) //Delete Each Expired Item in the table
{ grp = web.Groups[row["Group"].ToString()];
foreach (SPUser user in grp.Users)
{if (user.Name == row["User"].ToString()) //Verify the User exists in the Group
{ web.AllowUnsafeUpdates = true; //Temporarily Allow unsafe updates
Response.Write("Deleting item " + row["ID"].ToString() + " from " + splExpiryList.Title + " . . .");

try
{ splExpiryList.Items.DeleteItemById(int.Parse(row["ID"].ToString())); //Delete the List Item
Response.Write("Success!");
}
catch (Exception ex)
{ Response.Write("Failed!
"
+ ex.Source + "
"
+ ex.Message + "
Stack Trace
"
+ ex.StackTrace + "
+ ex.HelpLink + "\">Help

"); }
 
Response.Write("Deleting user " + user.Name + " from " + grp.Name + " . . .");

try
{ grp.RemoveUser(user); // Remove the user from the Group
Response.Write("Success!");
}
catch (Exception ex)
{
Response.Write("Failed!" + ex.Source + " " + ex.Message + "Stack Trace" + ex.StackTrace + " " + ex.HelpLink + "\">Help");
} 
splExpiryList.Update(); //Update the List
grp.Update(); //Update the Group
web.AllowUnsafeUpdates = false; //Turn off Unsafe Updates

}
else
{
Response.Write("Error! " + user.Name + " not found in group " + grp.Name + "");
}
}
}
 
//Dispose objects dtExpired.Dispose();
dtExpiryList.Dispose();
web.Dispose();
site.Dispose();
}
}


Sorry if this interrupts my governance, but I think this might be useful to someone out there.