Eamon Barker's eb.NET

Not quite vb.NET, but getting close!

Filter by APML
RSS Feed

Search

Profiles/Groups

Google Ads

Top Posts

Tags

Categories

Archive

Calendar

<<  March 2010  >>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Blogroll

Disclaimer

All postings are provided AS IS with no warranties, and confer no rights.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright Eamon Barker 2010

CC License

Visitor Map

Locations of visitors to this page
Welcome to eb.NET... a place that I can keep the things that help me in my day, that might help you in your's!

SharePoint Tip: Creating Multiple Sub-Sites with STSADM

Using STSADM to create multiple copies of one site!

In this post I will go cover the creation of a site using the STSADM tool. If you want to clone a few sites and re-create several copies of them this is the easiest way to do it. This is really handy if you have a base Project Management or Team Site that you want to use for a few different areas of your business!

STSADM Export - Exporting the base site

The first step of the process is to create a "template" for us to re-use when creating the copies. To do this we use the STSADM -o export command. Below is the syntax to do this. As always, I put this into a batch file so I have to change the directory to the 12 hive so I can run STSADM.

   1: cd %COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\12\bin 
   2: REM Export the base site to a folder on the file system
   3: stsadm -o export -url http://yoursite/demosite/ -filename c:\SiteBase\ -includeusersecurity -versions 4 -nofilecompression -quiet 

Creating the Site and Importing the base template

The next part of the process is to create a base site and then import the "template" we exported above.

Note: Make sure you use the same site template for the createweb command as you used to create the base template! not doing so will cause an error and the import process will not work.

Below is the syntax to create the site and import the exported "template".

   1: cd %COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\12\bin
   2: REM Create a site base
   3: stsadm -o createweb -url http://yoursite/NewSite -title "Demo Site Name" -sitetemplate STS#0 
   4: REM Import the template we exported 
   5: stsadm -o import -url http://yoursite/NewSite -filename c:\SiteBase\ -includeusersecurity -nofilecompression -quiet 

Now that we have this we can reproduce it to create multiple instances of the same site!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Accessing Database Images in ASP.NET and displaying them with .JPG extension

How to use .NET 3.5 SP1 to display images from a database so the user sees them with a .JPG extension.

In this post I am going to explain how to use the latest bits of the .NET framework (.NET 3.5 SP1) to display images from a database so the user sees them with a *.JPG extension. This is something that should be simple, yet, takes a little bit of work! The scenario I am using is as follows:

  • MSFT SQL 2005 Database
  • .NET 3.5 SP1 Entity Framework
  • .NET 2.5 SP1 Routing Engine
  • ASP.NET Web Forms
  • IIS 7 on a Vista PC (But works on Server 2008)

OK so to paint the picture, by the end of this we will be able to access an image from a URL that looks like this: http://yoursite.com/Image/imgID-1-T.jpg. This will return the image with ID of 1 and it will be a thumbnail image, but I will show how to display either the thumbnail or full size version, or what ever you want!

Displaying the images in an ASP.NET Webform

Rendering the image to a web page can be quite a cool thing to do, the code below takes a few different pieces of a pie and puts them nicely together! I will start off by accessing the data, then altering the images (if needed) and finally rendering the images to the page.

Note: As this page will be accessed via a routing rule that passes in the name of the JPG being accessed through, the JPG has to be named with the format of ImageType-ImageID-ImageSize.

For this example, I have created a folder called _Images. Inside that folder I have an aspx page called image.aspx. The actual ASPX page is standard as shown below:

   1: <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Image.aspx.vb" Inherits="Example.Image" EnableTheming="false" StylesheetTheme="" %>
   2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   3: <html xmlns="http://www.w3.org/1999/xhtml" >
   4: <head id="Head1" runat="server">
   5:     <title>Image</title>
   6: </head>
   7: <body>
   8:  
   9: </body>
  10: </html>

Database

The database in this example is a simple one table set-up with two columns in it, the table is called Images.

  • ImageID - Int field to hold the image ID
  • Image - Image field

