1

Hi I am trying to check if the a value exist from lost A in list B and based on my logic a method need to execute.

SPListItemCollection PersonalCalendar_items = spWeb.Lists["PersonalCalendar"].Items;

SPListItem NewPersonalCalendar_Item = PersonalCalendar_items.Add();

                    if (NewPersonalCalendar_Item["OldID"] ==null)
                    {
                        AddEventToCalendar(spWeb, LearningCalendar_Item, PersonalCalendar_items, NewPersonalCalendar_Item);
                    }
                    else
                    {
                        bool iExists = false;

                        foreach (SPListItem PersonalCalendaritem in PersonalCalendar_items)
                        {
                            //string one = LearningCalendar_Item["ID"].ToString();
                            //string two = PersonalCalendaritem["OldID"].ToString();

                            if (PersonalCalendaritem["Title"].ToString() == LearningCalendar_Item["Title"].ToString() || PersonalCalendaritem["Start Time"] == LearningCalendar_Item["Title"])
                            {
                                iExists = true;
                            }
                        }

                        if (iExists != true)
                        {
                            Context.Response.Redirect(SPContext.Current.Web.Url.ToString() + "/Pages/already-enroll.aspx");

                        }

1 Answer 1

2

It would be better to use the caml query to check if the item exists. Try changing your else branch to this:

    bool iExists = false;
    string queryString = @"<Query>
         <Where>
          <And>
           <Eq>
            <FieldRef Name='Title' />
            <Value Type='Text'>{0}</Value>
           </Eq>
           <Eq>
            <FieldRef Name='Start_x0020_Time' />
            <Value Type='DateTime'>{1}</Value>  
           </Eq>
          </And>
         </Where>
        </Query>";

    SPList list = spWeb.Lists["PersonalCalendar"];
    SPQuery query = new SPQuery();
    query.Query = string.Format(queryString, LearningCalendar_Item["Title"].ToString(), LearningCalendar_Item["YourDateColumn"]);
    SPListItemCollection listItems = list.GetItems(query);
    if (listItems.Count != 0)
    {
        iExists = true;     
    }

    if (iExists != true)
    {
        Context.Response.Redirect(SPContext.Current.Web.Url.ToString() + "/Pages/already-enroll.aspx");
    }

In your example you compared both values with the Title field, so I guess that should be changed (since one of the columns has name "Start Time" it should probably be compared with some DateTime variable). You should replace the LearningCalendar_Item["YourDateColumn"] in my example with your appropriate variable.

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.