0

We have a custom SharePoint application in which we manage user records and other info about employees in lists. We need a timer job to run through the entire records and remove the user records(list)from the SharePoint site for which a employee(user) is no longer available in the organization, .

Please guide me, how to achieve it.

  • Just to be sure; you want a TimerJob that goes through all the lists that contains information about the employee (user) and delete those entries? – user2536 Jul 6 '15 at 6:26
  • yes, and delete those entries, whom are not present in the organization as of now. – ZedProg Jul 6 '15 at 6:35
  • and you know that when you do this, you loose the information about who edited which document, if the user is not present anymore? – Gwny Jul 6 '15 at 7:45
0

First off, read this article: Create and Deploy Custom Timer Job Definition in SharePoint Programatically
It's a good beginners guid, taking you through the steps of creating a timerjob, what code goes where, and how to deploy the timerjob properly.

All the code for deleting stuff about the employee should go in the Execute method.

A good step-by-step approach would be as follows:

  1. Get all the lists storing information about the employee.
  2. Construct CAML Query to query for items about the employee for each list.
  3. Delete the found items.

Since I don't know the setup of your lists or their linking, the following code are just examples which you should change and expand into fitting with your requirements.

public override void Execute(Guid targetInstanceId)
{
    SPWebApplication webApp = this.Parent as SPWebApplication;
    SPContentDatabase contentDb = webApp.ContentDatabases[targetInstanceId];
    SPWeb rootWeb = contentDb.Sites[0].RootWeb;
    string[] listNames = new string[] { "EmployeeList1", "EmployeeList2", "EmployeeList3" };
    List<SPList> infoLists = rootWeb.Lists.Cast<SPList>().Where(list => listNames.Any(ln => ln == list.Title)).ToList();

    // If no lists are found, stop. In case someone deletes the lists.
    if (infoLists.Count <= 0)
        return;

    // CAML Query: https://msdn.microsoft.com/en-us/library/office/ms467521.aspx
    SPQuery employeeQuery = new SPQuery
    {
        Query = string.Format(
                    string.Concat("<Where>", 
                                      "<Eq>",
                                          "<FieldRef Type='Text' Name='FieldContainingEmployeeNameOrOtherIdentifier' />",
                                          "<Value>{0}</Value>",
                                      "</Eq>",
                                  "</Where>"),
                "John Doe")
    };

    // Loop through all lists and find items for employee.
    foreach (SPList list in infoLists)
    {
        SPListItemCollection employeeListItems = list.GetItems(employeeQuery);

        // Skip to next list if no info on the employee is found in the current list.
        if (employeeListItems == null || employeeListItems.Count <= 0)
            continue;

        // As long as there are items, keep deleting.
        while (employeeListItems.Count > 0)
        {
            employeeListItems[0].Delete();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.