嗨,目前我的代码可以获取列表项的值,并检查此列表中的值(“电话号码”列中的值)是否再次存在HTML表单提供的值。如果要通过此HTML表单输入到列表中的记录包含已在列表中的电话号码,则不会添加该记录。这适用于列表中的第一个项目,但是当另一个项目添加到具有不同电话号码的列表中时,代码似乎没有获取第二个记录的电话号码,因此如果输入第三个记录与未进行验证的第二条记录相同的电话号码,代码继续查看第一条记录。这是我的代码列表:
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);
}
}
}
});
我正在考虑使用foor循环迭代列表项以检查现有的电话号码记录。我想到了 if(TextBox3.Text!= valuePhonenumber) { } 在foor循环内部的代码中显示,但我不知道如何在不破坏代码的情况下实现此目的。如果有人能帮助我,我将不胜感激!
提前致谢,
更新!!!
我现在使用caml查询在列表中查询所需的值,在这种情况下,它在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);
}
}
}
});
我之前没有做过很多关于CAML的事情,这就是为什么我似乎在努力应该如此简单的事情。任何使这项工作成功的事情都将受到极大的赞赏!
非常感谢提前
答案 0 :(得分:0)
您可以使用CAML查询来实现此目的。您只需创建一个查询,其中电话号码等于(或包含,取决于您的要求)HTML表单的输入。执行查询时,将返回结果集(SPListItemCollection)。如果此结果集已包含项目,则表示它是重复项,并且不添加新项目。如果您尚未使用CAML查询,请参阅此文章:
答案 1 :(得分:0)
我终于找到了问题所在,在做了一些阅读后,显然在处理CAML时,最好提供列表列或字段的内部系统名称。就我而言,我之前使用的是“电话号码”,这就是整个事情无法正常工作的原因。
string camlquery = @"<Where>
<Eq>
<FieldRef Name='Phone_x0020_number'/>
<Value Type='Text'>" + TextBox3.Text + @"</Value>
</Eq>
</Where>";
query.Query = camlquery;
SPListItemCollection listItemsCollection = list.GetItems(query);
if (listItemsCollection.Count == 0) // if it doesn't exist in list, we can add it
{
}
但是,只需为列表列或字段提供内部系统名称,如上所示('Phone_x0020_number')。现在整个事情都有效。在这一切破坏我的头之后,所需要的只是列的内部系统名称.....
答案 2 :(得分:0)
private bool TryGetItem(Guid key, string value, SPList list, out SPListItemCollection items)
{
SPQuery query = new SPQuery();
string @template = @"<Where>
<Eq>
<FieldRef Name='{1}'/>
<Value Type='Text'>{0}</Value>
</Eq>
</Where>";
query.Query = string.Format(@template, key.ToString("D"), value);
items = list.GetItems(query);
return items.Count > 0;
}