0

I have console application collecting data from SQL and either creates or updates the item in a SP list.

I have added an event receiver to that list, this triggers if an item has been updated and finds a correlating item in another list and make changes to it.

The problem is that the even receiver is triggered, but not fully executed; it seems it is not able to get the list to modify. The event receiver works fine if I change an item manually.

It's a very simple event receiver, here is the code:

public override void ItemUpdated(SPItemEventProperties properties)
{
  try
  {
    base.ItemUpdated(properties);
    this.EventFiringEnabled = false;

    SPSecurity.RunWithElevatedPrivileges(delegate(){

      SPUserToken token = properties.ListItem.Web.Site.SystemAccount.UserToken;
      using(SPSite site = new SPSite(properties.SiteId, token)){

        using(SPWeb web = site.OpenWeb(properties.Web.ID)){
          try
          {
            SPListItem listItem = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID);

            string contactlisturl = web.ServerRelativeUrl + "/lists/Contacts";
            SPList contactlist = web.GetList(contactlisturl);
            SPListItemCollection contactItems = contactlist.GetItems();

            foreach(SPListItem item in contactItems){
              log.logToTextfile("Entered foreach");
              string id = "";
              string email = "";
              try
              {
                id = "" + GetFieldValueRegular(item, "ID");
                email = "" + GetFieldValueRegular(item, "EMail");
              }catch(Exception ex){
                log.logToTextfile(ex.ToString());
              }

              if(email.Trim() == listItem["Email"].ToString().Trim()){
                log.logToTextfile("The contact exsist");
                if (listItem["Unsubscribed"].ToString() == "True")
                {
                  item["UNSUBSCRIBED"] = true;
                }
                else
                {
                  item["UNSUBSCRIBED"] = false;
                }
              }
              item.Update();
            }
          }catch(Exception ex){
            log.logToTextfile(ex.ToString());
          }
        }
      }
    });
  }
  catch (Exception ex)
  {
    log.logToTextfile(ex.ToString());
  }
}

As I have mentioned the event receiver works fine if I do any changes to an item manually, but when triggered from the console application it seems unable to get the list and items:

        string contactlisturl = web.ServerRelativeUrl + "/lists/Contacts";
        SPList contactlist = web.GetList(contactlisturl);
        SPListItemCollection contactItems = contactlist.GetItems();

and it does not enter the foreach. The value of the item triggering the event receiver is listed in my log, so it's not Null. I am not getting any exceptions either.

Anyone have a clue why this happends? and what I may do wrong?

  • try to use trygetlist instead of getlist. ( intellisence should Show the function) – Raoul Mensink Apr 12 '16 at 10:08
  • Updated the event receiver to use SPList contactlist = web.Lists.TryGetList("contacts"); and added a check for if it was found, same for SPListItemCollection. They both are found and not null. But it does not enter the foreach… strange, since it works if I do it manually. – CronbachAlpha Apr 12 '16 at 10:30
  • can you go into debug mode and see if it is a list with items? calling if its null does not verify this. Might also be better to get a ´List<SpListItem>´ instead of a collection. (direct console is also a great tool to use during debugging) – Raoul Mensink Apr 12 '16 at 10:49
  • I managed to solve it! I had to change the xml to use <SequenceNumber>10001</SequenceNumber><Synchronization>Synchronous</Synchronization>. But thanks for helping, much appreciated! – CronbachAlpha Apr 12 '16 at 11:28
  • add your solution as an anwser so that you can help others :) – Raoul Mensink Apr 12 '16 at 12:47
1

I managed to solve this by adding SequenceNumber and Synchronization in the Elements.xml file of the event receiver:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="Lists/members">
      <Receiver>
        <Name>MembersItemUpdated</Name>
        <Type>ItemUpdated</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Members.Members</Class>
        <SequenceNumber>10001</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>

When adding xml tags to the Element.xml an update of the wsp is not sufficient. You either have to reinstall the wsp or update the event receiver with powershell. I went with the powershell version like so:

Add-PSSnapin Microsoft.SharePoint.PowerShell

$web = Get-SPWeb "site url"

$a = $web.Lists.TryGetList("List name").Eventreceivers

Since I only had one event receiver in this list I could get the first from the array like so:

$a = $a[0] (or assign it to a new variable)
$a.Synchronization = "Synchronous"
$a.SequenceNumber = 10001
$a.Update()

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.