web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

Creating incidents via the web API with attachments?

(0) ShareShare
ReportReport
Posted on by

/I am exploring using the Customer Engagement API to add attachments to incidents created (images, pdfs, etc...) - does anyone know of any samples that show how this can be implemented?

Thanks!

I have the same question (0)
  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi TWPW,

    Here are code.

    1.create a case with web API.

    var entity = {};
    entity.title = "Test Case From API";
    entity["customerid_account@odata.bind"] = "/accounts(e1192f1a-628e-ea11-a811-000d3a35bad3)";
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/incidents", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                var uri = this.getResponseHeader("OData-EntityId");
                var regExp = /\(([^)] )\)/;
                var matches = regExp.exec(uri);
                var newEntityId = matches[1];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
    

    2. Create Notes Attachment using Web API.

    Image attached you need to pass it as base64 string as document body.

    var entity = {};
    entity.subject = "test from api";
    entity.filename = "testpicture.png";
    entity["objectid_incident@odata.bind"] = "/incidents(23e6ee7c-5812-e911-a96b-000d3a3638df)";
    entity.documentbody = "iVBORw0KGgo...";
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/annotations", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                var uri = this.getResponseHeader("OData-EntityId");
                var regExp = /\(([^)] )\)/;
                var matches = regExp.exec(uri);
                var newEntityId = matches[1];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
    

    I have test them and they run successfully.

    Regards,

    Clofly

  • Community Member Profile Picture
    on at

    Thanks Clofly - I will validate if this is working for me today!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
NeerajPawar Profile Picture

NeerajPawar 31

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 19 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 16

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans