Modifying documents

When editing a document, you must work with its master version. To modify a document, you must perform the following:

  1. Get the master version.

    Get the master version of the document. When you modify a document, you must not work on its live version.

  2. Get a temp version.

    Check out the master version to get a temp version of the document.

  3. Modify the temp version.

    Set the properties of the temp version to the new values. You can also upload new document data.

  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.

The example below shows you how to modify a document by the ID of its master version. For more information about working with the IDof the live version, see Editing content in Content lifecycle.

Modifying a document by the ID of its master version

The following code modifies a document by the ID of its master version.

Native API

private void ModifyDocumentNativeAPI(Guid masterDocumentId, string newTitle, Stream documentStream, string documentExtension)
{
    LibrariesManager librariesManager = LibrariesManager.GetManager();
 
    //Get the master version.
    Document master = librariesManager.GetDocuments().Where(d => d.Id == masterDocumentId).FirstOrDefault();
 
    if (master != null)
    {
        //Check out the master to get a temp version.
        Document temp = librariesManager.Lifecycle.CheckOut(master) as Document;
 
        //Make the modifications to the temp version.
        temp.Title = newTitle;
        temp.LastModified = DateTime.UtcNow;
        temp.Urls.Clear();
        temp.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
        librariesManager.Upload(temp, documentStream, documentExtension);
 
        //Checkin the temp and get the updated master version.
        //After the check in the temp version is deleted.
        master = librariesManager.Lifecycle.CheckIn(temp) as Document;
 
        librariesManager.SaveChanges();
 
        //Publish the document.
        var bag = new Dictionary<string, string>();
        bag.Add("ContentType", typeof(Document).FullName);
        WorkflowManager.MessageWorkflow(masterDocumentId, typeof(Document), null, "Publish", false, bag);
    }
}

First, you get an instance of the LibrariesManager class. Then, you get the master version with the corresponding ID. Then, to get a temp version of the document, you call Lifecycle.CheckOut with the master version as an argument. You make all the modifications to the temp version. In this example you update the title of the document and its URL, and upload new document data. Then, to transfer the changes to the master version, you call Lifecycle.CheckIn with the temp version as an argument. By default, when calling the CheckIn method the temp version gets deleted. To persist the changes, you call SaveChanges. Finally, to publish the master version, you call theMessageWorkflow method of the WorkflowManager class and pass the required parameters.

Fluent API

private void ModifyDocumentFluentAPI(Guid masterDocumentId, string newTitle, Stream documentStream, string documentExtension)
{
    var count = 0;
 
    App.WorkWith().Documents().Where(i => i.Id == masterDocumentId).Count(out count);
 
    if (count > 0)
    {
        App.WorkWith().Document(masterDocumentId).CheckOut().Do(document =>
        {
            document.Title = newTitle;
            document.LastModified = DateTime.UtcNow;
            document.Urls.Clear();
            document.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
        }).UploadContent(documentStream, documentExtension).CheckIn().SaveChanges();
 
        //Publish the documentLibrary post.
        var bag = new Dictionary<string, string>();
        bag.Add("ContentType", typeof(Document).FullName);
        WorkflowManager.MessageWorkflow(masterDocumentId, typeof(Document), null, "Publish", false, bag);
 
    }
}

First, you check whether a document with the specified ID exists. Then, you get the singular document facade of the master version with the specified ID. Then, to get the facade for the temp version, you call the CheckOut method. You make all the modifications in the Domethod of the temp facade. In this example you update the title of the document and its URL. To upload new document data, you call theUploadContent and pass the data as an argument. Then, to transfer the changes to the master version of the item, you call CheckIn. To save the changes, you call SaveChanges. Finally, to publish the document, you call the MessageWorkflow method of theWorkflowManager class and pass the required parameters.

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