2

I want to know if an event reciever fires when a list item is added or updated using REST or CSOM.

Can anyone help me with this and give me a correct direction and an example so that I can get my concept clear.

6

Yes event receiver do get fired if item is updated from client object model code. This is how I tested it. Created a custom list and added a text column named "Quantity" to it. Then created a SharePoint project containing an event receiver "ItemAdded". This event is added to custom lists only. Then added code to update the title of the item.

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);
    SPListItem item = properties.ListItem;
    item["Title"] = "Updated From Server Code";
    item.Update();
}

Now created a console app containing CSOM code to add item to this list:

var ctx1 = new ClientContext("http://aissp2013/sites/team");
Web web1 = ctx1.Web;
List list1 = web1.Lists.GetByTitle("List1");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = list1.AddItem(itemCreateInfo);
newItem["Title"] = "Updated From CSOM";
newItem["Quantity"] = "5";
newItem.Update();
ctx1.ExecuteQuery();

When this code is run, I can see that title of the item is "Updated From Server Code" and not "Updated From CSOM". Then ran a JSOM code as shown:

function createListItem() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('List1');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', 'Update From JSOM');
    oListItem.set_item('Quantity', '100');
    oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onQuerySucceeded() {
    alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);

Again found that the title of new item was "Updated From Server Code" and not "Updated From JSOM". Note that "Quantity" will not change as server code (event receiver) is not updating it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.