0

I am trying to write the Start Time and End Time of an event to a SharePoint Events list (i.e. Calendar list).

I know that the internal names of Start Time and End Time are "EventDate" and "EndDate" respectively.

I've used Date.js to format my user's date into a JavaScript Date object. I think I now need to convert from a JavaScript Date object to a type of Date that the SharePoint list understands. I can't seem to find info on what it needs to be before the list will accept it and how to format it in that way.

Right now here is my code:

function addListItem(){

    //get info to access host web
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);
    var hostweb = hostContext.get_web();

    //get list in host web
    var lstObject = hostweb.get_lists().getByTitle(paidTimeOffListName);

    //prepare an object to add a new list item.
    var listItemCreationInfo = new SP.ListItemCreationInformation();
    var newItem = lstObject.addItem(listItemCreationInfo);

    //set attributes of new list item
    var titleText = $('#inputTitle').val();
    newItem.set_item('Title', titleText);

    var locationText = $('#inputLocation').val();
    newItem.set_item('Location', locationText);

    var startDate = Date.parse("5/29/2014 8:30am"); 
    //newItem.set_item('EventDate', startDate); //Strangely the internal name for Start Time is EventDate

    var endDate = Date.parse("5/29/2014 5:30pm");
    //newItem.set_item('EndDate', endDate); //The internal name for End Time is End Date.

    var descriptionText = $('#inputDescription').val();
    newItem.set_item('Description', descriptionText);

    //TODO We need a person object for here.
    //newItem.set_item('Requester','A name for Requestor');

    //TODO This is also a person object
    //newItem.set_item('Manager','A name for Manager');

    //All requests are unapproved by default
    newItem.set_item('Approved', false);

    //write this new item to the list
    newItem.update();
    currentContext.executeQueryAsync(onListItemSuccess, onListItemFailure);

     function onListItemSuccess() {

        alert('Item created: ' + newItem.get_id());
    }

    function onListItemFailure(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
}

The ones that just involve text like newItem.set_item('Title', titleText); work fine, but when I un-comment the lines about the dates I get "Request Failed. Unknown Error" so clearly I'm doing something wrong.

  • What formats have you tried? Not sure about CSOM, but I know REST uses the following format: YYYY-MM-DDTHH:MM:SS. Perhaps try converting the date to that format? – wjervis Jun 9 '14 at 17:32
  • Oh it looks to me that I could use a function from SPServices (a jQuery library I'm already using in my system). The function is $().SPServices.SPConvertDateToISO and it is described as, "Convert a JavaScript date to the ISO 8601 format required by SharePoint to update list items." I will test this and write it up as an answer if it works as desired. – Ectropy Jun 10 '14 at 14:34
  • It looks like JavaScript also has a toISOString Method - "Returns a date as a string value in ISO format." It's possible that it may work as well. – Ectropy Jun 10 '14 at 14:42
  • have you resolved this? – Madhav Aug 22 '16 at 9:12
  • Yes, will post the format that worked for me. Stand by. – Ectropy Aug 22 '16 at 21:11
2

The format required by SharePoint is yyyy-MM-ddTHH:mm:ssZ

I modified the date via date.js ( https://github.com/abritinthebay/datejs/ ) by adding a function to it that I called .toShortISOString().

My addition looked like:

if ( !$P.toShortISOString ) {
    $P.toShortISOString = function() {
        return this.getUTCFullYear() +
        "-" + p(this.getUTCMonth() + 1) +
        "-" + p(this.getUTCDate()) +
        "T" + p(this.getUTCHours()) +
        ":" + p(this.getUTCMinutes()) +
        ":" + p(this.getUTCSeconds()) +
        //Remove the milliseconds.
        //"." + String( (this.getUTCMilliseconds()/1000).toFixed(3)).slice(2, 5) +
        "Z";
    };
}

If you're trying to add this to date.js, make sure you have the latest version from the link above, then search the file for toISOString and place the code right below the handler for toISOString.

I used it like:

var jScriptStartDate = Date.parse("6/28/2014 8:00am"); //convert to Javascript Date
var startDate = jScriptStartDate.toShortISOString(); //convert to Sharepoint ISO format (UTC with no milliseconds)
newItem.set_item('EventDate', startDate); //Strangely the internal name for Start Time is EventDate

Details here: https://stackoverflow.com/a/24184704/3680712

An even simpler way was suggested by artinthebay who said I could use date.js to format the date without editing the library at all. I haven't tested it but I assume it would also work.

var startDate = Date.parse(yourStringDate).toString("yyyy-MM-ddTHH:mm:ssZ");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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