Here is the SQL for the table:

   1: CREATE TABLE [dbo].[Images](
   2:     [ImageID] [int] IDENTITY(1,1) NOT NULL,
   3:     [Image] [image] NOT NULL,
   4:  CONSTRAINT [PK_Images] PRIMARY KEY CLUSTERED 
   5: (
   6:     [ImageID] ASC
   7: )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
   8: IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
   9: ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  10:  
  11: GO

 

Code Behind

My data-source is the Entity Framework (EF), but you could use any source to populate the code. First off I need to reference the System.Data namespaces that will allow me to work with the EF, also I have added a reference to the System.Drawing namespace that I will use to resize the images.

   1: Imports System.Data.Objects
   2: Imports System.Data.Objects.DataClasses
   3: Imports System.Drawing.Image

The Process of generating the image

In this section I will explain how the code below works and what is happening through out.

Here is how the code below works:

  1. First we need to create the EF context that we will use to get the images from the data classes. This is the ImageData definition.
  2. The first thing we need to do in the Page_Load is to split up the name of the file. I will explain later how the file is routed to this page, but as we are using the routing rules, we need to get this name from the HttpContect. ImageName is set in the routing rules to pass the name of the image through to this page (see the Routing the Images using a *.JPG File section). 
  3. Now that we have access to the data and the image ID, we need to get the image out of the database. This is done using a lambda query on the context we opened above. We pass this to an instance of the Images class (this is an EF generated class).
  4. As we are not rendering an aspx text page, we need to change the Content Type of the output, this is now "image/jpeg"
  5. The next part of the process is to resize the image. As part of the image naming process (e.g. <img scr="/image/ImgID-1-T.jpg">) you need to make sure you have the "-" in the same order as I have and that you are using a size character you have defined in the code below.
    Notice there are three options, T and S being the resize options and the final is just rendering the full size image.
   1: Dim ImageData as new ImageEntities
   2: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   3:     'Split the File Name out to get the information we need
   4:     Dim ImageInfo As String() = Context.Items("imagename").ToString.Split("-")
   5:     
   6:     'In this example I am only accessing one data source, you could access many,
   7:     'add a Select Case with the Image Type and select from other areas
   8:     Dim ImageType As String = ImageInfo(0) 
   9:     
  10:     'We need the ID of the image
  11:     Dim ImageID As Integer = ImageInfo(1)
  12:     
  13:     'We need to know what size to change it to
  14:     Dim ImageSize As String = ImageInfo(2)
  15:     
  16:     Dim Images As ImageModel.Images
  17:     Images = ShoppingData.Images.FirstOrDefault( _
  18:                         Function(i As Images) i.ImageID = ImageID)
  19:     
  20:     'Cast the Image data from the database to a Byte()
  21:     Dim aryContent As Byte() = DirectCast(Images.Image, Byte())
  22:     
  23:     'Create a new Memory Stream to hold the contents of the 
  24:     'image in ready for the Drawing.Image
  25:     Dim imgStream As New IO.MemoryStream
  26:     
  27:     Dim dummyCallBack As GetThumbnailImageAbort
  28:     dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort( _
  29:                                                         AddressOf ThumbnailCallback)
  30:  
  31:     imgStream.Write(aryContent, 0, aryContent.Length)
  32:  
  33:     Dim Img As System.Drawing.Image
  34:     Img = Drawing.Image.FromStream(imgStream)
  35:     
  36:     'Set the MIME tyoe
  37:     Response.ContentType = "image/jpeg"
  38:     'Resize the image if we need to
  39:     If ImageSize = "T" Then
  40:         'This will keep the image 100px high and change the width to keep the 
  41:         'ratio correct
  42:         Dim ratio = Img.Width / Img.Height
  43:         Dim height = 100
  44:         Dim width = 100 * ratio
  45:         Dim thumbnail As Drawing.Image = Img.GetThumbnailImage( _ 
  46:                                             width, height, dummyCallBack, IntPtr.Zero)
  47:         thumbnail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  48:     ElseIf ImageSize = "S" Then
  49:         'This will keep the image 100px wide and change the height to keep the 
  50:         'ratio correct
  51:         Dim ratio = Img.Height / Img.Width
  52:         Dim height = 100 * ratio
  53:         Dim width = 100
  54:         Dim thumbnail As Drawing.Image = Img.GetThumbnailImage(width, height, _
  55:                                                            dummyCallBack, IntPtr.Zero)
  56:         thumbnail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  57:     Else
  58:         Img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  59:     End If
  60:     Response.End()
  61: End Sub

The above code will now render an image, but will have to go via the routing engine. If you want to skip that step change the Context.Items code to a Request.QueryString and pass through some parameters that way.

Routing the Images using a *.JPG File

To route the images with a certain path and extension I used the example Chris Cavanagh gave on his blog. His example was in C#, so I converted it and have the VB example below. First off, I added a new class file to my project and put the routing functions in that. Then I referenced those classes from the global.asax.

PageRouter Class

The PageRouter class will handle all the setting of page routing rules. The first thing we have to do is reference some of the namespaces we are going to use in the class:

   1: Imports System
   2: Imports System.Web
   3: Imports System.Web.UI
   4: Imports System.Web.UI.HtmlControls
   5: Imports System.Web.UI.WebControls
   6: Imports System.Web.UI.WebControls.WebParts
   7: Imports System.Web.Routing
   8: Imports System.Web.Compilation

The first class we want to create is a page handler class that will have our custom IRouteHandler and will do the registering of our rules. For more information about what happens in this class, please read Chris' blog.

   1: Public Class WebFormRouteHandler(Of T As {IHttpHandler, New})
   2:         Implements IRouteHandler
   3:         Private _virtualpath
   4:         Public Property VirtualPath() As String
   5:             Get
   6:                 Return _virtualpath
   7:             End Get
   8:             Set(ByVal value As String)
   9:                 _virtualpath = value
  10:             End Set
  11:         End Property
  12:  
  13:         Public Sub New(ByVal virtualPath As String)
  14:             _virtualpath = virtualPath
  15:         End Sub
  16:  
  17: #Region "IRouteHandler Members"
  18:         Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) _
  19:         As System.Web.IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
  20:             For Each value In requestContext.RouteData.Values
  21:                 requestContext.HttpContext.Items(value.Key) = value.Value
  22:             Next
  23:             If (VirtualPath IsNot Nothing) Then
  24:                 Return (DirectCast(BuildManager.CreateInstanceFromVirtualPath(VirtualPath, GetType(T)), IHttpHandler))
  25:             Else
  26:                 Return New T()
  27:             End If
  28:         End Function
  29: #End Region
  30:  
  31: End Class

