1

I have a SharePoint calender list which contains columns like orderID, orderdesc etc.

The data into calender list is updated from a CSV file in item added and item updated event.

I am facing an issue in item updating, if the order id is already present in calendar list, it should be updated. In my code, it is creating a new entry rather than updating. Where am I going wrong?

Here is my item updating event

 public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);
        try
        {
            SPWeb web = properties.Web;
            SPList calendarList = web.Lists.TryGetList(CalendarListTitle);
            SPQuery changeorderquery = new SPQuery();
            changeorderquery.Query =
                "<Query /><ViewFields><FieldRef Name='ChangeOrderNumber' /></ViewFields><QueryOptions />";
            SPListItemCollection changeOrderCollection = calendarList.GetItems(changeorderquery);

            StreamReader content;
            if (web.GetFile(properties.AfterUrl) != null)
            {
                SPFile file = web.GetFile(properties.AfterUrl);
                content = new StreamReader(file.OpenBinaryStream());
                var data = Parse(content);

                foreach (var fieldValues in data)
                {
                    foreach (SPListItem changeOrder in changeOrderCollection)
                    {
                        for (int i = 0; i < data.Count; ++i)
                        {
                            if (changeOrder["ChangeOrderNumber"].ToString() == data[i][0]) // if the changeorder exists
                            { 
                                var itemid = changeOrder.ID;
                                SPListItem item = calendarList.GetItemById(itemid);
                                //foreach (SPListItem item in calenderitems)
                                //{
                                item["ChangeOrderNumber"] = fieldValues[0];
                                item["Requestor"] = fieldValues[1];
                                item["ImplementationStartDate"] = fieldValues[2];
                                item["ImplementationEndDate"] = fieldValues[3];
                                item["COStatus"] = fieldValues[4];
                                item["Assignee"] = fieldValues[5];
                                item["OrderSummary"] = fieldValues[6];
                                item["OrderDescription"] = fieldValues[7];
                                item["Justification"] = fieldValues[8];
                                item["COURL"] = fieldValues[9];

                                var orderSummary = item["OrderDescription"].ToString();
                                string eventType = GetEventType(orderSummary);
                                if (eventType != null)
                                {
                                    item["EventType0"] = eventType;
                                }
                                else
                                {
                                    item["EventType0"] = "";
                                }
                                item.Update();

                            }
                             if (changeOrder["ChangeOrderNumber"].ToString() != data[i][0])
                            {
                                SPListItem item = calendarList.Items.Add();
                                item["ChangeOrderNumber"] = fieldValues[0];
                                item["Requestor"] = fieldValues[1];
                                item["ImplementationStartDate"] = fieldValues[2];
                                item["ImplementationEndDate"] = fieldValues[3];
                                item["COStatus"] = fieldValues[4];
                                item["Assignee"] = fieldValues[5];
                                item["OrderSummary"] = fieldValues[6];
                                item["OrderDescription"] = fieldValues[7];
                                item["Justification"] = fieldValues[8];
                                item["COURL"] = fieldValues[9];

                                var orderSummary = item["OrderDescription"].ToString();
                                string eventType = GetEventType(orderSummary);
                                if (eventType != null)
                                {
                                    item["EventType0"] = eventType;
                                }
                                else
                                {
                                    item["EventType0"] = "";
                                }

                                item.Update();
                            }
                            break;

                        }
                        break;
                    }
                   // break;
                }
            }
        }

        catch (Exception ex)
        {
            throw new Exception("failed to update");
        }
    }


}
1

The order of IF and For statements are confusing. Below pseduo code help :

SPListitem item = null;
SPListItemCollection coll = list.getitembyquery(query);
if(coll !=null && coll.count>0)
{
item = coll[0];
}
else
{
item = list.newitem() // just check the code to create new item
}

foreach (var fieldValues in data)
{
 for (int i = 0; i < data.Count; ++i)
 {
     item["ChangeOrderNumber"] = fieldValues[0];
                                item["Requestor"] = fieldValues[1];
                                item["ImplementationStartDate"] = fieldValues[2];
 // so no
 }
}
item.update();
  • Thanks !. I Used string match = null; in the first foreachloop and then after breaking from the inner loop, checked it for value. It solved my problem. – Deepika Mar 16 '15 at 14:00

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.