Real World SPServices Example: Checking Dates Against a Blackout List

I got an email from my blog this morning that asked an interesting question, and I thought that the answer might be generally of interest.

Here’s the email, cleaned up a little:

I feel glad to found your website which I can learn a lot from you. I would like to ask your opinion since I’m still new to SharePoint 2007.

I have two different Lists. In List 1 (Dates), the administrator will add dates that called are Blackout Dates.  In List 2, the user will select a date and if the date that he selected is in the Blackout Dates in List 1, the user should not be allowed to save the form and an error should pop-up say that “ This is blackout date. Please select another date”.

Do you have any ideas regarding this problem? It is possible to do in Sharepoint 2007 lists?

This is a great place to use SPServices. We can add some script to the forms for List 2 which checks against the contents of List 1 (the Blackout Dates) and acts accordingly. I’d either do the check  on the blur event from the date picker or in the PreSaveAction, depending on when we want to tell the user something is amiss.

The call to GetListItems would look something like this:

  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "List 1",
    CAMLViewFields: "<ViewFields><FieldRef Name='Blackout_0020_Date' /></ViewFields>",
    CAMLQuery: "<Eq><FieldRef Name='Blackout_x0020_Date' /><Value IncludeTimeValue='FALSE' Type='DateTime'>" + blackoutDateValue + "</Value></Eq>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
        // Do something here if there are items returned
      });
    }
  });
});

Similar Posts

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.