Once that is done we have to create our PageRouter class, we will reference this later in the Global.asax. This class will hold all of the routing rules we want to use in our project. This project calls on the WebFormRouteHandler to register the pages.

   1: Public Class PageRouter
   2:     Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
   3:             ' Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable 
   4:             ' automatic support on IIS6 and IIS7 classic mode 
   5:             routes.Add(New Route("Image/{imagename}.jpg", New WebFormRouteHandler(Of Page)("~/_Images/Image.aspx")))
   6:     End Sub
   7: End Class

The code above will catch any requests coming for images at http://yoursite.com/image/imgID-1-T.jpg and send them to our image page. You will notice this is where we set the name of the context item, {imagename}.

Global.asax Code

Finally we need to fire the PageRouter class and register the routes. When the application starts, I want to set all then routing rules, so in the global.asax I add the following code to the Application_Start subroutine. Please note, you will have to reference the System.Web namespace (Imports System.Web.Routing).

   1: Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
   2:     PageRouter.RegisterRoutes(RouteTable.Routes)
   3: End Sub

Finally

Hopefully that explains how to render a database image as a *.JPG using ASP.NET SP1! There are some cool things we can now do straight out of the box, and from what I have seen of SP1 I can't wait for it to be released!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


10 Reasons why SharePoint Designer is cool!

10 reasons why you should take another look at SharePoint Designer!

