Click or drag to resize

Adding And Editing Documents

The following operations are performed when the APIDemo application is started with “/m:put” parameters:

To create document schemas and metadata fields, the Document Schema Manager service can be used. It also provides methods for retrieving, modifying and deleting existing schemas and fields. In order to manage schemas and fields, at least Library Administration permissions are required.

Similarly the Library Structure Manager can be used to create and manipulate cabinets, drawers, folder groups (also known as categories) and folders and to retrieve the library structure. For performance reasons, cabinets are always retrieved with drawers and individual drawers can be populated with folder groups and folders.

The first step to add a new document to the library, unless an offline document is added, is to create a new upload token and upload the file to the document repository. The process is similar to downloading and for larger files can be performed in chunks.

C#
 1Guid token = repositoryController.CreateUploadToken( fileSize );
 2
 3byte[] buffer = new byte[ 64 * 1024 ];
 4
 5while ( true )
 6{
 7    int read = stream.Read( buffer, 0, buffer.Length );
 8    if ( read == 0 )
 9        break;
10
11    byte[] sendBuffer;
12    if ( read == buffer.Length )
13        sendBuffer = buffer;
14    else
15    {
16        sendBuffer = new byte[ read ];
17        Array.Copy( buffer, sendBuffer, read );
18    }
19
20    repositoryController.UploadFileDataChunk( token, sendBuffer );
21}

Once the document is uploaded, the metadata values must be specified according to the selected schema. Depending on the field type, values should be specified as:

Note that the types are slightly different than the ones returned from Document Finder, where dropdown and drilldown fields are represented as DropdownFieldChoice[] and DrilldownFieldChoice[] respectively.

C#
1FieldWithValue text = new FieldWithValue();
2text.FieldId = textFieldId;
3text.Value = "hello, world";
4
5FieldWithValue number = new FieldWithValue();
6number.FieldId = numberFieldId;
7number.Value = 1.23m;
8
9FieldWithValue[] fields = new FieldWithValue[ 2 ] { text, number };

The document can be added using the Document Manager service. The upload token of the file is also passed and the uploaded file is automatically committed in the repository and queued for full text search indexing.

C#
1DocumentInfo info = new DocumentInfo();
2info.FolderId = folderId;
3info.DocumentSchemaId = documentSchemaId;
4info.DocumentName = Path.GetFileNameWithoutExtension( filePath );
5info.FieldsWithValues = fields;
6info.UploadToken = token;
7info.OriginalFileName = Path.GetFileName( filePath );
8int metadataVersionId = documentManager.AddDocumentInfo( info );

In order to modify the metadata of an existing document, the SetMetadata method of Document Manager service can be used. Pass the new document name, document schema ID and metadata field values in the same format, as when adding a document:

C#
1documentManager.SetMetadata( metadataVersionId, documentSchemaId, Path.GetFileNameWithoutExtension( filePath ),
2  fields, false, null );

Note that when updating metadata of an existing document, you don't have to include all metadata fields in the fieldWithValues parameter. Values of fields which are not included will not be modified and will be copied from the previous metadata version instead.

The new document schema can be different than the current schema of the document. In that case make sure that you include all required metadata fields which are not present in the old schema.

For more information about retrieving the metadata version ID from a search, see the chapter about Searching Documents. You can also change metadata of multiple document at once by creating a selection and using the SetMultiMetadata method.