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

My question is on validation of dates in SharePoint… If i have two date fields – start date(& time) and end date(&time). How do i validate these, so that a person cannot pick a time in between these? For example, if someone picks friday, 15th of June, 8-11am, another wont be able to pick friday 15th of June, 8-12pm or 8:30 – 10:00am or any time in between.

This involves comparing the current column with previously entered ones, how do i do this? I just got started on sharepoint.

Thanks in anticipation and kind regards

share|improve this question

migrated from stackoverflow.com Jul 3 '12 at 14:35

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 0 down vote accepted

This isn't something which is easy using SharePoint.

The only way to validate using values from other list items is to use an EventReceiver. See Creating SharePoint 2010 Event Receivers in Visual Studio 2010

In the ItemAdding and ItemUpdating events you want to query for other items which collide with the selected datetimes.
To query for items you have to use SPList.GetItems with a CAML query something like:

<Where>
  <And>
    <Gte>
      <FieldRef Name="EndDate" />
      <Value Type="DateTime>value of entered StartDate in ISO8601 format</Value>
    </Gte>
    <Lte>
      <FieldRef Name="StartDate" />
      <Value Type="DateTime>value of entered EndDate in ISO8601 format</Value>
    </Lte>
  </And>
</Where> 

To format the dates you can use SPUtility.CreateISO8601DateTimeFromSystemDateTime

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.