Microsoft Office SharePoint Designer It seems every time I talk to another developer about SharePoint, the second I mention SharePoint Designer (SPD) I get a sideways look and told "I won't use FrontPage". First off, Expression blend is FrontPage; SharePoint Designer is a tool to modify and enhance SharePoint. Hopefully reading the rest of this post will motivate you to install it (or open it) and give it a good ole' fashion thrashing... not too hard, it has a tenancy to break every now and then...

The list below isn't wholly features of SPD, it includes spin-offs that have come in handy for myself and others using it.

I have ordered this list from what I think are its great features, to the best features... in terms of how it helps me when tinkering with SharePoint.

 

1. Let Technical BA's and Power Users do their stuff

I wasn't sure whether to put this in the number one position or ten, but it made it to one... I am developer, I enjoy chunking through code and not doing workflow and page modifications that don't include at least one line of VB. Getting a technical analyst trained up in SPD will save a lot of time for everyone. For example, say a client asks for modifications in a workshop, the analyst can do these on the fly, fantastic! This tool is aimed at those sort of power users, so let them go for it!

2. The DataView Web Part

This is my favourite feature of SPD. It takes all of two minutes to get a DataView Web Part on your page and linked up to a data source. Creating custom views and filtering information from around the site is simple using this feature. If a project manager has ever asked you to give them a summary of all their outstanding tasks across all their projects, then the DataView Web Part will help a lot!

Dataview Web Parts use XSLT to render the data. Looking back at point one, there are some things BA's can't do, one of those is tweaking the XSLT... the DataView Web Part offers an extension (ddwrt namespace) that helps a bit, but there is always something that will need some customisation.

3. ASPX Page Editing

Have you ever tried to edit SharePoint ASPX pages in Visual Studio? If you need to make changes to the look and feel of a page, then SPD is the only tool to use. If you are creating a feature, you wouldn't use SPD to create the sites/lists etc. but to modify existing pages, SPD is great! Another advantage of SPD over Visual Studio is the preview... Visual Studio doesn't give you a preview of the page when it is up on the site, SPD does. In saying that, you can copy the ASPX code from VS and paste it into SPD to see how it looks.

4. Branding SharePoint SitesSharePoint Designer Server Controls

This isn't a feature as such, but it is something that is near impossible without SPD. Modifying your "companyPage.master" is simple in SPD, it gives you a great preview, all the controls SharePoint controls in a menu (see image) ready to drag on and use.

Editing your corporate style sheet and seeing the results straight away is another bonus of SPD. Doing it the hard way would mean changing it in an editor of choice, uploading it to the 12 hive, refreshing the APP pool and then the browser... SPD seems a lot easier!

5. Workflow Designer

As I said in point one, getting some of the workflows completed by a BA will help the progress of the project to no end. Of course, there are limitations to the sort of things the SPD workflow designer can do, anything that needs some custom coding will have to go to the developers. All in all the workflow designer in SPD is a great tool for getting a workflow out and working!

6. SP DatasourceData From a SharePoint Web Service

Out of the top five, but only just! This is an easy way to connect to any number of data sources, not just SharePoint.

A few Examples:

  • SharePoint Lists
  • SharePoint Libraries
  • Other Databases
  • XML Files
  • Linked sources
  • BDC (If you are running the enterprise version of MOSS,
    see my post on the differences between WSS and MOSS for details)
  • Web Services (SharePoint or Other), see the image for an example of output data

One of the really cool data sources is the Linked sources data source. The can pull together any number of other data sources and give you a sing source, great for roll ups from multiple sites.

SP Datasource can be used by a DataView Web Part OR be dragged onto your page as a normal ASP.NET data source and consumed by any of the standard ASP.NET controls. For example, if you wanted a drop-down list that looks to an active issues list: connect to that list, filter the list showing only active and drag the data source onto the page, that easy!

7. Custom List Forms

Much like the SP Datasource, this is something that is a must for anyone checking out SPD to have a look at. If you want to customise any list, survey, etc. within SharePoint, you will need to use this feature. This is a great thing if you want to add some additional information to a list form, change the way titles SharePoint Designer Folder Listare displayed, or generally mess with a form.

8. Site Connectivity

