2009-03-18 6 views
2

私はいくつかの列を含むカスタムリスト定義を実装しています。列の1つの要件は、固有値のみを含むことができるということです。ユーザーが別のリスト項目に既に存在する値を入力した場合は、検証エラーが表示される必要があります。一意の値を必要とするカスタムフィールドの作成

このOOTBを実行するためのオプションはありますか?おそらく、リスト定義やカスタムフィールドタイプにありますか?または、この機能を自分で実装する必要がありますか?

答えて

3

カラム内のユニークな値を保証するには、2つの可能性があります。

  1. フィールドの型に対してValidate()メソッドをオーバーライドし、同じ値を持つアイテムのリストを照会し、それに応じてIsValidプロパティを設定できます。

  2. 独自のアイテムを作成し、リストを照会するイベント受信者を追加して更新します。

0

私はこれまで過去に以下を使用しました。

/// <summary> 
      /// Override Validation to check to see if our list already contains an item with the same value 
      /// </summary> 
      /// <param name="value"></param> 
      /// <returns></returns> 
      public override string GetValidatedString(object value) 
      { 
       // Get Current List 
       SPList thisList = ParentList; 

       SPQuery keywordQuery = new SPQuery(); 

       // Check to see if the current field contains the value entered into field 
       keywordQuery.Query = string.Format("<Where>" + 
         "<Eq>" + 
          "<FieldRef Name='{1}' />" + 
          "<Value Type='Text'>{0}</Value>" + 
         "</Eq>" + 
        "</Where>", value, InternalName); 

       SPListItemCollection liKeywords = thisList.GetItems(keywordQuery); 

       // Will return greater than 0 if it finds the value in the list 
       if (liKeywords.Count > 0) 
       { 
        // checks to see if the list item is being updated, if it is the same finds the same ID as the one being Validated 
        if (liKeywords[0].ID != SPContext.Current.ItemId) 
        { 

         // Show error message 
         throw new SPFieldValidationException(string.Format("An item called '{0}' already exists", value)); 
        } 
       } 

       // return entered value if unique 
       return base.GetValidatedString(value); 
      } 

HTHフィル

0

http://www.codeplex.com/featuresで入手可能な「一意の列方針」と呼ばれるオープンソースの機能があり、それは新しいフィールドの種類に依存を作成せずに、あなたの問題を解決します。

関連する問題