Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

Update content in code without changing publish state

Vote:
 

I'm trying to write a schedule job that scans through all content (pages and blocks) and updates certain properties. 

I'm creating a writeable clone but I'm wondering is there an easy way to save the content without creating a new version or changing the publish state. Essentially the change should just update all the current versions without changing state.

Also worth checking - at the moment I'm using IContentRepository to try loop through and get all the content in the site by getting children of each node. It doesn't feel a particularly fantastic approach so could I do this better?

I'm using CMS 9.7.2.

#155024
Sep 07, 2016 17:56
Vote:
 

Find the correct content:

1. EPiServer Find if you have it.

2. FindPageByCriteria and search by page type 

3. ContentModelUsage. Useful in some cases when you know the content type.

4. GetChildren recursive like you are doing.

Change content:

Might be enough to only change the published ones? The you can add a new validation on the properties for the rest...

Use the filter below EPiServer.Filters for published state if you go for that and then implement IValidate to ensure editors fix the rest / change automatically on publishing event.

#155027
Sep 07, 2016 18:50
Vote:
 

Hi,

Yes you can use the ForceCurrentVersion flag. E.g.:

contentRepository.Save(theWriteableClone, SaveAction.Publish | SaveAction.ForceCurrentVersion);

And for your second question, your approach should work fine. There is not really any better solution. It depends on if you really want to update all content, or just a specific content type? Then there are better solutions.

Edit: You also need to check whether the content was published, if not you need to use SaveAction.Save | SaveAction.ForceCurrentVersion. You always need to specify an "save action", but then you can combine that with e.g. ForceCurrentVersion.

#155028
Edited, Sep 07, 2016 18:52
Vote:
 

Essentially I trying to write a job that scans for external links in all pages and blocks.

  1. If the content is draft or not published it needs to be updated and stay in that state.
  2. If the content is publish it needs to be updated and stay in that state.
  3. If needs to scan all content whether published or not and whether access is restricted or not.

There seem to be more helpers for checking publish state on pages than blocks which seems to complicate things a little.

#155045
Sep 08, 2016 10:52
Vote:
 

BlockData doesn't implement all interfaces as PageData, but most are mixed in during runtime. So you can cast a block to IVersionable and then check publish status.

#155049
Sep 08, 2016 11:13
Vote:
 

Thanks.

Here's what I'm toying with currently for saving. I'm running through and testing but am expecting issues. I'm sure I saw something that said SaveAction.ForceCurrentVersion | SaveAction.Save isn't valid under version 8 for example.

                    var version = clone as IVersionable;
                    if (version != null)
                    {
                        switch (version.Status)
                        {
                            case VersionStatus.CheckedIn:
                                _contentRepository.Save(clone as IContent,
                                    SaveAction.ForceCurrentVersion | SaveAction.CheckIn);
                                break;
                            case VersionStatus.DelayedPublish:
                                _contentRepository.Save(clone as IContent,
                                    SaveAction.ForceCurrentVersion | SaveAction.DelayedPublish);
                                break;
                            case VersionStatus.Published:
                                _contentRepository.Save(clone as IContent,
                                    SaveAction.ForceCurrentVersion | SaveAction.Publish);
                                break;
                            case VersionStatus.Rejected:
                                _contentRepository.Save(clone as IContent,
                                    SaveAction.ForceCurrentVersion | SaveAction.Reject);
                                break;
                            case VersionStatus.AwaitingApproval:
                            case VersionStatus.CheckedOut:
                            case VersionStatus.NotCreated:
                            default:
                                _contentRepository.Save(clone as IContent,
                                    SaveAction.ForceCurrentVersion | SaveAction.Save);
                                break;
                        }
                    }
                    else
                    {
                        _contentRepository.Save(clone as IContent,
                            SaveAction.ForceCurrentVersion | SaveAction.Publish);
                    }

Still feel a bt nervous though. Feels like there needs to be a save action that will just update the version without changing version status.

#155050
Sep 08, 2016 12:14
* 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.