0

Need help understanding how Event Receivers work while referencing multiple lists.

I have a document library, called 'Emails'. On an Email object, I have a lookup field called 'Candidate'. I have another list called 'Candidates' (which 'Candidate' field looks up to). A Candidates object has an 'email address' text field.

I have setup an EmailReceived Event Receiver, and I am able to extract the desired metadata information.

What I want to achieve is that when an Email is received, look through the 'Candidates' list and find any items in the list where their 'email address' field matches the EmailReceived 'sender' field item.

If a match is found, assign the Candidate lookup ID value to the email object 'Candidate' lookup field.

So my goals are

  • A way to search through List B from within a ER of List A (doing a 'foreach' loop Im assuming, looking for a field match)
  • If match is found, update lookup field in list A with ID of matched object from list B

Thanks for any guidance!

  • 1
    Or would this maybe be better suited as a 'workflow', rather than in the Event Receiver code? – Simon Apr 21 '15 at 9:56
1

You have to search for the ID of the item in the Candidates list (filtering by sender value) and then create the lookup value.

//if you are in the event receiver to get the web use:
SPWeb web = properties.OpenWeb() 
int lookupID = GetLookupIDFromList(web, item["sender"]);
// the first parameter is the ID of the item (lookupto) the second is the label showed, see: https://msdn.microsoft.com/en-us/library/aa543857.aspx
item["Candidate"] = new SPFieldLookupValue(lookupID, item["sender"].toString());
//remember to dispose
web.Dispose()

This method get the ID of the item:

private int GetLookupIDFromList(SPWeb web, string strLookupValue)
{
    string strListName = "Candidates";
    string strLookupColumnName = "email address";
    try
    {
        SPList list = web.Lists[strListName];
        string strCAMLQuery = "<Where><Eq><FieldRef Name='" + strLookupColumnName + "' /><Value Type='Text'>" + strLookupValue + "</Value></Eq></Where>";
        SPQuery query = new SPQuery();
        query.Query = strCAMLQuery;
        query.ViewFields = string.Concat(
                                "<FieldRef Name='ID' />",
                                "<FieldRef Name='" + strLookupColumnName + "' />");
        SPListItemCollection items = list.GetItems(query);
        if (items.Count > 0)
        {
            return items[0].ID;
        }else{
            //handle item missing
            return 0;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
  • Thank you very much for the quick response, Nk! Two questions, how do I get the 'web' value which I pass into the function, and the line where I assign the item["Candidate"] with the 'new SPFieldLookupValue' it doesn't like the second parameter. It is looking for a string to go alongside the ID value. What string am I wanting to pass in here? Thanks again otherwise! Will let you know how it goes.. :) – Simon Apr 21 '15 at 10:43
  • @SimonZA updated my answer – Nk SP Apr 21 '15 at 10:47
  • I have been testing this on a test Document Library list, since my email library is on a different environment. And I have got this search working 100%, thank you! :) The only remaining question I have is that, on my demo list, I am adding the code to the 'ItemAdded' event receiver, which passes the 'property' argument we are using to get the 'web' value. On the email library I am having to use the 'EmailReceived' Event Receiver which doesnt provide a 'property' argument. Is there another way to get this? - "EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)" – Simon Apr 21 '15 at 12:31
  • get the url with properties.RelativeWebUrl and then SPSite site = new SPSite(properties.RelativeWebUrl); SPWeb web = site.OpenWeb(); – Nk SP Apr 21 '15 at 12:36
  • Think I found it. As mentioned I dont have a 'properties' value to pull anything from within this receiver, but I found that the 'list' value has a '.ParentWeb' property ("SPWeb web = list.ParentWeb" ). Going to try that and let you know.. :) – Simon Apr 21 '15 at 12:59

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.