简单的列表项验证(防止重复)
本文关键字:列表 验证 简单 | 更新日期: 2023-09-27 18:08:24
嗨,目前我有代码获取列表项的值,并检查该列表中的值("电话号码"列中的值)是否存在于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);
}
}
}
});
我正在考虑使用一个底循环来遍历列表项以检查现有的电话号码记录。我想把如果(TextBox3。= valuePhonenumber){}在上面的代码中显示了一个底循环,但我不确定如何在不破坏代码的情况下实现这一点。如果有人能帮助我,我将不胜感激!
提前感谢,
更新! !
我现在使用caml查询来查询所需值的列表,在这种情况下,它的值在HTML表单上输入到TextBox3.Text。然后将qquery的结果存储在对象"listtitemscollection"中。然后,我使用这个执行检查,所以如果在"TextBox3"中的值。"text"不等于存储在"listtitemscollection"中的值,则记录被添加到列表中,如果它等于,则记录不被添加。代码如下所示:
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,这就是为什么我似乎在挣扎,应该是如此简单的东西。任何疑问得到这个工作将非常感激!
提前致谢
您可以使用CAML Query实现这一点。您只需创建一个查询,其中电话号码等于(或包含,取决于您的需求)HTML表单的输入。当执行查询时,将返回一个结果集(SPListItemCollection)。如果这个结果集已经包含了一个项目,您就知道它是重复的,不需要添加新项目。如果您还没有使用过CAML查询,请参考本文:
http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list我终于找到了问题所在,在处理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')。现在整个系统都工作了。经过这么长时间的思考,我终于明白了,所需要做的就是为列指定内部系统名称.....
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;
}