Create documents
When creating a document, you must perform the following:
-
Check whether the document already exists*.*Before you create the document, you must check whether a document with the same
IDalready exists. -
Create the document.
If the document does not exist, you can create a new document with the specifiedID. -
Specify the parent document library of the document.
Specify the library where the document belongs to. -
Set the required properties.
When creating a new document, set the following properties:TitleLastModifiedDateCreatedUrlNameMediaFileUrlNameYou can also set any other properties in this step.
-
Upload the document data.
Upload the document data to the database. -
Save the document.
Save all changes that you have made to the document. -
Publish the document.
You publish the document using the workflow manager.
The example below shows you how to create a document with predefined ID.
NOTE: The
IDargument is assigned to the master version of the document. For more information about the different versions of a document, see For developers: Content lifecycle.
EXAMPLE: Creating a document with predefined ID.
The following code creates a document with the specifiedID,Titleand document data.
Native API ```C# using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using Telerik.Sitefinity.Libraries.Model; using Telerik.Sitefinity.Modules.Libraries; using Telerik.Sitefinity.Workflow;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.DocumentsAndFiles.ManagingDocuments { public partial class DocumentsSnippets { public static void CreateDocumentNativeAPI(Guid masterDocumentId, Guid parentDocumentLibraryId, string documentTitle, Stream documentStream, string documentFileName, string documentExtension) { LibrariesManager librariesManager = LibrariesManager.GetManager(); Document document = librariesManager.GetDocuments().Where(d => d.Id == masterDocumentId).FirstOrDefault();
if (document == null)
{
//The document is created as master. The masterDocumentId is assigned to the master version.
document = librariesManager.CreateDocument(masterDocumentId);
//Set the parent document library.
DocumentLibrary documentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.Id == parentDocumentLibraryId).SingleOrDefault();
document.Parent = documentLibrary;
//Set the properties of the document.
document.Title = documentTitle;
document.DateCreated = DateTime.UtcNow;
document.PublicationDate = DateTime.UtcNow;
document.LastModified = DateTime.UtcNow;
document.UrlName = Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
document.MediaFileUrlName = Regex.Replace(documentFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
//Recompiles and validates the url of the document.
librariesManager.RecompileAndValidateUrls(document);
//Upload the document file.
librariesManager.Upload(document, documentStream, documentExtension);
//Save the changes.
librariesManager.SaveChanges();
//Publish the DocumentLibraries item. The live version acquires new ID.
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. You check whether a document with the same `ID` already exists. Then, to create the document, you must call the `CreateDocument` method of the `LibrariesManager` class. You can create a document with either predefined or auto-generated `ID` depending which overload of the method you use. The method returns the master version of the document. Then, you get an instance of the specified parent document library. To associate the document with the document library, you set the document library to the `Parent` property of the `document` object. Then, you set the properties of the master version. We recommend to set at least the following properties: `Title`, `UrlName`, `LastModified`, `PublicationDate`, `DateCreated`, `MediaFileUrlName`. Then, to upload the document data, you call the `Upload` method of the `LibrariesManager` class and pass the stream of the selected file as parameter. To save the changes, you call the `SaveChanges` method of the manager. Finally, to publish the document, you call the `MessageWorkflow` method of the `WorkflowManager` class and pass the required parameters.
**Fluent API**
```C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Workflow;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.DocumentsAndFiles.ManagingDocuments
{
public partial class DocumentsSnippets
{
public static Guid CreateDocumentFluentAPI(Guid documentId, Guid parentDocumentLibraryId, string documentTitle, Stream documentStream, string documentFileName, string documentExtension)
{
//Check whether the parent DocumentLibrary exists.
var count = 0;
App.WorkWith().DocumentLibraries().Where(i => i.Id == parentDocumentLibraryId).Count(out count);
if (count > 0)
{
//The document is created as master. The masterDocumentId is assigned to the master version.
App.WorkWith().DocumentLibrary(parentDocumentLibraryId).CreateDocument()
.Do(document =>
{
//Set the properties.
documentId = document.Id;
document.Title = documentTitle;
document.DateCreated = DateTime.UtcNow;
document.PublicationDate = DateTime.UtcNow;
document.LastModified = DateTime.UtcNow;
document.Urls.Clear();
document.UrlName = Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
document.MediaFileUrlName = Regex.Replace(documentFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
}).CheckOut()
//Upload the document stream.
.UploadContent(documentStream, documentExtension)
//Save the changes.
.CheckIn().Publish().SaveChanges();
}
return documentId;
}
}
}
First, you check whether a document with the same ID already exists. Then, you check whether the specified parent document library exists. If it exists you get the singular document library facade of the parent document library. To create the document, you call the CreateDocument method of the facade. The ID argument is assigned to the ID property of the master version of the item. You check out the item. Then, you set the properties of the document by calling the Do method of the facade. To upload the document data, you call the UploadContent method of the facade and pass the document, the stream of the selected file, and its extension as arguments. To check in the document, you call the CheckIn method of the facade. Then, you save the changes. Finally, to publish the document, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.