現在、私はリスト項目の値を取得し、このリスト( "電話番号"列の値)に値が存在するかどうかを確認するコードを持っていますHTML形式でsugmittedします。このHTMLフォームを使用してリストに入力するレコードに、既にリストにある電話番号が含まれている場合、そのレコードは追加されません。これはリストの最初の項目ではうまくいきますが、別の項目が別の電話番号でリストに追加された場合、コードは2番目のレコードの電話番号を受け取っていないように見えるので、3番目のレコードが検証が行われない2番目のレコードと同じ電話番号、コードは最初のレコードを調べ続けます。ここに私のコードの一覧は以下のとおりです。単純なリスト項目の検証(重複防止)
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("
<script type=text/javascript>window.frameElement.commonModalDialogClose(1, 1);</script>");
//this.Page.Response.Write("Submitted!"); //replacement for the above javascript
this.Page.Response.End();
}
}
catch (Exception doh)
{
DisplayError(doh);
}
}
}
});
私は電話番号のレコードをexisitingをチェックするリスト項目をthrought反復するfoorループを使用して考えています。私は、(TextBox3.Text!= valuePhonenumber) を { } のコードを上記のコードに示していますが、コードを壊さずにこれを達成する方法についてはわかりません。もし誰かが私にこれを手伝ってもらえると大変感謝しています!
ありがとうございます。
更新!!!
私は今caml queryを使用してリストに必要な値を照会しています。この場合、HTMLフォームにTextBox3.Textに入力した値を照会します。 qqueryの結果は、オブジェクト "listItemsCollection"に格納されます。次に、これを使用してチェックを行います。「TextBox3.text」の値が「listItemsCollection」に格納されている値と等しくない場合、レコードがリストに追加されます。等しい場合はレコードが追加されません。コードは以下の通りである:
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 = "<Where><Eq><FieldRef Name='Phone number'/>" + "<Value Type='Text'>"
+ TextBox3.Text + "</Value></Eq></Where>";
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("<script
type=text/javascript>window.frameElement.commonModalDialogClose(1, 1);</script>");
//this.Page.Response.Write("Submitted!"); //replacement for the above javascript
this.Page.Response.End();
}
catch (Exception doh)
{
DisplayError(doh);
}
}
}
});
を私はとてもシンプルであるべき何かwhith苦労しているように見える理由である前に、CAMLであまり行っていません。この功績を得るためのあらゆるsujestionsは大いに評価されるでしょう!
事前