Update an existing page property

Using methods from IContentLoader or IContentRepository to get existing data, it will be returned as read-only version. If you try to update and save it, you will get a ReadOnly exception, although the model has the public set available

Since the API assume that most of the time are read operations, than it shares the instance object on multiple threads. This reduces the amount of short-lived objects and reduce the memory need for the website.

If you want to change programmatic the property, you need to access to a writable clone of the current instance object. Then you can change it and request to save it. The property IsReadOnly gives information about if is a read-only or cloned instance.

The following code, update a property named “MyProperty” with a new text and publish the page with the new content.

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

var contentReference = new ContentReference(23123);

var readOnlyPage = contentRepository.Get<ContentPage>(contentReference);

var clonedPageInstance = readOnlyPage.CreateWritableClone<ContentPage>();

clonedPageInstance.MyProperty = "The new value";

contentRepository.Save(clonedPageInstance, SaveAction.Publish, AccessLevel.NoAccess);

The SaveAction is an enum type of save to perform on the page data object

Member nameMember summary
NoneDo not save data.
SaveSave a page, leaving it in a checked out state.
CheckInSave and check in page, creating a new version only if necessary.
PublishPublish page, creating a new version only if necessary.
RejectReject a checked-in page.
ForceNewVersionFlag that is used to force the creation of a new version.
ForceCurrentVersionSave and check in page, always updating the current version
SkipValidationDoes not validate the data against IValidationService
DelayedPublishSave and check in page, creating a new version only if necessary and sets the content as delayed publish.
ActionMaskMask to clear Force… settings from SaveAction
GetOriginalTypeGets the Type of the current object, ensuring that the eventual type that could be generated by a proxy interceptor is ignored.

The AccessLevel determine the minimum access level that the current user must have to save the content.

Member nameMember summary
NoAccessNo access to an item
ReadRead access to an item
CreateCreate access for an item, i e create new items below this item
EditChange / create new versions of this item
DeleteDelete this item
PublishPublish/unpublish items and versions of an item
AdministerSet access rights for an item
FullAccessFull access for an item
UndefinedAccess level not defined.
GetOriginalTypeGets the Type of the current object, ensuring that the eventual type that could be generated by a proxy interceptor is ignored.

2 thoughts on “Update an existing page property

  1. One small note: when doing manipulations in some ContentEvents e.g. PublishingContent you will need to do something like this though:

    if (pageData.IsReadOnly)
    {
    pageData = pageData.CreateWritableClone();
    contentEventArgs.Content = pageData;
    }

    you can do your manipulation on pageData after that. No need of saving the content, as it is already in the process of saving it.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: