【问题标题】:Simple list items validation (prevent duplicates)简单的列表项验证(防止重复)
【发布时间】:2011-09-12 09:33:45
【问题描述】:

您好,目前我有获取列表项值的代码,并检查此列表中的值(“电话号码”列中的值)是否存在与 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);
}
}
}
});

我正在考虑使用 foo 循环遍历列表项以检查现有的电话号码记录。我想把 if (TextBox3.Text != valuePhonenumber) { } 在上面的代码中显示在一个 foo 循环内,但我不确定如何在不破坏代码的情况下实现这一点。如果有人可以帮助我,将不胜感激!

提前致谢,

更新!!!

我现在使用 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 方面做得不多,这就是为什么我似乎在为应该如此简单的事情而苦苦挣扎。任何使这项工作正常进行的 sujestion 将不胜感激!

在此先感谢

【问题讨论】:

    标签: c# list sharepoint-2010


    【解决方案1】:

    您可以使用 CAML 查询来实现此目的。您只需创建一个查询,其中电话号码等于(或包含,取决于您的要求)来自 HTML 表单的输入。执行查询时,将返回结果集 (SPListItemCollection)。如果此结果集已包含一个项目,则您知道它是重复的,因此不要添加新项目。如果您还没有使用过 CAML 查询,请参阅这篇文章:

    http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

    【讨论】:

      【解决方案2】:

      我终于找到了问题所在,显然在处理 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')。整个事情现在有效。在这一切都让我头疼之后,所需要的只是列的内部系统名称.....

      【讨论】:

        【解决方案3】:
        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;
           }
        

        【讨论】:

          猜你喜欢
          • 2016-08-17
          • 2018-11-03
          • 1970-01-01
          • 2019-06-18
          • 2020-02-22
          • 1970-01-01
          • 2012-10-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多