Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple list items validation (prevent duplicates)
    primarykey
    data
    text
    <p>Hi currently I have code that get the value of of a list item and checks to see whether the value in this list (the value in a "Phone number" column) exists agains a value sugmitted by a HTML form. If the record to be entered into the list via this HTML form contains a Phone number that is already in the list, the record will not be added. This works well for the first item in the list, however when another item is added into the list with a different Phone number, the code does not seem to pick up the Phone number for the second record and so if a third record is entered with same Phone number as the second record the validation does not take place, the code keeps on looking at the first record. Here is a listing of my code:</p> <hr> <pre><code>SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(valueListURL)) { using (SPWeb web = site.OpenWeb()) { try { //--This is very important-- web.AllowUnsafeUpdates = true; SPList list = web.Lists["Contact Requests"]; SPListItemCollection collsListItems = list.Items; //Following lines of code added for validation oreach (SPListItem objListItem in list.Items) { string valuePhonenumber = objListItem["Phone number"].ToString(); string valueEmailaddress = objListItem["Email address"].ToString(); SPListItem newItem = list.Items.Add(); if (TextBox3.Text != valuePhonenumber) { newItem["Contact name"] = TextBox1.Text; TextBox1.Text = null; newItem["Company"] = TextBox2.Text; TextBox2.Text = null; newItem["Phone number"] = TextBox3.Text; this.TextBox3.Text = null; newItem["Email address"] = TextBox4.Text; TextBox4.Text = null; newItem["Best time to call you"] = TextBox5.Text; TextBox5.Text = null; newItem["Enquiry subject"] = DropDownList1.SelectedItem; this.DropDownList1.ClearSelection(); newItem["Enquiry details"] = TextBox6.Text; this.TextBox6.Text = null; if (RadioButton1.Checked) newItem["Contact method"] = Label1.Text; this.RadioButton1.Checked = false; if (RadioButton2.Checked) newItem["Contact method"] = Label2.Text; this.RadioButton2.Checked = false; newItem.Update(); } //this.Response.Redirect(Request.RawUrl); //Lines of code below used to insert or inject a javacript in order to close //modeal dialog box at the press of the button this.Page.Response.Clear(); this.Page.Response.Write(" &lt;script type=text/javascript&gt;window.frameElement.commonModalDialogClose(1, 1);&lt;/script&gt;"); //this.Page.Response.Write("Submitted!"); //replacement for the above javascript this.Page.Response.End(); } } catch (Exception doh) { DisplayError(doh); } } } }); </code></pre> <hr> <p>I am thinking of using a foor loop to iterate throught the list items to check for exisiting Phone number records. I am think of putting the if (TextBox3.Text != valuePhonenumber) { } shown in the code above inside of a foor loop, but Im not sure on how to achieve this without breaking the code. Would be much appreciated if anyone could assist me with this!</p> <p>Thanks in advance,</p> <p>Update!!!</p> <p>I am now using caml query to query the list for the required value in this case its the value entered on the HTML form into TextBox3.Text. The result of the qquery then gets stored in the object "listItemsCollection". I then use this perform a check so if the value in "TextBox3.text" is not equal to the value stored in "listItemsCollection" then the records get added to the list, if it is equal then the records does not get added. The code is listed below:</p> <pre><code>SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(valueListURL)) //using (SPSite site = new SPSite(webUrl)) { using (SPWeb web = site.OpenWeb()) { try { //added to resolve the issue with security validation on the page //--This is very important-- web.AllowUnsafeUpdates = true; SPList list = web.Lists["Contact Requests"] SPQuery query = new SPQuery(); // try and find phone number we dont want to add in list string camlquery = "&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Phone number'/&gt;" + "&lt;Value Type='Text'&gt;" + TextBox3.Text + "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;"; query.Query = camlquery; SPListItemCollection listItemsCollection = list.GetItems(query); if (TextBox3.Text != listItemsCollection.ToString()) // if it doesn't exist in list, //we can add it records { SPListItem newItem = list.Items.Add(); // add code goes here newItem["Contact name"] = TextBox1.Text; TextBox1.Text = null; newItem["Company"] = TextBox2.Text; TextBox2.Text = null; newItem["Phone number"] = TextBox3.Text; this.TextBox3.Text = null; newItem["Email address"] = TextBox4.Text; TextBox4.Text = null; newItem["Best time to call you"] = TextBox5.Text; TextBox5.Text = null; newItem["Enquiry subject"] = DropDownList1.SelectedItem; this.DropDownList1.ClearSelection(); newItem["Enquiry details"] = TextBox6.Text; this.TextBox6.Text = null; if (RadioButton1.Checked) newItem["Contact method"] = Label1.Text; this.RadioButton1.Checked = false; if (RadioButton2.Checked) newItem["Contact method"] = Label2.Text; this.RadioButton2.Checked = false; newItem.Update(); } //this.Response.Redirect(Request.RawUrl); //Lines of code below used to insert or inject a javacript in order to close //modeal dialog box at the press of the button this.Page.Response.Clear(); this.Page.Response.Write("&lt;script type=text/javascript&gt;window.frameElement.commonModalDialogClose(1, 1);&lt;/script&gt;"); //this.Page.Response.Write("Submitted!"); //replacement for the above javascript this.Page.Response.End(); } catch (Exception doh) { DisplayError(doh); } } } }); </code></pre> <p>I haven't done much with CAML before which is why I seem to be struggling whith something that should be so simple. Any sujestions to get this working will be hugely appreciated!</p> <p>Many thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload