Adding And Editing Documents

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

  • A new document schema is created.
  • A cabinet, drawer and folder are created in the library.
  • The selected file is uploaded as a new document to the created folder.

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# .NET Framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WebRequest request = WebRequest.Create( url + "DocumentRepository/UploadHandler.ashx?token=" + token );

request.Method = "POST";
request.Headers.Add( HttpRequestHeader.Cookie, "FHLSID=" + sessionInfo.SessionId );
request.ContentLength = filesize;
request.ContentType = "application/octet-stream";

HttpWebRequest httpRequest = request as HttpWebRequest;
if ( httpRequest != null )
    httpRequest.AllowWriteStreamBuffering = false;

Stream requestStream = request.GetRequestStream();
FileInfo fileInfo = new FileInfo( filepath + filename );
Stream fileStream = fileInfo.Open( FileMode.Open, FileAccess.Read, FileShare.Read );

buffer = new byte[ 65536 ]; // the maximum chunk size is configured on the server

while ( true )
{
    int read = fileStream.Read( buffer, 0, buffer.Length );
    if ( read <= 0 )
        break;
    requestStream.Write( buffer, 0, read );
}

requestStream.Close();
fileStream.Close();

WebResponse response = request.GetResponse();

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:

  • string – for text and URL fields.
  • decimal – for numeric and currency fields.
  • DateTime - for date fields.
  • int[] – for dropdown and drilldown fields (array should contain IDs of dropdown or drilldown menu choices). Typically this is an array with one value.
  • bool – for checkbox fields.

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# .NET Framework
1
2
3
4
5
6
7
8
9
FieldWithValue text = new FieldWithValue();
text.FieldId = textFieldId;
text.Value = "hello, world";

FieldWithValue number = new FieldWithValue();
number.FieldId = numberFieldId;
number.Value = 1.23m;

FieldWithValue[] 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# .NET Framework
1
2
3
4
5
6
7
8
DocumentInfo info = new DocumentInfo();
info.FolderId = folderId;
info.DocumentSchemaId = documentSchemaId;
info.DocumentName = Path.GetFileNameWithoutExtension( filePath );
info.FieldsWithValues = fields;
info.UploadToken = token;
info.OriginalFileName = Path.GetFileName( filePath );
int 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# .NET Framework
1
2
documentManager.SetMetadata( metadataVersionId, documentSchemaId, Path.GetFileNameWithoutExtension( filePath ),
  fields, false, null );

  Updating metadata of an existing document

When updating metadata of an existing document, you do not 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.

  Changing the document schema

When calling SetMetadata, 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.

  Note

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.