2011-09-12 13 views
0

現在、私はリスト項目の値を取得し、このリスト( "電話番号"列の値)に値が存在するかどうかを確認するコードを持っています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は大いに評価されるでしょう!

事前

答えて

0

にあなたはCAMLクエリを使用してこれを実現することができ感謝します。電話番号がHTMLフォームからの入力と同じ(または要件に応じて異なります)の照会を作成するだけです。クエリを実行すると、結果セット(SPListItemCollection)が返されます。この結果セットにアイテムがすでに含まれている場合は、アイテムが重複していることが分かり、新しいアイテムを追加しないでください。あなたはまだCAMLクエリで働いていない場合は、この記事を参照してください:

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

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')を入力するだけです。すべてのことが今働きます。すべてのこの時に壊滅的な頭の後、必要なのは、列の内部システム名でした.....

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; 
    } 
関連する問題