Returning the document Id when uploading a document.

My code seems to work fine except after the document gets uploaded.

I often do not get the document Id returned in ObjVer.ID. It returns '0' that happens intermittently.

Anything I can do to ensure that the correct Document Id always gets returned? 

Parents
  • The ID should always be returned in success situations.  Could you maybe show some code?

  • Hi Craig thanks for the quick reply:
    Here is my code:

  • public (string docId, string errMsg) DocumentUpload(Models.Authentication.AuthenticationModel auth, ObjectCreationModel? obj, string filePath)
    {
        var options = new RestClientOptions(auth.ClientUri)
        {
            Timeout = TimeSpan.FromMinutes(3),
        };
    
        string token = auth.token;
        string ASPNETSessionId = auth.ASPNETSessionId;
        string mfilesmsm = auth.mfilesmsm;
        var client = new RestClient(auth.ClientUri);
    
    
        var requestUpload = new RestRequest("/REST/files", Method.Post);
        requestUpload.AddHeader("Content-Type", "multipart/form-data");
        requestUpload.AddFile("File", filePath);
        requestUpload.AddHeader("X-Authentication", token);
        requestUpload.AddHeader("Authorization", "Bearer " + token);
        requestUpload.AddHeader("Cookie", "ASP.NET_SessionId=" + ASPNETSessionId + "; mfilesmsm=" + mfilesmsm + "");
    
        var uploadResponse = client.Execute(requestUpload);
        if (!uploadResponse.IsSuccessful)
        {
    
            _log.Error($"File upload failed: {uploadResponse.ErrorMessage}");
    
            return ("", $"File upload failed: {uploadResponse.ErrorMessage}");
        }
        var uploadInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<UploadInfo>(uploadResponse.Content);
        uploadInfo.Extension = Path.GetExtension(filePath).TrimStart('.');
    
        obj.Files = new[] { uploadInfo };
        
        var requestCreateObject = new RestRequest("/REST/objects/0?checkIn=true", Method.Post); 
    
        requestCreateObject.AddJsonBody(obj);
        requestCreateObject.AddHeader("X-Authentication", token);
        requestCreateObject.AddHeader("Authorization", "Bearer " + token);
        requestCreateObject.AddHeader("Cookie", "ASP.NET_SessionId=" + ASPNETSessionId + "; mfilesmsm=" + mfilesmsm + "");
    
        var createObjectResponse =  client.Execute(requestCreateObject);
        if (!createObjectResponse.IsSuccessful)
        {
            Console.WriteLine($"Object creation failed: {createObjectResponse.ErrorMessage}");
            _log.Error($"Object creation failed: {createObjectResponse.ErrorMessage}");
            return ("", $"File upload failed: {createObjectResponse.ErrorMessage}");
        }
        Thread.Sleep(1000);
        var objectVersion = Newtonsoft.Json.JsonConvert.DeserializeObject<ObjectVersion>(createObjectResponse.Content);
        
    
        return (objectVersion.ObjVer.ID.ToString(), ""); //Often returns '0' but the documents work fine in M-Files
    
    }

  • In the cases where the code returns "0" it'd be good to output the entire response from the server.  I suspect that something is causing an error and it's some default value that's actually being returned through your deserialisation process.

  • This is what I got:

    2026-01-28 13:25:11.883 -07:00 [ERR] IsSuccessful: true
    2026-01-28 13:25:11.886 -07:00 [ERR] StatusCode: 200 ("OK")
    2026-01-28 13:25:11.888 -07:00 [ERR] ResponseStatus: "Completed"
    2026-01-28 13:25:11.889 -07:00 [ERR] ErrorMessage: null
    2026-01-28 13:25:11.890 -07:00 [ERR] ErrorException: null
    2026-01-28 13:25:11.891 -07:00 [ERR] ContentType: application/json
    2026-01-28 13:25:11.892 -07:00 [ERR] ContentLength: 1168
    2026-01-28 13:25:11.893 -07:00 [ERR] --- HEADERS ---
    2026-01-28 13:25:11.894 -07:00 [ERR] Date: Wed, 28 Jan 2026 20:25:06 GMT
    2026-01-28 13:25:11.895 -07:00 [ERR] Connection: keep-alive
    2026-01-28 13:25:11.895 -07:00 [ERR] Cache-Control: must-revalidate, max-age=0, private
    2026-01-28 13:25:11.896 -07:00 [ERR] Content-Security-Policy: default-src 'none'; object-src 'self'; script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: https://js.monitor.azure.com; style-src 'unsafe-inline' 'self'; font-src 'self' data:; connect-src 'self' northeurope-0.in.applicationinsights.azure.com https://js.monitor.azure.com; img-src 'self' blob: data:; manifest-src 'self'; form-action 'self' javascript:; frame-src * blob:; frame-ancestors 'self';
    2026-01-28 13:25:11.896 -07:00 [ERR] Strict-Transport-Security: max-age=31536000; includeSubDomains
    2026-01-28 13:25:11.897 -07:00 [ERR] X-Content-Type-Options: nosniff
    2026-01-28 13:25:11.897 -07:00 [ERR] X-Frame-Options: SAMEORIGIN
    2026-01-28 13:25:11.898 -07:00 [ERR] X-XSS-Protection: 1; mode=block
    2026-01-28 13:25:11.899 -07:00 [ERR] --- CONTENT ---
    2026-01-28 13:25:11.900 -07:00 [ERR] {
    "EscapedTitleWithID": " (ID 0)",
    "DisplayID": "0",
    "ObjVer": {
    "Version": 0,
    "VersionType": 0,
    "ID": 0,
    "Type": 0
    },
    "Class": 0,
    "CheckedOutAtUtc": "1601-01-01T00:00:00Z",
    "CheckedOutAt": "1601-01-01T00:00:00Z",
    "LastModifiedUtc": "1601-01-01T00:00:00Z",
    "LastModified": "1601-01-01T00:00:00Z",
    "ObjectCheckedOut": false,
    "ObjectCheckedOutToThisUser": false,
    "CheckedOutTo": 0,
    "SingleFile": false,
    "HasRelationshipsFrom": false,
    "HasRelationshipsTo": false,
    "HasRelationshipsFromThis": false,
    "HasRelationshipsToThis": false,
    "HasAssignments": false,
    "Deleted": false,
    "IsStub": false,
    "ThisVersionLatestToThisUser": true,
    "CreatedUtc": "1601-01-01T00:00:00Z",
    "Created": "1601-01-01T00:00:00Z",
    "Files": [],
    "VisibleAfterOperation": false,
    "PathInIDView": "0\\0-999\\S\\v0",
    "LastModifiedDisplayValue": "1/1/1601 12:00 AM",
    "CheckedOutAtDisplayValue": "1/1/1601 12:00 AM",
    "CreatedDisplayValue": "1/1/1601 12:00 AM",
    "ObjectVersionFlags": 0,
    "Score": 0,
    "LastAccessedByMe": "1899-12-30T00:00:00Z",
    "AccessedByMeUtc": "1899-12-30T00:00:00Z",
    "AccessedByMe": "1899-12-30T00:00:00Z",
    "ObjectGUID": "{00000000-0000-0000-0000-000000000000}",
    "ObjectCapabilityFlags": 0,
    "ObjectFlags": 64,
    "propertyID": 0,
    "LatestCheckedInVersion": 0,
    "BaseProperties": []
    }
    2026-01-28 13:25:24.931 -07:00 [INF] M-Files response content: {"EscapedTitleWithID":" (ID 0)","DisplayID":"0","ObjVer":{"Version":0,"VersionType":0,"ID":0,"Type":0},"Class":0,"CheckedOutAtUtc":"1601-01-01T00:00:00Z","CheckedOutAt":"1601-01-01T00:00:00Z","LastModifiedUtc":"1601-01-01T00:00:00Z","LastModified":"1601-01-01T00:00:00Z","ObjectCheckedOut":false,"ObjectCheckedOutToThisUser":false,"CheckedOutTo":0,"SingleFile":false,"HasRelationshipsFrom":false,"HasRelationshipsTo":false,"HasRelationshipsFromThis":false,"HasRelationshipsToThis":false,"HasAssignments":false,"Deleted":false,"IsStub":false,"ThisVersionLatestToThisUser":true,"CreatedUtc":"1601-01-01T00:00:00Z","Created":"1601-01-01T00:00:00Z","Files":[],"VisibleAfterOperation":false,"PathInIDView":"0\\0-999\\S\\v0","LastModifiedDisplayValue":"1/1/1601 12:00 AM","CheckedOutAtDisplayValue":"1/1/1601 12:00 AM","CreatedDisplayValue":"1/1/1601 12:00 AM","ObjectVersionFlags":0,"Score":0,"LastAccessedByMe":"1899-12-30T00:00:00Z","AccessedByMeUtc":"1899-12-30T00:00:00Z","AccessedByMe":"1899-12-30T00:00:00Z","ObjectGUID":"{00000000-0000-0000-0000-000000000000}","ObjectCapabilityFlags":0,"ObjectFlags":64,"propertyID":0,"LatestCheckedInVersion":0,"BaseProperties":[]}

  • All of those values seem to be empty.  I was going to say that I think you've output just an empty deserialised instance of a dummy deserialisation class, but things like the PathInIDView seem to have values (not just empty strings), which wouldn't happen.

    Just to be clear: when you deserialise the object you pass the type in - "ObjectVersion".  Where is that class defined?  It's not a COM API class, is it?

    I have never personally seen the REST API return an empty object like this.  You may need to reach out to the partner team via support to see whether anyone has any ideas.

  • It turns out that this was a permissions issue.
    Gave the account admin rights and it's all working great again.

Reply Children
No Data