0

I'm having so much trouble with this one. How can I populate List A with the most recent date from List B? List A shouldn't have duplicate names.

List A (NAME / DATE)

  1. CARL / 2-21-18
  2. TOM / 2-20-18
  3. NICOLE / 2-19-18

List B (NAME / DATE)

  1. CARL / 2-21-18
  2. CARL / 2-18-18
  3. TOM / 2-16-18
  4. TOM / 2-20-18
  5. NICOLE / 2-19-18 ...

I am using sharepoint online w/ modern experience, and I have access to Sharepoint Designer

1
  • Using what method? A workflow? A script? A web app?
    – sasfrog
    Feb 22, 2018 at 3:41

1 Answer 1

0

You could use scheduled job to achieve this (either creating a Custom Timer Job, or simple Task Scheduler), and run it based on the preferred interval (every x minutes for example).

The logic inside is something like:

using(SPSite site = new SPSite("https://yourspsite"))
{
    using(SPWeb web = site.OpenWeb())
    {
        SPList listA = web.Lists["listA"];
        SPList listB = web.Lists["listB"];

        // Assuming list A users are unique (based on your requirement)
        SPListItemCollection usersOnListA = listA.GetItems();
        SPListItemCollection usersOnListB;
        SPQuery query = new SPQuery();
        query.RowLimit = 1
        foreach(SPListItem userListA in usersOnListA)
        {
            // Get user on list B whose name is equal to user on list A, ordered descending by its date
            query.Query = @"<Where>
                               <Eq>
                                   <FieldRef Name='Employee_x0020_Name' />
                                   <Value Type='Text'>"+ userListA["Emplloyee_x0020_Name"].ToString() +@"</Value>
                               </Eq>
                          </Where>
                          <OrderBy>
                              <FieldRef Name='Date' Ascending='FALSE' />
                          </OrderBy>";
            usersOnListB = listB.GetItems(query);
            if(usersOnListB != null && usersOnListB.Count > 0)
            {
                // update date column in listA
                userListA["Date"] = DateTime.Parse(usersOnListB[0]["Date"].ToString());
                userListA.Update();
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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