JSOM - Scheduling a list item

Hello all

Currently working on a task that requires scheduling a list item using JSOM.

I did find that this can be done in server side code with the following code

using (SPSite site = new SPSite("http://serverName:1111/"))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPList list=web.Lists["Doc Library"];
                SPListItem listItem = list.GetItemById(1);              
                ScheduledItem scheduledItem = null;
                if (ScheduledItem.IsScheduledItem(listItem))
                {
                    scheduledItem = ScheduledItem.GetScheduledItem(listItem);
                }
                else
                {
                    throw new System.ArgumentException
                      ("SPListItem must support scheduling",
                      "listItem");
                }

                DateTime startDate = new DateTime(2011, 4, 6, 22, 50, 00);
                DateTime endDate = new DateTime(2011, 4, 6, 22, 51, 00);
                scheduledItem.StartDate = startDate;
                scheduledItem.EndDate = endDate;
                scheduledItem.ListItem.Update();
                scheduledItem.Schedule();
            }
        }

I would like to do the scheduling using JSOM in a button click.

I did a search for the js equivalent for this scheduled item and found this.

SP.Publishing.ScheduledItem

But am not sure how to use this.

Need your help on this.

Thanks in advance

July 22nd, 2015 2:47am

Hi,

As a workaround, we can create a REST service in SharePint 2013 and use Server Object Model to achieve it, then call the REST service using jQuery ajax.

SharePoint 2013: Create a Custom WCF REST Service Hosted in SharePoint and Deployed in a WSP

http://social.technet.microsoft.com/wiki/contents/articles/24194.sharepoint-2013-create-a-custom-wcf-rest-service-hosted-in-sharepoint-and-deployed-in-a-wsp.aspx

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 9:25am

You should be able to use SP.Publishing.PublishingPage.GetPublishingPage method.

var ScheduledItem = SP.Publishing.PublishingPage.GetPublishingPage(listItem);
    ScheduledItem.set_StartDate(startDate);
    ScheduledItem.set_EndDate(endDate);
    ScheduledItem.Schedule(comment);

July 23rd, 2015 4:17pm

Hello Steve!!

Thanks for pointing me in the right direction :)

I did try the code and feel like am almost there, am getting some error.

Could you please let me know what is that i am missing or what is wrong in the below code?

I enabled scheduling in the library, checked out a page and kept the page in edit mode.

Executed the following lines of code in the developer toolbar console.

<script type=text/javascript src="/_layouts/15/SP.Publishing.js"></script>
<script>
function runCode() {
     var clientContext = new SP.ClientContext();
     var targetList = clientContext.get_web().get_lists().getByTitle('Pages');
     targetListItem = targetList.getItemById(_spPageContextInfo.pageItemId);
     clientContext.load(targetListItem);
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
	var schItem = SP.Publishing.PublishingPage.getPublishingPage(clientContext);
	var startDate = new Date()
	schItem.set_startDate(startDate);
	var endDate = new Date();	
	var numberOfDaysToAdd = 6;
	endDate.setDate(endDate.getDate() + numberOfDaysToAdd); 
	schItem.set_endDate(endDate);
	schItem.schedule("test of scheduling");
	clientContext.load(schItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onScheduleSucceeded), Function.createDelegate(this, this.onScheduleFailed));
}

function onQueryFailed(sender, args) {
	alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

function onScheduleSucceeded() {
	schItem.schedule("test of scheduling");
	alert("Scheduled successfully");
}

function onScheduleFailed(sender, args) {
	alert('Scheduling failed  \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>


  • Edited by bshivashankar 16 hours 25 minutes ago Added few more details
Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 7:12am

Hello Steve!!

Thanks for pointing me in the right direction :)

I did try the code and feel like am almost there, am getting some error.

Could you please let me know what is that i am missing or what is wrong in the below code?

I enabled scheduling in the library, checked out a page and kept the page in edit mode.

Executed the following lines of code in the developer toolbar console.

<script type=text/javascript src="/_layouts/15/SP.Publishing.js"></script>
<script>
function runCode() {
     var clientContext = new SP.ClientContext();
     var targetList = clientContext.get_web().get_lists().getByTitle('Pages');
     targetListItem = targetList.getItemById(_spPageContextInfo.pageItemId);
     clientContext.load(targetListItem);
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
	var schItem = SP.Publishing.PublishingPage.getPublishingPage(clientContext);
	var startDate = new Date()
	schItem.set_startDate(startDate);
	var endDate = new Date();	
	var numberOfDaysToAdd = 6;
	endDate.setDate(endDate.getDate() + numberOfDaysToAdd); 
	schItem.set_endDate(endDate);
	schItem.schedule("test of scheduling");
	clientContext.load(schItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onScheduleSucceeded), Function.createDelegate(this, this.onScheduleFailed));
}

function onQueryFailed(sender, args) {
	alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

function onScheduleSucceeded() {
	schItem.schedule("test of scheduling");
	alert("Scheduled successfully");
}

function onScheduleFailed(sender, args) {
	alert('Scheduling failed  \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
</script>


July 27th, 2015 11:09am

Here is a working example that runs in an app.

function scheduleItem() {
    context = new SP.ClientContext.get_current();

    spHostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    var ctx = new SP.ClientContext.get_current();
    parentContext = new SP.AppContextSite(ctx, spHostUrl);
    web = parentContext.get_web();
    var targetList = web.get_lists().getByTitle('Pages');
    var item = targetList.getItemById(2);
    var scheduledItem = SP.Publishing.PublishingPage.getPublishingPage(ctx, item);

    var startDate = new Date()
    var endDate = new Date();
    var numberOfDaysToAdd = 6;
    endDate.setDate(endDate.getDate() + numberOfDaysToAdd);


    ScheduledItem.set_startDate(startDate);
    ScheduledItem.set_endDate(endDate);
    ScheduledItem.schedule("scheduled");
    item.update();

    ctx.load(item);
    ctx.load(ScheduledItem);

    ctx.executeQueryAsync(function (event, obj) {
        var success = event;
    }, function (event, obj) {
        var failure = event;
    });

}

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 9:41pm

Hello Steve!!

Thanks for your help, the code is working fine :)

Regards

Shiva

--

August 6th, 2015 1:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics