Editing content

When editing a content item, you must work with its master version. To modify an item, you must perfrom the following:

  1. Get the master version.

    Get the master version of the content item. When you modify an item, you must not work on its live version.

  2. Get a temp version.

    To get a temp version of the item, check out the master version. This is the version you must modify.

  3. Update the master version.

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

  4. Update the live version.

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

This topic explains how to modify a content item by its ID. Because of the lifecycle of the content item, you can fall in the following scenarios:

You can perform these actions by using either Native API or Fluent API. Therefore, the sections below provide code snippets for both APIs.

NOTE: In the following examples, news items will be used as content. However, the code applies to the following content types as well:

Modifying an item by the ID of its master version

The following code modifies an item by the ID of its master version.

Native API

private void ModifyItemByMasterIDNativeAPI(Guid masterId)
{
    NewsManager manager = NewsManager.GetManager();
    NewsItem master = manager.GetNewsItems().Where(newsItem => newsItem.Id == masterId).FirstOrDefault();
 
    if (master != null)
    {
        //Check out the master to get a temp version.
        NewsItem temp = manager.Lifecycle.CheckOut(master) as NewsItem;
 
        //Make the modifications to the temp version.
        temp.Title = "New Title";
        temp.LastModified = DateTime.UtcNow;
        temp.Urls.Clear();
        temp.UrlName = Regex.Replace(temp.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
 
        //Checkin the temp and get the updated master version.
        //After the check in the temp version is deleted.
        master = manager.Lifecycle.CheckIn(temp) as NewsItem;
 
        manager.SaveChanges();
 
        //Publish the news item.
        var bag = new Dictionary<string, string>();
        bag.Add("ContentType", typeof(NewsItem).FullName);
        WorkflowManager.MessageWorkflow(masterId, typeof(NewsItem), null, "Publish", false, bag);
    }
}

First, you get an instance of the manager. Then, you get the master version corresponding to the ID. Then, to get a temp version of the item, you call Lifecycle.CheckOut with the master version as argument. You make all the modifications to the temp version. Then, to transfer the changes to the master version, you call Lifecycle.CheckIn with the temp version as argument. By default, when calling theCheckIn method the temp version gets deleted. Then, you call SaveChanges to persist the changes. Finally, to publish the item, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.

Fluent API

private void ModifyItemByMasterIDFluentAPI(Guid masterId)
{
    //Use the singular facade to modify the master by the master ID.
    App.WorkWith().NewsItem(masterId).CheckOut().Do(item =>
    {
        item.Title = "New Title";
        item.LastModified = DateTime.UtcNow;
        item.Urls.Clear();
        item.UrlName = Regex.Replace(item.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
    }).CheckIn().Publish().SaveChanges();
 
    //Publish the news item.
    var bag = new Dictionary<string, string>();
    bag.Add("ContentType", typeof(NewsItem).FullName);
    WorkflowManager.MessageWorkflow(masterId, typeof(NewsItem), null, "Publish", false, bag);
}

First, you get the singular facade of the master version with the specified ID. Then, to get the facade for the temp version, you call theCheckOut method. You make all the modifications in the Do method of the temp facade. Then, to transfer the changes to the master version of the item, you call CheckIn. Then, you save the changes by calling SaveChanges. Finally, to publish the news item, you call theMessageWorkflow method of the WorkflowManager class and pass the required parameters.

Modifying an item by the ID of its live version

The following code modifies an item by the ID of its live version.

Native API

private void ModifyItemByLiveIDNativeAPI(Guid liveId)
{
    NewsManager manager = NewsManager.GetManager();
    NewsItem live = manager.GetNewsItems().Where(newsItem => newsItem.Id == liveId).FirstOrDefault();
 
    if (live != null)
    {
        //Edit the item to get the master version.
        NewsItem master = manager.Lifecycle.Edit(live) as NewsItem;
 
        //Check out the master to get a temp version.
        NewsItem temp = manager.Lifecycle.CheckOut(master) as NewsItem;
 
        //Make the modifications to the temp version.
        temp.Title = "New Title";
        temp.LastModified = DateTime.UtcNow;
        temp.Urls.Clear();
        temp.UrlName = Regex.Replace(temp.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
 
        //Checkin the temp and get the updated master version.
        //After the check in the temp version is deleted.
        master = manager.Lifecycle.CheckIn(temp) as NewsItem;
 
        manager.SaveChanges();
 
        //Publish the news item.
        var bag = new Dictionary<string, string>();
        bag.Add("ContentType", typeof(NewsItem).FullName);
        WorkflowManager.MessageWorkflow(master.Id, typeof(NewsItem), null, "Publish", false, bag);
    }
}

First, you get an instance of the manager. Then, you get the live version corresponding to the ID. Then, to start editing the item and get the master version, you call Lifecycle.Edit. To get a temp version of the item, you call Lifecycle.CheckOut with the master version as argument. You make all the modifications to the temp version. To transfer the changes to the master version, you call Lifecycle.CheckIn with the temp version as argument. By default, when calling the CheckIn method the temp version gets deleted. Then, to persist the changes, you callSaveChanges. Finally, to publish the item, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.

Fluent API

private void ModifyItemByLiveIDFluentAPI(Guid liveId)
{
    //Get the live version.
    NewsItem live = App.WorkWith().NewsItems().Where(item => item.Id == liveId).Get().SingleOrDefault();
 
    if (live != null)
    {
        //Get the ID of the master version.
        Guid masterId = App.WorkWith().NewsItem().GetManager().GetMaster(live).Id;
 
        //Use the singular facade to modify the master by the master ID.
        App.WorkWith().NewsItem(masterId).CheckOut().Do(item =>
            {
                item.Title = "New Title";
                item.LastModified = DateTime.UtcNow;
                item.Urls.Clear();
                item.UrlName = Regex.Replace(item.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
            }).CheckIn().SaveChanges();
 
        //Publish the news item.
        var bag = new Dictionary<string, string>();
        bag.Add("ContentType", typeof(NewsItem).FullName);
        WorkflowManager.MessageWorkflow(masterId, typeof(NewsItem), null, "Publish", false, bag);
    }
}

First, you get the live version with the specified ID through the plural facade. Then, you get the ID of the master version of the item using the manager exposed by the singular facade. You get the singular facade of the master version. To get the facade for the temp version, you call the CheckOut method. You make all the modifications in the Do method of the temp facade. To transfer the changes to the master version of the item, you call CheckIn. Then, you save the changes by calling SaveChanges. Finally, to publish the news item, you call theMessageWorkflow method of the WorkflowManager class and pass the required parameters.

See Also

Next steps

+1-888-365-2779
sales@sitefinity.com

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