Tuesday, October 30, 2012

Disable event firing - Part 3 (multiple event firing)

In previous posts (Part 1, Part 2), I've demostrated how to disable\enable event firing in event receiver and outside of an event receiver.

Sometimes, you can find yourself in a situation of having multiple event firing in your code, but just can't find the source of event trigger.

Problem may be that you have more than one event receiver of the same kind registered in your project (two ItemAdding receivers, for example).

Solution to this problem is using EventHandlerManager program. With this small app, you can register, or unregister every event to your Sharepoint list. It can be downloaded from Codeplex - search for "speventhandlermanage".

Write link to your Site Collection in the "Site URL" column and press "Load Lists" button.

In the "Site Lists" section you will get the list of all libraries and lists of that Site Collection.

Select the list you want to check for event handlers and press "Show Registered Event Handlers" button on the right side. You will get the list of all event handlers of that list.

If you see more multiple event handlers of the same kind, you've found your problem. Just select all unwanted event handlers (so that you leave only one of each sort) and press "Remove Selected Event Handlers" button.

Multiple event firing problems are gone.

With this application, it is possible to register event handlers also. To do that, select the desired list in the "Site List" section, select the desired event from "Event Type" dropdown and press "Register To Selected Lists" button.

Really simple, but useful tool.

Friday, October 26, 2012

Disable event firing - Part 2 (outside an event)

In previous post I have shown how to disable/enable event firing in event receiver. But, sometimes, you want to disable event firing outside of an event. It is possible with this simple code:

private static void EventFiringEnabled(bool enabled)


    Assembly assembly = Assembly.Load("Microsoft.Sharepoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
    Type type = assembly.GetType("Microsoft.SharePoint.SPEventManager");

    PropertyInfo pi = type.GetProperty("EventFiringDisabled", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    pi.SetValue(null, !enabled, null);

In the next post, I'll show you how to detect all event receivers in your project.

Thursday, October 25, 2012

Disable event firing - Part 1 (in event receiver)

If you are using Event Receivers for Add, Update and Delete operations on a Sharepoint list item, you might experience some issues concerning multiple event firing.

Firstly, what are Event Receivers?

An event receiver in MS SharePoint a method that is called when a triggering action occurs on a specified SharePoint object. This triggering actions may be various actions such as updating, adding, moving, deleting, checking in, and checking out.

That means that if you register a event receiver ItemAdding to your list, every time you add new item to your list, that event will trigger, and custom code that you put in your Event Receiver will be executed. Same happens with ItemUpdating event if you update list item.

Problem appears when you add code to event receiver that updates (or inserts new item) SPListItem. That is going to trigger ItemUpdating (or ItemAdding) event again, and again, and again...

To prevent that use this code in your event receiver:

public class HandleEventFiring : SPItemEventReceiver


            public void DisableEventFiring()
                this.EventFiringEnabled = false;

            public void EnableEventFiring()
                this.EventFiringEnabled = true;

In the next post I'll show you how to disable/enable event firing outside of an event receiver.

Monday, October 22, 2012

Sharepoint 2007 to Sharepoint 2010 migration

If you're still using Sharepoint 2007 and want to migrate your sites to Sharepoint 2010 there is only two ways of doing this:
  1. In-Place Upgrade - upgrades the whole SP Farm on the same machine,
  2. Database Attach Upgrade - upgrade on the new machine detaching content databases from the old machine and attaching then to the new machine.
 Today, I'll demonstrate how to migrate a site to SharePoint 2010 using the database attach upgrade.

Step 1: Create new machine
Install Sharepoint 2010 on a new server and create new Web Application and new Site Collection there.

Step 2: Backup content database
On the old SP 2007 machine go to SQL Server Management Studio. Select your WSS_Content database, right click and select Tasks --> Back Up.

On the next screen, you can leave everything as it is, or you can enter your own Name and Destination where the backup will be saved. Click OK and Backup process will begin. 

Step 3: Copy database to the new machine
When backup process is over, copy that bak from old machines file system to the new machine (let's say to C:\temp).

Step 4: Install custom parts
If you have custom components, like Web Parts, Features, etc, on your old site, you need to deploy them to the new site now.

Step 5: Restore database on the new machine
On the new machine open SQL Server Management Studio. Right-click on Databases and select Restore Database.

Enter the new database name, making sure that you don't overwrite the existing content database. Select the Source and click OK to start.

Step 6: Remove existing content database
Navigate to central administration -->click on manage content databases --> select the web application you created for migration -->click on database --> check in the remove content database --> click OK.

Step 7: Add the restored database in the target site in Management Shell
Go to: Start --> All Programs --> Microsoft Sharepoint 2010 Products --> Sharepoint 2010 Management Shell.

Write command:
Stsadm.exe –o addcontentdb –url “enter your url” –databasename “enter your db name” 

Step 8: Restart IIS
Go to: Start --> All Programs --> Microsoft Sharepoint 2010 Products --> Sharepoint 2010 Management Shell.

Write command:

Your Sharepoint Site is now migrated!

Wednesday, October 17, 2012

How to delete App from Sharepoint 2013 Site Content

If you're building App for Sharepoint 2013, you will most certainly come across this message during deployment of your Visual Studio 2012 project: "Error occurred in deployment step 'Install App for Sharepoint': An instance of this App already exists at the specified location."

This is a bug of Sharepoint 2013 Preview!

You can go to your Sharepoint site, click on "Site Content" and there you will see your App in a list.

You can on link "Click to retry" and try to delete your application. If your App is gets delete, great, you have removed your App and now you can go back to Visual Studio and deploy your projetct again.

But, sometimes, App just will not uninstall, no matter how many times you click on it. Then, only thing you can do is to change the productId in your appmanifest.xml in your VS project.

After that, you can successfully deploy your project, but you will have two instances of the App in your Site Content; old and new one. This is a known bug and it will be fixed in future versions.

Monday, October 8, 2012

SP2013 - Register-SPWorkflowService error

If you're trying to install Windows Azure Workflow for Sharepoint 2013 Preview you can expect to get tons of possible errors.

Today, I'm going to cover only the one which I encountered during my install (all others I had are already documented on web :)  ).

Let's say that you successfully installed Windows Azure Workflow (if you haven't, you can follow tutorial from sharepointassist web page.

At the end of installation, you just need to register Azure Workflow for your SPSite in Management Shell (last step in tutorial). 

If you encouter following error: "Register-SPWorkflowService : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

Just change ports, so you don't use https. Change your command from:

Register-SPWorkflowService –SPSite "http:localhost/yourSite" –WorkflowHostUri "https:localhost:12290" –AllowOAuthHttp


Register-SPWorkflowService –SPSite "http:localhost/yourSite" –WorkflowHostUri "http:localhost:12291" –AllowOAuthHttp 

And now it should be ok.