C# 简单列表项验证(防止重复)

C# 简单列表项验证(防止重复),c#,list,sharepoint-2010,C#,List,Sharepoint 2010,您好,目前我有代码获取列表项的值,并检查此列表中的值电话号码列中的值是否与HTML表单建议的值一致。如果通过此HTML表单输入列表的记录包含列表中已有的电话号码,则不会添加该记录。这对于列表中的第一项很有效,但是,当使用不同的电话号码将另一项添加到列表中时,代码似乎不会拾取第二条记录的电话号码,因此如果使用与第二条记录相同的电话号码输入第三条记录,则不会进行验证,代码继续查看第一条记录。下面是我的代码列表: 我正在考虑使用foor循环来迭代列表项,以检查是否存在电话号码记录。我正在考虑把这个 如

您好,目前我有代码获取列表项的值,并检查此列表中的值电话号码列中的值是否与HTML表单建议的值一致。如果通过此HTML表单输入列表的记录包含列表中已有的电话号码,则不会添加该记录。这对于列表中的第一项很有效,但是,当使用不同的电话号码将另一项添加到列表中时,代码似乎不会拾取第二条记录的电话号码,因此如果使用与第二条记录相同的电话号码输入第三条记录,则不会进行验证,代码继续查看第一条记录。下面是我的代码列表:

我正在考虑使用foor循环来迭代列表项,以检查是否存在电话号码记录。我正在考虑把这个 如果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做过太多的工作,这就是为什么我似乎在努力做一些简单的事情。我们将非常感谢任何能让这项工作顺利进行的努力


非常感谢您使用CAML查询可以实现这一点。您只需创建一个查询,其中电话号码等于或包含来自HTML表单的输入,具体取决于您的需求。执行查询时,将返回结果集SPListItemCollection。如果此结果集已包含一个项,则您知道它是重复项,并且不添加新项。如果尚未使用CAML查询,请参阅本文:


我最终发现了问题所在,在阅读了一些关于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\u x0020\u 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;
   }
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;
   }