Modifying content items

Sitefinity allows you to modify a content item through the Content Blocks API.

When modifying a content item, you must perform the following:

  1. Get the master version.

    Get the master version of the content item. When editing a content item, you must work with its master version.

    For more information about querying content items, see Querying content items.

    For more information about finding specific content items, see Finding content items.

  2. Get a temp version.

    Check out the master version to get a temp version of the content item. This is the version that you must modify.

  3. Modify the temp version.

    Use the temp version to modify the content item.

  4. Update the master version.

    Check in the temp version to transfer the changes to the master version.

  5. Update the live version.

    Publish the master version to transfer the changes to the live version.

NOTE: The examples below modify a content item by the ID of its master version. For more information about working with the ID of the live version, see Editing content.

To modify a content item you can use the Native API or the Fluent API.

To modify a content item using the Native API, you must use the ContentManager class. For more information, see Modifying a content item using Native API.

To modify a content item using the Fluent API, you must use the content item facade. For more information, see Modifying a content item using Fluent API.

Modifying content items using Native API

The following example modifies the Content property of the content item with the specified ID:

public void ModifyContentItemNative(Guid masterContentItemId, string newContent)
{
    ContentManager manager = ContentManager.GetManager();
  
    // Get the master version.
    ContentItem master = manager.GetContent().Where(cI => cI.Id == masterContentItemId).FirstOrDefault();
  
    if (master != null)
    {
        // Check out the master to get a temp version.
        ContentItem temp = manager.Lifecycle.CheckOut(master) as ContentItem;
  
        // Make the modifications to the temp version.
        temp.Content = newContent;
  
        // Check in the temp and get the updated master version. 
        // After the check in the temp version is deleted.
        master = manager.Lifecycle.CheckIn(temp) as ContentItem;
  
        // Publish the master.
        manager.Lifecycle.Publish(master);
  
        manager.SaveChanges();
    }
}

First, you initialize ContentManager. Then, you use GetContent and filter based on Id to get the master version corresponding to the ID. Then, you call Lifecycle.CheckOut with the master version as argument to get a temp version of the item. You modify the content of the temp version with newContent. Then, you call Lifecycle.CheckIn with the temp version as argument to transfer the changes to the master version. By default, calling the CheckIn method deletes the temp version. Then, you call Lifecycle.Publish with the master version as argument to transfer the changes to the live version. Finally, you call SaveChanges to save all changes.

Modifying a content item using Fluent API

The following example modifies the Content property of the content item with the specified ID:

public void ModifyContentItemFluent(Guid masterContentItemId, string newContent)
{
    int count = 0;
  
    // Check whether the item exists.
    App.WorkWith().ContentItems().Where(cI => cI.Id == masterContentItemId).Count(out count);
  
    if (count > 0)
    {
        // Use the singular facade to modify the master by the master ID.
        App.WorkWith().ContentItem(masterContentItemId).CheckOut().Do(cI =>
                {
                    cI.Content = newContent;
                    cI.LastModified = DateTime.UtcNow;
                })
                .CheckIn().Publish().SaveChanges();
    }
}

First, you check whether an item with the specified ID exists. Then, you get the master version with the specified ID using the content item singular facade.

NOTE: If there is no item with the specified Id, ContentItem(contentItemId) throws an exception of type ItemNotFoundException.

To get a temp for the item, you use the CheckOut method of the content item facade. Then, you modify the content with newContent in the Do method of the temp facade. You call CheckIn to transfer the changes to the master version of the content item. Then, you call Publish to transfer the changes to the live version. Finally, you call SaveChanges to save all changes.

See Also

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK