Update Expiration date programatically

Vote:
 

I have a need to create a scheduled job that updates expiry date on contents by certain duration. I created the job and below is the code that I have for updating the expiry date.

(pageToUpdate as IVersionable).StopPublish = expToUpdate; //expToUpdate is a extended date time value 

_contentRepository.Save(pageToUpdate as IContent, SaveAction.Publish);

Funny thing is, this seems to work fine in local environment but this doesn't update the stop publish date in DXP. There is no error too. Can anyone point me to what am I missing here

#224163
Jun 12, 2020 17:53
Vote:
 

I think you need to set the access level in the save method as it is running without context.

Ex:

_contentRepository.Save(pageToUpdate as IContent, SaveAction.Publish, AccessLevel.NoAccess);

#224225
Jun 15, 2020 1:57
Vote:
 

Hi Dileep,

I used this in my ContentEvents_PublishedContent event.

var pressReleaseDetailPageVarient = contentRepository.Get<PressReleaseDetailPage>(pressReleaseDetailPage.ContentLink);
var writablePage = pressReleaseDetailPageVarient.CreateWritableClone() as PressReleaseDetailPage;
writablePage.StopPublish = LastDateParser(writablePage.Created); // LastDateParser is my method to calculate the date
contentRepository.Save(writablePage, SaveAction.Default, AccessLevel.NoAccess);
#224244
Jun 15, 2020 8:30
Vote:
 

Hi Dileep,

Gustav is correct here, I suspect the difference you're experiencing is based on whether you manually start the job or its started by the scheduler? Suprised you're not seeing anything in the logs though, it should be throwing an exception if it's failing to save.

You can either set the AccessLevel as suggested or you could set a role when the job is triggered by the scheduler. This blog post provides some more details: https://world.episerver.com/blogs/Magnus-Rahl/Dates/2010/12/Run-a-scheduled-job-as-a-specific-role/

#224262
Jun 15, 2020 15:41
Vote:
 

Thanks everyone for the details. This helped me resolve the issue.

#224291
Jun 16, 2020 1:09
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.