Tuesday, 1 May 2007

Transferring Files Between the Host and Guest in Virtual Server 2005

Microsoft Virtual Server 2005 makes life very easy to assist in the development of SharePoint projects (or any I.T Project), particularly when you want to develop or test on an environment that is close to the target deployment environment as a risk mitigation strategy.

I often hear of stories where developers have developed on their XP workstation using Visual Studio 2005 and tested their deployment on IIS 5.0 in XP, only to then spend numerous hours debugging their project after deploying to Windows Server 2003 IIS 6.0, wondering why on earth the solution doesn't function as it did on XP ...

But this article doens't talk about the pros or cons about MS Virtual Server 2005, rather this article explains a simple approach to transfer files between the HOST (computer on which Virtual Server has been installed) and the GUEST (virtual machine) - which is just like any other computer on the network - requiring virtual network card, virtual hard drive, CPU, RAM, etc.

Microsoft Loopback Network Adapter can be used to transfer files from a Virtual Server Host & the Guest machine without having to create an external network.

This article assumes that you have some knowledge of Virtual Server 2005 and that you have already setup a virtual machine - both host & guest running windows server 2003 SP1.

First you must intall Microsoft Loopback Adapter on the host computer.


  1. In the host operating system, right-click My Computer, and then select Properties. Depending on the style of the start menu, My Computer may be located in the Start menu.
  2. In the System Properties dialog box, on the Hardware tab, click Add Hardware Wizard.
  3. In the Add Hardware dialog box, click Next.
  4. When the Is the hardware connected? dialog box appears, click Yes, I have already connected the hardware, and then click Next.
  5. In the Installed hardware list, click Add a new hardware device, and then click Next.
  6. In the What do you want the wizard to do? list, click Install the hardware that I manually select from a list (Advanced), and then click Next.
  7. In the Common hardware types list, click Network adapters, and then click Next.
  8. In the Manufacturer list, click Microsoft.
  9. In the Network Adapter list, click Microsoft Loopback Adapter, and then click Next twice.
  10. If a message about driver signing appears, click Continue Anyway.
  11. In the Completing the Add Hardware Wizard dialog box, click Finish, and then click OK.
Reference: http://www.microsoft.com/technet/prodtechnol/virtualserver/2005/proddocs/vs_operate_ht_install_loopback.mspx?mfr=true

Now configure the loopback adapter.


  1. On the host operating system, open Network Connections, right-click the local area connection for Microsoft Loopback Adapter, and then select Properties.
  2. In the Microsoft Loopback Adapter Properties dialog box, verify that the Virtual Machine Network services check box is selected.
  3. Click Internet Protocol (TCP/IP), and then click Properties.
  4. On the General tab, click Use the following IP address, and then type the IP address and subnet mask (such as 192.168.1.1 and 255.255.255.0).

    Important

    You can use any Transmission Control Protocol/Internet Protocol (TCP/IP) address, but it is best to choose one from a reserved range of non-routable TCP/IP addresses. For example, TCP/IP addresses of the form 192.168.x.y, where x is a value from 0 through 255 and y is a value from 1 through 254, are non-routable. The value you choose for x must be the same on the host operating system and each guest operating system that is to be part of this virtual network. If your primary Ethernet connection uses one of these non-routable addresses, you must choose a different value for x to assign to Microsoft Loopback Adapter.
  5. Click OK, and then click Close.

    Notes

    To perform this procedure, you must be an administrator or a member of the Administrators group.

    To set up multiple network connections using Microsoft Loopback Adapter, use different subnets.

    Do not set a value for Default gateway.

Reference: http://www.microsoft.com/technet/prodtechnol/virtualserver/2005/proddocs/vs_operate_using_loopback.mspx?mfr=true

The key to making this work is to correctly set the IP settings of the loopback adapter.

In my case the IP address of my host computer is 10.140.7.32, mask 255.255.240.0. The IP address of my virtual machine is 10.1.1.11, mask 255.255.240.0.

On the host computer, I have set an IP address of 192.168.7.1 on the loopback adapter, a mask of 255.255.240 and no default gateway.















Now, create a virtual network from the virtual server administrator web page, name it "Loopback Network" and select "Microsoft Loopback Adapter" as the network adapter on the physical computer.

Once you have created the loopback network, edit the configuration of your virtual machine and add a new network adapter. - you will have to shutdown the virtual machine before you will be able to edit the configuration.


Set the new network adapter to connect to the loopback network, as show in the following.




Now start up your virtual machine and go into the network settings - you should see a 2nd network adapter - the one you just created.

Set the IP address of the loopback adapter (which I have called Host Network) to 192.168.7.2, and increase the last number as you add more virtual computers to this network if you also want to add loopback adadpters to them. For example, the next virtual machine will have an IP address of 192.168.7.3 for its loopback adapter. Remember we added an IP address of 192.168.7.1 to the host loopback adapter - so in essence we have created a network using 192.168.7.x.

To transfer files, map a drive to the host computer from the virtual machine using the hosts loopback adadapter address, so you can make a drive to "//192.168.7.1/c$" from the virtual machine for example.

Thats it!