Using SPD to connect to a SharePoint site opens up a whole bunch of interesting places you wouldn't have known about unless you went for a hunt through the database and 12 hive. Opening a site in SPD shows all the master page and css files that give the site its look and feel (see the image to the left). You can view all the sites and lists within the folder list.

9. Client Application

The fact that SPD is a client application that can connect to SharePoint is great! There is no need to install a virtual server to do development work, it sits on a normal PC and connects to the deployment without issue.

10. Import/Export

And finally in the tenth position the ability to export and import sites to either a SharePoint site template or a Personal Web Package. Makes it very easy to move something from a development environment to testing, without having to create a whole bunch of XML and config files. This feature is great for moving something like a Wiki from one site to another, without losing the links!

 

Hopefully that gives you enough to break out the Office disk and put SharePoint Designer on your machine! If you have any comments or other reasons why SharePoint Designer is a cool product, please submit a comment!

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


SharePoint Tip: Upgrading a SharePoint (SP0) Content Database to SP1

Moving a SharePoint content database from SP0 to a deployment with SP1

If you have installed SharePoint (SP0) and you want to move a content database to a deployment running SP1 without upgrading the whole farm, read on!

First off, create a blank team site (Central Administration > Application Management > Create or extend Web application) and then follow the steps below.

When you attempt to attach the database through the central admin web interface, you might get hit with the following error:

Attaching this database requires upgrade, which could time out the browser session.  You must use the STSADM command 'addcontentdb' to attach this database.

To get around this, do what the error says...

  1. RDP to your SharePoint server
  2. Open the Command Prompt (Start > Run >CMD)
  3. Paste the following line into the prompt "cd %COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\12\bin", this will take you to the 12 Hive
  4. Type the following command:
    stsadm -o addcontentdb -url http://SiteName -databasename WSS_Content_DATABASENAME -databaseserver DOMAIN\DATABASESERVER
  5. Hit enter, and all going to plan you will get: Operation completed successfully

To make sure, go to Central Administration > Application Management > Content Databases and it should be there. Take the origional database (the one created when you created the site) offline and make sure this one is the only one online. Navigate to your site and again, make sure it is all there!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Development Tools for SharePoint

Development tools I use when cutting code for SharePoint.

I had to go through the process of installing a new virtual machine for SharePoint development today and realised there was a fair list of tools that I use in SharePoint Development.

As SharePoint is (in my view) an extension of the .NET framework, and as such a platform for developers to weave their magic, some of the tools in my list are tools I also use in my general ASP.NET development. So without further ado, my list of development tools (this list will change from time to time!):

Tool's Name Description Use
Windows Server 2008 Hyper-V Hosts the virtual environments, give the virtual machine 1500mb of RAM and you will be fine! Host for VM
Windows Server 2003 32bit Installed as a virtual environment, use 32bit as VSeWSS does NOT work in 64bit. OS for Development
Visual Studio 2008 IDE for code development, workflows and packages. I use 2008 because I don't need any add-ins for Office. This is installed on the Server. IDE
Office SharePoint Designer This is installed on my PC, not the server. Don't listen to what other say about SPD, it has a whole lot of GREAT things! Branding wouldn't be very fun without it! Design tool
Microsoft Office SharePoint Server 2007 SDK Has a good amount of code demos and documentation! SDK
Windows SharePoint Services 3.0 SDK Opens the world of custom workflows and SharePoint tweaking! Same as the MOSS 2007 SDK, full of good code and documentation! SDK
VSeWSS 1.2 Trying to package up a project without this was a bit of a mission... This is a great addition to the arsenal! VS2008 Add-in
U2U CAML Query Builder and Execution Tool A great tool for writing and firing CAML queries! Query Tool
I use these tools for general development, and SharePoint development is no different!
Fiddler I use this to view what is being passed back and forward between my browser and SP. HTTP Debugger
Internet Explorer Developer Toolbar This is a fantastic add-on for IE, viewing the DOM has never been so nice! DOM/CSS/Browser Debugger
NotePad++ Great way to view the source of the page! Viewing Source, Text Editor

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5