SharePoint EXIF event handler

clock January 27, 2008 19:22 by author Fabian Tuender

Something that's missing from the default installation of SharePoint is sometimes simple; like picture information. When you place a picture in SharePoint it will not tell you anything about the EXIF information stored in the jpeg file. Using the exifworks class created by Michal A. Valášek to read the information from the picture; we can update the SharePoint listitem. There is a small modification in my exifworks class to get correct readings when converting from rational to int16 or int32 values.

When you install the feature it will register itself for the ItemAdded and ItemUpdating event on lists with ListTemplateId number 109 (the picture library). So when you add a picture to the library our code will be called. The ItemUpdating event is still empty but it should be easy to use when you know how to use the ItemAdded event. When the ItemAdded event is fired a bitmap picture is created by reading the drawing from the file in the listitem that has just been added. This bitmap is passed to our exifworks class to read out the properties. Next we pass the property from the exifworks class to our sub to check if the field exists. If the field does not exist it will create the field on the fly, see code below.

Private Sub GetExifProp(ByVal ListItem As SPListItem, ByVal FieldName As String, ByVal FieldType As String, ByVal prop As Object)

Dim SPFI As SPField

If Not ListItem.Fields.ContainsField(FieldName) Then

SPFI = ListItem.Fields.CreateNewField(FieldType, FieldName)

SPFI.ShowInEditForm = False

ListItem.Fields.Add(SPFI)

End If

ListItem.Item(FieldName) = prop

End Sub

   

Public Overrides Sub ItemAdded(ByVal properties As Microsoft.SharePoint.SPItemEventProperties)

Dim BM As New System.Drawing.Bitmap(properties.ListItem.File.OpenBinaryStream)

Dim EW As New ExifWorks(BM)

Dim LI As SPListItem = properties.ListItem

   

GetExifProp(LI, "ISO", "Text", EW.ISO)

GetExifProp(LI, "EquipmentModel", "Text", EW.EquipmentModel)

GetExifProp(LI, "ShotDate", "DateTime", EW.DateTimeOriginal)

GetExifProp(LI, "FocalLength", "Text", EW.FocalLength)

This will result in any property you wish to be added as metadata to your picture.

To install this feature:

  • Download and unzip this feature somewhere locally
  • Create a directory in your features folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES) called myeventhandler. Copy the elements.xml and feature.xml to this directory
  • Register your assembly from the project using: C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\gacutil /i <path to your project>\ bin\Release\myeventhandler.dll
  • Next install the feature: stsadm -o installfeature -filename myeventhandler\feature.xml
  • Now we need to activate the feature on a site you want to use it: stsadm -o activatefeature -filename myeventhandler\feature.xml -url http://www.fabita.nl/test
  • You will now see that the feature is active if you goto the site settings and look under site features.

EventHandler.zip (474,35 kb)



Visual Basic SharePoint webpart (Template)

clock September 5, 2007 19:17 by author Fabian Tuender

In my search for a SharePoint template I could only find templates for C#. There was 1 project for Visual Basic but build with VS2002. I converted the project and rebuild it again in VS2005 and created some basic functions in it to show how you can use the webpart. I am just starting with SharePoint development after some years of visual basic 6 and .net hobby development so any comments welcome!

The webpart shows an htmlbutton, a text box, some text and a textbox with the value you set in the property of the webpart. When you push the Click me button the function Private Sub btnText_ServerClick is called. It will set the value of the textbox to your username.

 

When the webpart is created it will start by calling CreateChildControls() and RenderWebPart. In the render function it will write out the html to show on the webpart as well as it sets the value of the property field to the value you defined in your webpart properties.

 

Download the template or download the project.
To easy test the installation I installed VS2005 on a server together with WSS 3.0. After you build the project you can install the dll in the GAC by running the following command:
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\gacutil /i <path to your project>\ bin\Release\webparttemplate.dll


Update 8.9.2007: Okay; there was something I forgot to tell. You need to modify your web.config file to tell ASP .NET that your assembly is safe. By default the web.config file will be located in c:\Inetpub\wwwroot\wss\VirtualDirectories\80. The project contains a safecontrols.txt file, add the first line starting with <safecontrol to the SafeControls section of your existing web.config file. Add the second line (starting with <add assembly=) to the <assemblies> section.

Next run iisreset (Warning This will stop and start the web service, so its down for a few seconds and takes some more seconds after starting to load again)

The coding is shown below. Have fun with it, I hope it's helpful for your webpart development.
My information source Chapter 19: Beginning Web Part Development and Walkthrough: Creating a Basic SharePoint Web Part

           

Imports System.ComponentModel
Imports System.Web.UI
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages
Imports System.Runtime.InteropServices
Imports System.Web.UI.HtmlControls
Imports System.Xml.Serialization
       

<ToolboxData("<{0}:WebPartTemplate runat='server'></{0}:WebPartTemplate>"), XmlRoot(namespace:="Fabita"), Guid("C19B01AA-4593-4426-87BE-3B4194521CC2")> Public Class WebPartTemplate

Inherits WebPart      

Dim WithEvents btnTest As New HtmlButton
Dim lblDisplay As New HtmlInputText
Dim txtProperty As New HtmlInputText
       

Private Const _defPropertyText As String = "Your webpart property"
Private defPropertyText As String = _defPropertyText
       

<Browsable(True), Category("Web Part Template"), DefaultValue(_defPropertyText), WebPartStorage(Storage.Shared), FriendlyName("Your text"), Description("Your description of the friendlyname")> Public Property DisplayPropertyText() As String

Get
Return
defPropertyText
End Get
Set
(ByVal value As String)
defPropertyText = value
txtProperty.Value = value
End Set
End
Property

Private Sub btnText_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.ServerClick
Dim user As SPUser = SPContext.Current.Web.CurrentUser
lblDisplay.Value = "Welcome " & user.Name
End Sub

Protected Overrides Sub CreateChildControls()
btnTest.InnerText = "Click me"
Controls.Add(btnTest)
lblDisplay.Value = ""
Controls.Add(lblDisplay)
txtProperty.Value = ""
Controls.Add(txtProperty)
End Sub

Protected Overrides Sub RenderWebPart(ByVal output As System.Web.UI.HtmlTextWriter)
btnTest.RenderControl(output)
lblDisplay.RenderControl(output)
output.WriteLine("<br>Your property")
txtProperty.Value = defPropertyText
txtProperty.RenderControl(output)
End Sub

End Class

VB WebPart.zip (17,42 kb)

WebPartTemplate Project.zip (432,65 kb)



Creating your SharePoint blog site

clock September 3, 2007 19:14 by author Fabian Tuender

When I wanted to start this blog I was wondering how I can remove this top navigation bar. The anonymous user should be limited with options, since most options apply only to users who have access to my site.

Searching the internet I found these posts: SharePoint Blogs Part I: Blog Access Control, Built-In Groups & Anonymous Users, SharePoint Blogs Part II: UI Customization and SharePoint Blogs Part III: Blogging from Word. They are great to get you started with modifying your site and start blogging from Word.

Next mission would be to find out how to modify the form that displays the comments in a post. You can now only enter a title and body but I want to extend that with a name, email, website and body. An option to remember your input would also be nice