Hope that helps.

Abdul.

Lookup Columns in Sharepoint

In Portal Server 2003 there was no easy way to create a lookup column that could be used across lists within a site. Microsoft Office SharePoint Server (MOSS) 2007 has taken care of this issue.

The concept is known as Site Columns.

So lets look at how we can create a site column and an example of its use.

First browse to a sharepoint site, click on "Site Actions" > "Site Settings"




From the next screen select "Site Columns", located under the galleries section.




Click on "Site Columns".

Now you will be presented with the "Site Column Gallery" Screen, click on "Create" to create a new site column.


Once you click on "Create" you will be able to enter the details into a form, enter a name for the column, select "Choice" for its type, and enter the valid lookup values in the "choice" section below, and select "Drop-Down Menu" as shown in the following.




Click OK and your site column will be created.

Now we have essentially created a lookup field that we can use in any list that we create within this site.


Lets create a list and use our Site Column to popuate a lookup field.

To create a list go back into "Site Actions">"Create" and from the "Custom List" section, select "Custom List", as shown in the following.


Click on "Custom List" to create a new list.

Give the list a name and a description and click "Create" to create the list, as shown in the following.



A list is created with a "Title" column by default. Lets add a new lookup column to our list to test our Site Column.

Go into "Settings">"List Settings". from the list page, scroll and and select "Add from existing Site Columns".

From the drop-down menu - "Select a site column from" - open and select "Custom Columns". Recall when we created a new site column we selected the "Custom Columns" group.




From the list of site solumns, select the one you created, "myCategories" and click on "Add>", click OK and navigate back to the List Page and click "New" to create a new list item.

This will present the list entry form, here you will see a lookup which is populated from the Site Column we created earlier.





I hope this article has been helpful.

Abdul.

Saturday, 28 April 2007

Where Is the Hosts File

The windows hosts file is located in a strange location and I am often asked of its location.

It can be found in C:\Windows\System32\Drivers\etc\hosts

Most often at a client's site I am developing a SharePoint Web Part or Library and while my laptop is connected to the client's network, most often my laptop is not a member of the corporate domain, an entry in the hosts file can make it easier to connect to sharepoint sites - without having to remember server IP addresses, etc - as no DNS Server is available to resolve names.

It is also a great way for Sharepoint developers to point to newly provisoned SharePoint sites that have been provisioned on the same port - without having to worry about host headers, etc.

More details on this later on another day.

Store Your Favourite Links Online

I often leverage the web to help perform my work.

AND often link to articles for future reference.

Here is a great way to store your favourite links online so you can access your favourite links from any computer at any time.

http://del.icio.us

What interests me more about this tool is that it also allows me to see what others have linked to for a particular topic.

Hope you find it as productive as I do.

Thursday, 26 April 2007

Using Sharepoint Lists to Manage Master Data (Lookups)

A common need for any development project is to create an administration module and set of CRUD(Create, Read, Update, Delete) screens to service master data - lookups.

Why not use SharePoint to manage this data, after all SharePoint is just like any other database, with tables full of columns (lists). The beauty with using SharePoint lists to manage master data is that SharePoint already has screens to allow users to create and maintain this data, with a very comprehensive security model ready to go out-of-the-box.

So lets look at an example ...

Wednesday, 25 April 2007

MOSS Web Part That Uses Microsoft.Sharepoint namespace

My first MOSS Web Part.

I needed a web part to sum up a numeric column of a list. Just like any other developer, I didn't think to look for one, rather I created my own.

Here are the steps necessary to create my first web part.

Summary of tasks.


  1. Create Class Library
  2. Create a List in MOSS
  3. Deploy Assembly in Sharepoint bin dir
  4. Edit Sharepoint web.config to allow Sharepoint to use Web Part (SafeControl)
  5. Make necessary adjustments to fix security issue with using Microsoft.Sharepoint namespace (WSS_minimal vs WSS_medium, etc) in class library
  6. Test web Part
Create Class Library

Create a new class library project in Visual Studio 2005.



Add a reference to System.Web & Microsoft.SharePoint (Microsoft.SharePoint.dll)



The Microsoft.Sharepoint reference can be copied from the MOSS Server.

Your class needs to be derived from the WebPart base class as shown in the following.




So now that we have the basic foundation completed, lets get on with developing our Web Part.

Override the RenderControl method of the WebPart base class, and add a using statement to the Microsft.SharePoint namespace.



Now the code will probably not make much sense right now, and your probably wondering how it's ever going to work with references to a list named "Test", and a column named, "aNumber". So lets create these elements on our Sharepoint Server - before we deploy and test this piece of code.

Create a List in MOSS

Navigate to your development MOSS site and create a list, by viewing all site content and clicking on the "Create" action and then selecting "Custom List".





When creating your list, make sure you name it "Test" and add a numeric column named "aNumber" as shown in the following.





The list needs to be named "Test" with a column named "aNumber" because these values are hard-coded in the source code. In the next version we will extend this Web Part to make the list name and the column(s) to sum as input values.

Add the list to the site.



Once you add the list to the site, add a few new items in the list.





Deploy Assembly in Sharepoint Bin Dir

