Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm looking for a way to implement programmatically vallidation for NewListItems/EditListItems.

One way I found was: to use the ListEvents and Validate on ItemAdding / ItemEditing. But instead of a clean Errormessage I just get a Server Error Page.

public override void ItemAdding(SPItemEventProperties properties)
       {
          base.ItemAdding(properties);

          if (properties.AfterProperties["a"].ToString() == properties.AfterProperties["v"].ToString())
          {
                  properties.Status = SPEventReceiverStatus.CancelWithError;
                  properties.Cancel = true;
                  properties.ErrorMessage = "Error-Message";
          }
       }

Perhabs someone has en idea how to solve this problem. Thanks a lot.

share|improve this question

2 Answers 2

Perhaps you see server error because your web.config file is set up for enable debugging. Try to set <customErrors mode="On" /> and CallStack="false" and verify error message.
Some more info here (Enabling Debugging)

share|improve this answer
    
I've tried it, but without any success. Any other ideas? Perhabs this not best practise and there is a better way to handle list validation from code? –  MOI Mar 26 '12 at 10:48
up vote 0 down vote accepted

I've solved the problem by using custom fields.

http://msdn.microsoft.com/en-us/library/ms446361.aspx

So I have a custom field definition located under Layouts\XML. The Name has to start with "fldtypes_":

<FieldTypes>
  <FieldType>
    <Field Name="TypeName">TestFieldType</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">TestCode</Field>
    <Field Name="TypeShortDescription">Test</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="FieldTypeClass">CustomFieldTypes.TestFieldType, $SharePoint.Project.AssemblyFullName$</Field>
  </FieldType>
</FieldTypes>

FieldTypeClass references a class:

public  class TestFieldType:SPFieldText
    {



        public TestFieldType(SPFieldCollection fields, string fName): base(fields, fName)
        {

        }

        public TestFieldType(SPFieldCollection fields, string tName, string dName)
            : base(fields, tName, dName)
        {

        }


        public override string DefaultValue
        {
            get
            {
                return "ttt05";
            }
        }


        public override string GetValidatedString(object value)
        {

            if (!value.ToString().StartsWith("CP"))
            {
               throw new SPFieldValidationException("Product code must start with 'CP' for Contoso Products");
            }

            if (value.ToString().Length != 6)
            {
                throw new SPFieldValidationException("Product code must be 6 characters long");
            }

           return value.ToString().ToUpper();
        }
    }

This way can be used to validate programmatically or get access to the behaviour of controls.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.