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

When the user publishes a page, I need to automatically schedule it with a start date being as soon as possible, and the end date being a year from the start date. I've tried the event receiver approach (i.e. creating a ScheduledItem and setting the dates accordingly then ListItem.Update() and ScheduledItem.Schedule()) but I get an errro to do with workflow's that I just can't seem to get around.

Does anybody know any other way I can achieve this?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Does it look like this:

        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();
            }
        }

Source from my bookmarks: http://www.c-sharpcorner.com/uploadfile/anavijai/programmatically-schedule-an-item-in-sharepoint-2010/

Perhaps if the link doesn't help can you provide more information, perhaps your code isn't quite right on the event receiver. Can you also post the error because afaik you are heading down the right path.

share|improve this answer
    
It does indeed, it looks exactly like that. What event would you advise this code be included in? I've tried "CheckingIn" and I get an error with a correlation ID (doesn't even get as far as scheduling it), and on "Updating" it appears to have all worked but when I check the page Status I see the error (next to Page Approval failed): "The workflow could not update the item, possibly because one or more columns for the items require a different type of information." Thanks a lot Hugh. –  DeeMac Aug 28 '12 at 13:39
1  
I am thinking ItemCheckedIn would be the best place also check properties.ListItem.File.Level == SPFileLevel.Published –  Hugh Wood Aug 28 '12 at 13:48
    
Thanks a lot Hugh, I'll give moving it to CheckedIn a go and report back, appreciate your reply mate thanks. –  DeeMac Aug 28 '12 at 13:54
    
It never satisfies "properties.ListItem.File.Level == SPFileLevel.Published" on CheckedIn I'm afraid Hugh. Not sure where to go from here!? –  DeeMac Aug 28 '12 at 15:15
1  
This is turning into a discussion, so all I will say is for the timer job you want to query GetPublishingPages for the pages with the correct status and enddate. If this is beyond then maybe someone else can shed some light on this strange behavior. –  Hugh Wood Aug 28 '12 at 16:47
show 5 more comments

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.