CreateDocument function |
These examples VB.NET and C# code import a document into a vault and return the document object.
Public Shared Function CreateDocument(repository As BCRepository, _ documentPath As [String], documentTypeName As [String], _ initialState As DocumentWorkflowFlags, propertyDefNames As String(), _ propertyValues As Object()) As BCDocument If repository Is Nothing Then Throw New ArgumentNullException("repository") End If If [String].IsNullOrEmpty(documentPath) Then Throw New ArgumentNullException("documentPath") End If Dim folderPath As [String] = System.IO.Path.GetDirectoryName(documentPath) Dim documentName As [String] = System.IO.Path.GetFileName(documentPath) Dim retDocument As BCDocument = Nothing Try Using env As BCEnvironment = repository.Environment Dim docTypeIdx = env.DocumentTypes.IndexOf(documentTypeName) If docTypeIdx < 0 Then ' Cannot find document type Return Nothing End If Dim docTypeAMID = env.DocumentTypes.GetRowAMID(docTypeIdx) Using documentType = env.GetDocumentType(docTypeAMID) ' Make sure that the destination folder exists in the vault. If Not repository.FSObjectExists(folderPath) Then ' Folder does not exist Return Nothing End If Using folder = TryCast(repository.GetFSObject(folderPath), BCFolder) Try retDocument = repository.ImportDocument(folder, documentType, initialState, Nothing) Catch e As Exception ' Cannot import document Return Nothing End Try Try repository.SavePropertyValues(retDocument, propertyDefNames, _ propertyValues, PropertyDefFlags.eSetValueNullIfPDNotFound) Catch e As Exception ' Cannot set properties Return Nothing End Try Try retDocument.DisplayName = documentName Catch e As Exception ' Cannot set DisplayName Return Nothing End Try End Using End Using Dim retDocID As String = retDocument.ID If repository.FSObjectExists(retDocID) Then retDocument = TryCast(repository.GetFSObject(retDocID), BCDocument) Else retDocument = Nothing End If End Using Catch e As Exception Return Nothing End Try Return retDocument End Function
public static BCDocument CreateDocument( BCRepository repository, String documentPath, String documentTypeName, DocumentWorkflowFlags initialState, string[] propertyDefNames, object[] propertyValues ) { if ( repository == null ) { throw new ArgumentNullException( "repository" ); } if ( String.IsNullOrEmpty( documentPath ) ) { throw new ArgumentNullException( "documentPath" ); } String folderPath = System.IO.Path.GetDirectoryName( documentPath ); String documentName = System.IO.Path.GetFileName( documentPath ); BCDocument retDocument = null; try { using ( BCEnvironment env = repository.Environment ) { var docTypeIdx = env.DocumentTypes.IndexOf( documentTypeName ); if ( docTypeIdx < 0 ) { // Cannot find document type return null; } var docTypeAMID = env.DocumentTypes.GetRowAMID( docTypeIdx ); using ( var documentType = env.GetDocumentType( docTypeAMID ) ) { // Make sure that the destination folder exists in the vault. if ( !repository.FSObjectExists( folderPath ) ) { // Folder does not exist return null; } using ( var folder = repository.GetFSObject( folderPath ) as BCFolder ) { try { retDocument = repository.ImportDocument( folder, documentType, initialState, null ); } catch ( Exception e ) { // Cannot import document return null; } try { repository.SavePropertyValues( retDocument, propertyDefNames, propertyValues, PropertyDefFlags.eSetValueNullIfPDNotFound ); } catch ( Exception e ) { // Cannot set properties return null; } try { retDocument.DisplayName = documentName; } catch ( Exception e ) { // Cannot set DisplayName return null; } } } string retDocID = retDocument.ID; if ( repository.FSObjectExists( retDocID ) ) { retDocument = repository.GetFSObject( retDocID ) as BCDocument; } else { retDocument = null; } } } catch ( Exception e ) { return null; } return retDocument; }