From IIS locate the home directory of your MOSS deployment.




Copy the assembly (myWebPartForBlog.dll) of the previously built class library to the bin directory of the MOSS deployment. - which in my case is the folder - C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin.

(Create the bin directory if it does not exist).

Edit SharePoint Web.Config

Now go and edit the web.config file for this site, which will be located in the root folder for this site. - in my case it is located at C:\Inetpub\wwwroot\wss\VirtualDirectories\80.

In the SafeControls section of the web.config file add the new web part as a safe control by adding the line:



The assembly is the name of the compiled dll (without the .dll), the namespace is the namespace used in the source file, which in my case is the same.

This is an important step, I have found this to be a sensitive area, even the slightest difference in this line will stop the web part from appearing in the web site - the tags, assembly name, namespace are all case sensitive.

Once you have edited the web.config file, go into your MOSS site and edit the settings, by clicking on "Site Actions" > "Site Settings" and select "Web Parts" from the Galleries area.



You will see a big list of all the Web Parts that are available to your sites, click on the "New" link to add our custom web part.




Once you have clicked on "New" you will see another list of Web Parts to choose form, scroll down and locate and select "myWebPartForBlog.SumListColum" and select "Populate" at the top of the screen.




If at this point you cannot see the newly created Web Part, ensure you have entered the information in the web.config file correctly. - check all case sensitivity, spelling, etc.

Now you might think your Web Part is available, try and add it to your site.



- You will receive a security exception - an error similar to the following.



This error is due to the fact that our classs library is making a reference to the Microsoft.SharePoint namespace, which requires a higher level of trust than is normally available in a minimum trust environment (default).

This presents two issues: a) our site on which we have dropped this web part is no longer available because we have not handled the security exception, b) we need to resolve the security exception so our web part will function.

There is an excellent article that describes this better than I can, read it here.

As described in the artcile there are a number of approaches to resolve the security issue. In my opinion the best approach is to leave the assembly in the bin directory and write a custom security policy for our assembly to allow it to be able to access the resources it needs to function (which in our case is the Microsoft.Sharepoint namespace and any related assemblies).

However, for the purposes of this article, the simplest approach is to deploy the assembly into the GAC - which in some cases may in itself be a breach of securiy, as this means the assembly is made available to a wider group of applications (as it will be in the GAC).

In a later version of this assembly, I will write a version that uses the custom security policy approach (Watch this space).

On the error page (above), you will see a link to the "Web Parts Maintenance Page" for the current site, you can go into this page and close the "bad" web part so atleast your site is available to users while we resolve ths issue.

Fix Security Issue

Lets first sign the assembly so we can deploy it into the GAC.

Open up the project (if not already open) in visual studio 2005.

From visual studio, select the project properties from the "Project" menu, you will be presented with the following screen.



On the left hand side, notice the signing tab, click it to select it, and on the next screen, select "sign the assembly" and from the "Choose a strong name key file" drop down choose "New"




When you select "New", enter a file name for your key file on the next popup screen as in the following and click OK to continue.




You should see a new key file in your solution explorer, build your project and re-deploy to the bin directory of your sharepoint deployment location as described above.

Now we need to add our assembly to the global assembly cache (GAC) as all assemblies in the GAC run under full trust - which is what the reference Microsoft.SharePoint needs. Only administrators can add assemblies into the GAC - hence why assemblies in the GAC are considered safe and are provided full trust.

On your MOSS Server, locate the gacuil.exe for .Net 2.0, in my case it is in the c:\windows\system32\dllcache directory and enter the following command from a dos prompt. (dos prompt can be accessed from start>run> then enter "cmd" and press OK.

gacutil.exe /i [location of your assembly]\myWebPartForBlog.dll




Now we have to remove the previously added Web Part and re-add it so that the new version can be correctly referenced in the Web.Config file.

From the top level site, go into "Site Actions">"Site Settings">"Modify All Settings"

Select "Web Parts" from the Galleries area.

Scroll down and find our custom web part, "SumListColumn.webpart", click on the edit icon, and then select "Delete Item" when the item properties popup.

Now lets find our assembly in the GAC, lets get a list of all the assemblies that have been added into the GAC and copy this into a text file, use the following command form the dos prompt.

gacutil.exe /l > assemblies.txt

Now open this text file by issuing the following command from the dos prompt

notepad.exe assemblies.txt

Search for our assembly by searching on its name, myWebPartForBlog.

Highlight the entire line when found and copy it as shown in the following.



Open the SharePoint web.config file and change the previously added line with the following



Now go into the site settings of your top level site and re-add the web part to the web part galleris area as described above.

Notice this time when you add the new item and scroll down the list of available web parts to add, you will see a value in PublicToken - before this was null.




Select this web part, and click on populate above.

Test Web Part

Now, go to the test site - the same site in which we previously created the "Test" List, make sure you add a few rows and then drop the web part under this list and you should see a total for the numeric column, as show in the following.




Go into the settings of the web part (Edit) and make the direction "Right to Left" to correct the alignment.

Thats It!

Hope you enjoyed it,

Feel free to leave any comments.