1

I have two sharepoint lists. (“ClassNameList”) and (“Notifications”) The (“Notifications”) has three columns: “classID” which is a lookup id from (“ClassNameList”), Title, and Boolean IsEvaluated.

In the (“Notifications”) I may have one or more items related to the same classID. For example: the first row : classID “20”, Title. ”Math”, IsEvaluated “Yes”. The second row: classID. “20”, Title. ”math ”, IsEvaluated “Yes”. I need to insert a third row with the same information but this time i need to update the column IsEvaluated of the first two rows to “No” and set the third row to “Yes”. So when I display the items in a gridview, i only need to display one item that is “Yes”. I don’t want to duplicate rows in my gird. I just need the last created. Here is what im trying to do.

try  
{  
    using(SPSite site = new SPSite(SPContext.Current.Web.Url))  
    {  
        using(SPWeb web = site.OpenWeb())  
        {  
            SPList list = web.Lists.TryGetList("Notifications ");  
            if(list!=null)  
            {  
                SPListItem NewItem = list.Items.Add();  
                {  
                    web.AllowUnsafeUpdates = true;  

                    NewItem["IsEvaluated"] = false;  // How to iterate through the two rows?

                    NewItem["classID"] = id; ==> got it from query string 
                    NewItem["Title"] = txtClassName;
                    NewItem["IsEvaluated"] = true; 
                }
            }
        }  
    }  
}  
catch(Exception ex)  
{  
    Alert.Text = ex.Message.ToString();  
}

It is tricky. Please help.

2
  • its just two records or you have more than it
    – Sunil Sahu
    Commented Aug 8, 2018 at 6:50
  • Two or more. It depends. It might be one or ten.
    – Kal
    Commented Aug 8, 2018 at 13:35

2 Answers 2

1

You can put SPQuery request(CAML) before adding new item. In CAML request you can find all items that have similar 'classID' and 'Title'. And change 'IsEvaluated' to False for this items.

It is example, but you need to change SPQuery request

                        var query = new SPQuery
                        {
                            Query = String.Format("<Where><Eq><FieldRef Name='{0}' LookupId="TRUE" /><Value Type='Lookup'>{1}</Value></Eq></Where>",
                                        "classID", id)
                        };
                        var items = list.GetItems(query);

                        if (items.Count > 0)
                        {
                            foreach (SPListItem item in items){
                               item["IsEvaluated"]= false;
                               item.Update();
                            }
                        }

You need to add 'AND' statement in SPQuery. How to do it read here. It is not clear what is 'id' in your code. In my example it is integer(it is lookup id value).

0
1

Additional information based on Raf's answer:

Don't forget to execute SPListItem.Update Method to commit the changes. Otherwise, the update will not reflect in the list.

SPList list = web.Lists.TryGetList("Notifications");
if (list != null)
{
    // update items
    // build the query
    SPQuery query = new SPQuery();
    query.Query = string.Format("<Where><Eq><FieldRef Name='classID' LookupId='TRUE' /><Value Type='Lookup'>{0}</Value></Eq></Where>", id);    
    // execute the query
    SPListItemCollection items = list.GetItems(query);
    // update items with same classID
    if (items != null && items.Count > 0)
    {
        foreach (SPListItem item in items) {
            item["IsEvaluated"] = false;
            item.Update();
        }
    } 

    // new item
    SPListItem NewItem = list.Items.Add();
    {                               
        NewItem["classID"] = id;
        NewItem["Title"] = txtClassName;
        NewItem["IsEvaluated"] = true;
        NewItem.Update();
    }
}
0

Your Answer

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

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