Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created this Sandboxed Event Receiver on ItemAdding on a custom list.

It works on my on-premise server, but it does not work on Sharepoint Online aka. O365.

Anybody know how to create an Event Receiver in Sharepoint Online, I will be really thankful!

----- EDIT ----

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Change_Title_Event.Change_Title
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class Change_Title : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           properties.AfterProperties["Title"] = "Kevin: Changed by event receiver at" + DateTime.Now.TimeOfDay.ToString();
           base.ItemAdding(properties);
       }


    }
}

Elements.xml

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="100">
      <Receiver>
        <Name>Change_TitleItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Change_Title_Event.Change_Title.Change_Title</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>

  </Receivers>
</Elements>

Scope is Web and the Item is in the Feature.

-------- EDIT 2 ----------

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Change_Title_Event.Change_Title
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class Change_Title : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           properties.ListItem["Title"] = "Kevin: Changed by event receiver at" + DateTime.Now.TimeOfDay.ToString();
           properties.ListItem.Update();
       }


    }
}

This code works on on-premise, but do not work on Sharepoint Online.

Does somebody has a working solution (.wsp), that works on Sharepoint Online?

share|improve this question
    
You might want to add logging so you can actually see what's going wrong: spsl.codeplex.com. –  Jasper Jul 20 '12 at 13:13
    
There must be a bug on Sharepoint Online, my friend who is an exp. sharepoint programmer also has the problem with event receivers. –  Kevin Simper Jul 23 '12 at 11:11
    
I use SharePoint Online too on a fairly regular basis, don't know of any bugs with event receivers. But a small error can cause a lot of trouble without you knowing if you don't implement any kind of logging. –  Jasper Jul 23 '12 at 12:05
add comment

4 Answers 4

up vote 1 down vote accepted

If your event receiver is working on your on-premise server, there is no reason it should not work on Office 365. Unless you are doing something from the Event Receiver which is not allowed in Sandbox. In that case, a SPException will be thrown.

There is really no "different" way to create Event Receivers for SharePoint online. I think that your Event Receiver might be firing, but there must be some error in the code for the Event Receiver. Can you please update your answer with code so that it would be easier to find out what might be going wrong?

Update:

Can you try the following code?

 public override void ItemAdding(SPItemEventProperties properties)
   {
       base.ItemAdding(properties);
       properties.ListItem["Title"] = "Kevin: Changed by event receiver at" + DateTime.Now.TimeOfDay.ToString();
       properties.ListItem.Update();
   }

Also, is there a reason why you are using the ItemAdd*ing* event instead of the ItemAdd*ed* event?

share|improve this answer
    
I have added the code, it simply changes the title. –  Kevin Simper Jul 19 '12 at 9:18
    
Please see if my updated answer helps. –  Vardhaman Deshpande Jul 19 '12 at 13:15
    
I have tried your code, and that only works with ItemAdded. The reason i am using ItemAdding is when you use that, the user see the result immediately. I have updated my post with the new code. –  Kevin Simper Jul 20 '12 at 9:20
    
You can make the item added synchronous. Then the user will see the results immediately. Just add the <Synchronization> element in your Elements.xml. Have a look here: blogs.msdn.com/b/unsharepoint/archive/2010/11/10/… –  Vardhaman Deshpande Jul 20 '12 at 11:14
    
There must be a bug on Sharepoint Online, my friend who is an exp. sharepoint programmer also has the problem with event receivers. –  Kevin Simper Jul 23 '12 at 11:11
show 1 more comment

I wonder if it is because of your using references ? Try cutting your references and using statements to the bare minimum you need.

share|improve this answer
    
What references should i cut? I am using the "Add item" in visual basic to create my event receiver and that is what visual studio puts in. –  Kevin Simper Jul 20 '12 at 6:52
    
There must be a bug on Sharepoint Online, my friend who is an exp. sharepoint programmer also has the problem with event receivers. –  Kevin Simper Jul 23 '12 at 11:11
add comment

I think, in your code properties.ListItem.Update(); will throw error during ItemAdding Event.

Kevin,

I am able to see that in EDIT-2 you are using ItemAdded event, but in element.xml you have missed to input receiver for the same.

share|improve this answer
add comment

You first need to do GetItemByID on your list. and then update it.

public override void ItemAdded(SPItemEventProperties properties)
        {

this.EventFiringEnabled = false;
            base.ItemAdded(properties);

            SPList emp = properties.Web.Lists.TryGetList(Constants.PFSListCollectors);
            SPListItem item = emp.GetItemById(properties.ListItemId);

                item[Constants.ColNameCollectorsName] = Convert.ToString(item[Constants.ColNameName]).Split('#')[1];
                item.Update();

            this.EventFiringEnabled = true;
        }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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