druiz druiz - 11 months ago 39
C# Question

Send message to Azure ServiceBus when items have changed in a Sharepoint List

I'd like to send message to an Azure ServiceBus queue when some items have changed inside a SharePoint list. When you upload/delete a file, I need to create an event that enqueue a message in Azure Service Bus with the content of the file and the id. I don't know how to create an event that do that, Can someone point to me some articles, tutorial to start.

Answer Source

you are looking for event receiver:

By creating event receivers, you can respond when a user interacts with SharePoint items such as lists or list items. For example, the code in an event receiver can be triggered when a user changes the calendar or deletes a name from a contacts list.

Sample code from the documentation:

public override void ItemAdded(SPItemEventProperties properties)
    properties.ListItem["Patient Name"] = "Scott Brown";

Now you need to change this code to send message to a servicebus queue: Get started with Service Bus Queues

public override void ItemAdded(SPItemEventProperties properties)
    var connectionString = "<Your connection string>";
    var queueName = "<Your queue name>";
    var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
    var message = new BrokeredMessage("This is a test message!");



If you need to send the content of the file, just keep in mind tihs:

Service Bus queues support a maximum message size of 256 Kb (the header, which includes the standard and custom application properties, can have a maximum size of 64 Kb).

So you may need to store your file into a blob storage and just send a message to the queue with the id of the create blob also.