Just little about C#, .NET, SQL Server, SharePoint and SAP

Archive for July, 2010

How to play flash video in SharePoint?

Posted by zieglers on July 28, 2010

In this post, i’ll try to show you how you can embed and play flash videos in SharePoint pages.
For example, you might want to share with your colleagues a training video, an announcement video… etc. Usually these videos can be hosted in Mysites, Team Sites or Publishing sites.
Here one challange we face is that being an end-user, we have no luxury to deploy anything to SharePoint servers to achieve this task. We have to do this only within our site collection scope.
Sounds hard to do, however it’s possible, and actually it’s quite easy too.
[For this section, credit goes to this post]
Flash movie files are mostly in two formats: SWF files and FLV files. 
FLV files are simply a container movie file, and in order to present them to the user in a manner in which they will be able to control the playback of the movie you need to wrap them in an SWF playback control. Embedding flash video into a SharePoint site isn’t that much different than doing it in any other site. The only challenge is figuring out where and how the required files will be hosted and made accessible by the SharePoint site and its users.
Assuming you already have an FLV file, if not there are several converter applications available that will create FLV files out of common video formats.
The next step is to find an FLV player that will present the video on our site. There are several third party FLV players available, many for free although some require licensing for commercial use. I’ve listed some here:

Each player has their own way of embedding, in this example I will use JW player. They have a neat tool on their site that you can use to generate the code needed to embed your video.

The player does require several files to be hosted and made readable by the user who will have access to the videos. You can create a dedicated document library and upload the appropriate files included in the FLV players package and then use the absolute URL in your embed code to reference them.

NOTE: Be sure that the users who will be watching the video have read access on the document library the FLV player files are stored in.

You then create a content editor web part in which you want the video to appear in and place the embedding code in the source editor.

DEMO: Hosting flash video in MySites

In this demo, i’ll show how you can host your videos in your mysites.
In order to do so, as mentioned above, we need a dedicated document library to host flash player files and video files.
1. Create a document library called ‘Media’. Also create a folder called ‘Player‘ so we keep flash player files separate from video files in Media library.
2. Upload flash player files into Player folder. Those files are:
  • player.swf (Flash player wrapping the video)
  • preview.jpg (Preview image. Once you click on it, video will start playing)
  • swfobject.js (JavaScript file controlling player and video)
3. Upload your video FLV file in media library. In this demo, i use a sample video file named as video.flv.
4. So far we are hosting player files and video files. Now go to page you want to put your video and insert a Content Editor Web Part. (Site Actions > Edit Page > Add a Web Part > Miscellaneous > Content Editor Web Part)
This web part will be displaying flash player and video.
5. Now edit this web part and open source editor.
6. Then insert following code into source editor window.
<script type=’text/javascript’ src=’Media/Player/swfobject.js’></script>
<div id=’mediaspace’>This text will be replaced</div>
<script type=’text/javascript’>
  var so = new SWFObject(‘Media/Player/player.swf’,’ply’,’550′,’403′,’9′,’#000000′);
Note: Don’t forget to replace url of image and file variables with your own ‘mysite’ web url.
7. That’s all. If you have done everything without a mistake, you should be seeing your flash video as follows.
You can download flash player and sample video files here.
Good Luck!

Posted in IT Stuff, SharePoint | Tagged: , , , , , , | 5 Comments »

Event Receiver not firing when document is created using Office

Posted by zieglers on July 7, 2010

Problem Description:

If you create a new document using Office client applications (such as Word, Excel, PowerPoint) and save it into SharePoint, event receivers do not fire. 

Common belief: This is because “adds from Office only fire ItemAdded, not ItemUpdated”. (Please note that this statement is not correct.)

Correction: Adds from Office client applications fire all registered event receivers at all times. However, since they fire with security context of triggering user (normal users), most of the time that user has no admin rights to perform operations such as creating subsite, modifying site, web objects, sending email … etc. This makes users think that event receivers do not fire.

Here are all item events:

  • ItemAdded
  • ItemAdding
  • ItemAttachmentAdded
  • ItemAttachmentAdding
  • ItemAttachmentDeleted
  • ItemAttachmentDeleting
  • ItemCheckedIn
  • ItemCheckedOut
  • ItemCheckingIn
  • ItemCheckingOut
  • ItemDeleted
  • ItemDeleting
  • ItemFileConverted
  • ItemFileMoved
  • ItemFileMoving
  • ItemUncheckedOut
  • ItemUncheckingOut
  • ItemUpdated
  • ItemUpdating

Let’s take a look at the sequence event are fired. 

While uploading a new document in SharePoint library:

Case-1: If a document library does not force check-out, here is the sequence of item events fired.

Case-2: If a document library forces check-out, here is the sequence of item events fired.

Note: ‘..ing’ events are sync before-events. ‘…ed’ events are asynch after-events.

While creating a new document using Office client applications:

(such as Word, Excel, PowerPoint)

Case-1: If a document library does not force check-out, here is the sequence of item events fired.

Case-2: If a document library forces check-out, here is the sequence of item events fired.


Let’s deep dive into the issue: 

Event Receiver not firing while adding a document using Office client applications”

In this demo, I’ll have a code snippet to update current web’s description each time an event fires as seen below.

Doing so, not only we get to see which events fire but also we’ll observe the sequence they fire. Remember before-events are sync events, however after-events are asynch events. This means that execution sequence of after-events may vary with different executions of same code.

Usage-1: No Try-Catch, opening web object from event properties object

Create a new document

Type something and save it

Debug and put a breakpoint at web.Update().

Go ahead and execute this line, you’ll see that an exception is thrown.

Important Finding:

Reason we get a security exception here is that event receiver’s ‘properties’ object was created using current user who has no privileges to update web object.

Misleading fact:

Since there is no exception handling here, thrown exception is not caught by event receiver code. Execution continues and end-user gets no exception, which makes end-user think event receiver is not firing.

What to do:

Always use exception handling in your event receiver code. Handle any exceptions thrown accordingly.


Usage-2: Using Try-Catch, opening web object from event properties object

Follow same steps above to create a test doc. This time we’ll catch the exception.

Now, we are aware that something went wrong. It’s an “Access Denied” error, due to security context of current user. Current user has no rights to modify web object, which is normal behavior to expect.

Usage-3: Using Try-Catch, opening web object from event properties object, and running code with elevated privileges

This time let’s try to run the code with elevated privileges to solve security context problem.

Again, create another test document and debug.

Now, we see here something really interesting:

Access Denied error while running with Elevated Privileges!!!

Important Finding:

Even though code is run with elevated privileges, we still get a security exception. This is because event receiver’s ‘properties’ object is still created using current user’s security context.

Misleading fact:

Running code with elevated privileges does NOT always guarantee that you will not get an “Access Denied” error. Security context of objects are still to be checked even though your code executes with elevated privileges.

What to do:

Always use ‘Using’ clauses when running your code with Elevated Privileges in order to avoid security related exceptions.

Usage-4: Using Try-Catch, opening web object using ‘Using’ clauses, and running code with elevated privileges

Finally, let’s add using clauses to the code. Doing so, we ensure that a brand new web object is created while running code with elevated privileges.

Create another test document and debug.

As seen above, this time web.Update() method is executed successfully!

Also, web description has been updated.  🙂


Posted in IT Stuff, SharePoint | Tagged: , , , , , , , , | 2 Comments »