-1

I have got two lists. I must do synchronize this list, but one have got only 3 columns, and secound list have got 3 columns too, but have got more columns with hidden value. I done synchronizing adding, but I must do ItemUpdated to. Because if i change a value in 3 columns in first list it should change in secaund list. Adding was done by this code Event Receiver:

   public override void ItemAdded(SPItemEventProperties properties)
    {
        this.EventFiringEnabled = false;
        base.ItemAdded(properties);
        if (properties.List.Title == "Jawna")
        {
            using (SPSite site = new SPSite(properties.WebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lstOtherList = properties.Web.Lists["Niejawna"];
                    SPListItem item = lstOtherList.Items.Add();
                    web.AllowUnsafeUpdates = true;
                    item["Title"] = properties.AfterProperties["Title"];
                    item["B"] = properties.AfterProperties["B"];
                    item["Status"] = properties.AfterProperties["Status"];
                    item.Update();

                }
            }
        }
        if (properties.List.Title == "Niejawna")
        {
            using (SPSite site = new SPSite(properties.WebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lstOtherList = properties.Web.Lists["Jawna"];
                    SPListItem item = lstOtherList.Items.Add();
                    web.AllowUnsafeUpdates = true;
                    item["Title"] = properties.AfterProperties["Title"];
                    item["B"] = properties.AfterProperties["B"];
                    item["Status"] = properties.AfterProperties["Status"];
                    item.Update();

                }
            }
        }
        this.EventFiringEnabled = true;
    }

How to done the same with items update ? I think it should be the Caml Query, or linq query. But how ? Please help me. To change only this element wchich have got 3 Colums the same as in first List.

Updated Question:

I write the code:

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        this.EventFiringEnabled = false;
        base.ItemUpdated(properties);
        if (properties.List.Title == "Jawna")
        {
            using (SPSite site = new SPSite(properties.WebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList lstOtherList = properties.Web.Lists["Niejawna"];
                    string fieldA = properties.ListItem["Title"].ToString();
                    string fieldB = properties.ListItem["B"].ToString();
                    string fieldStatus = properties.ListItem["Status"].ToString();

                    SPQuery query = new SPQuery();
                    query.Query = "<Where><Eq><FieldRef Name='Field A'/><Value Type='Text'>" + fieldA + "</Value></Eq></Where>";

                    SPListItemCollection items = lstOtherList.GetItems(query);
                    SPListItem item = items[0];

                    item["Title"] = properties.ListItem["Title"];
                    item.Update();
                }
            }
        }
        this.EventFiringEnabled = true;
    }

And I getting now this error:

One or more field types are not installed properly. Go to the list settings page to delete these fields.

  • 3
    Please update your original post instead of posting multiple questions about the same problem! – Robert Lindgren May 25 '15 at 7:48
  • I deleted my first Question on this topic. Because there is not inough sample of problem. – Grzegorz Z May 25 '15 at 8:07
2

Firstly there's no value for properties.AfterProperties in the ItemAdded event receiver. You can see in what receivers After- and Before- properties are available here.

To get a field's value use properties.ListItem['FieldName'].

To synchronize appropriate items in both lists you should have some unique field to search items. If you have Title field as a unique one you can search item using this CAML query:

var query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Searched Title</Value></Eq></Where>";
var items = list.GetItems(query);

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.