Blog, Development, Howto

How to Add Item to Campaign using WebApi

Following JS (with small changes) can be used to add Marketing List/Product/Sales Literature to Campaign:

var parameters = {
	Campaign: {
		campaignid: "E5EE91B6-7A75-E811-A95F-000D3A33EB4E",
		//put valid if of campaign here
		"@odata.type": "Microsoft.Dynamics.CRM.campaign"
	}
};
 
var context;
 
if (typeof GetGlobalContext === "function") {
    context = GetGlobalContext();
} else {
    context = Xrm.Page.context;
}
 
var req = new XMLHttpRequest();
req.open("POST", context.getClientUrl() + 
	"/api/data/v8.2/lists(2DBBE7F5-7A75-E811-A95F-000D3A33EB4E)/Microsoft.Dynamics.CRM.AddItemCampaign", true);
//replace collection name and record id with record you want to add to campaign
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) {
            //Success - No Return Data - Do Something
        } else {
            var errorText = this.responseText;
            //Error and errorText variable contains an error - do something with it
        }
    }
};
req.send(JSON.stringify(parameters));

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.