Wednesday, May 27, 2009

Enable Anonymous Access on a SharePoint Site

Today I was working on a SharePoint site that needed to be accessed by people outside of our domain.  Assuming you have your site created in the stand way, Users would normally access it via the NTML (windows Challenge/Response) protocol, i.e. SharePoint knows who you are from your domain login.  This was not possible for my Users as they would not be allowed have usernames on our domain.  Therefore the solution was to enable Anonymous access to the site.

1) Access SharePoint Central Administration and select Application Management from the left hand links.

2) Under Application Security select Authentication providers, then select the Web Application using the drop down on the tool bar.  In my case it was “SharePointPortal – 80”.

image 

3) Click on the” Default” zone link and tick the “Enable anonymous access” check box and press Save.

image

This enables Anonymous access on the Server, however each site still needs to be configured to use this setting.  This prevents people from making information available to external sources in error.

4) On the specific site login as the SharePoint Administrator or another User who has assess to the SharePoint Site Settings menu.  From the SharePoint Site Setting screen select “Advanced Permissions”

image

5) From the Advanced Permissions page select Settings/Anonymous Access from the toolbar menu.  When that screen appears you can select the level of access an anonymous user can see information.

image

The options are self explanatorily and in my case Entire Web site was the correct option.  Save this option by Clicking OK.

The site should now be accessible to external Users to view.

Tuesday, May 26, 2009

Google killer? Probably not

Keeping track of new developments on the net is never easy, there is so much going on that usually its all just white noise.  Every now and again you do come across something interesting so I figured I’d do a post in case someone else gets some ideas.

It was all over the recent Tech news that there was a new browser in town called Wolfram Alpha.  This takes a different approach from Google in two ways. First is that it’s a closed system i.e. only information that is vetted by experts is allowed in, which will supposedly increase accuracy. Second is that it takes real English questions like “population of Ireland in 2008” or “convert 100 euro to US dollar”.

image

Having played with it for a while I’m not really impressed, but it’s an interesting idea.

Error running Powershell Scripts

Now that I’m generating Sharepoint sites using Powershell scripts I noticed an error appearing on the servers.

"File xxx cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.".

image

Turns out that for security reasons running scripts this way is disabled by default.  Check out this blog posting for more information on the various options available.

Solution

From within Powershell type the command “Set-ExecutionPolicy Unrestricted” and then “exit”.

Friday, May 1, 2009

SharePoint Development – Creating a webpart

This is the first of many posts which will try to put together the steps you need to develop within the SharePoint platform.  First off it I’ll describe creating a simple WebPart that puts a message on the page. 

Creating the solution

To help with the general issue of creating your Visual Studio 2008 solution we use a handy tool called STSDEV.  More information on this can be found on the projects homepage on Codeplex

Browse to c:\Program Files\StsDev\ and run StsDev.exe.

image

Enter the name of the solution you want to create, in this example I’ll use “EIWebPart”.  Browse to D:\Development\EIPortal\ as the Parent directory.  Select C:\development\eiportal\eiportal.snk as the Signing Key (you can get the latest version of this from SourceSafe).  Select “Web Part Solution (C# Assembly) as the solution type and “Visual Studio 2005 with .Net 3.0” as the version type.  “Why?” I hear you ask?  Well because the version of STSDEV on the VM is not working for some reason and I’ve not had a chance to find out why. yet….

image

All going well, you should see this message.

Working in Visual Studio

Browse to the location shown by STSDEV and open the solution file, allowing for the usual conversion questions for an upgrade to Visual Studio 2008.  The conversion should happen without any errors.

image

You’ll end up with a solution with the following structure.  This structure is very important to sharePoint development as it exactly matches the “12” (aka. “Hive” or “12 Hive”) directory on the server.  The various XML files are also very important as these are used when building and deploying solution.

  • feature.xml – contains the link to the webparts file, version numbers and feature information for the solution.
  • webparts.xml – contains the links to the various webparts in our feature.
  • CustomWebPart1.cs is the code file containing all our work.

We only want one webpart in our solution to I remove from the feature.xml and webparts.xml all references to CustomWebpart2.cs.  Delete the CustomWebPart2.cs and CustomWebPart2.webpart files.

CustomWebPart1.cs

Is a very simple file, it simply adds a able to the page and fills it with the text “Hello”.

protected override void CreateChildControls() {
  Label lblHello = new Label();
  lblHello.Text = "Hello";
  Controls.Add(lblHello);
}

Webparts work and a coded exactly the same as ASPX files, the only exception is that you can’t use the design pallet.  That means you can drag a Text Box from the Tools and have VS create the code for you, you need to hand code this.  My advice is to keep your webparts simple and avoid anything to crazy like messing with viewstate or late data binding.

feature.xml

This feature file contains the information about your feature, it’s name a description, version number etc.  If lets as your feature contained lots of different parts, each part would have an entry in the ElementManifests section.

<Feature
  Id="6FA7C22C-52A7-4A46-B944-44E5E7FECE2B"
  Title="A sample feature: EIWebPart"
  Description="This SharePoint solution was created by the STSDEV utility (http://codeplex.com/stsdev)"
  Version="1.0.0.0"
  Scope="Site"
  Hidden="false"
  ImageUrl="EIWebPart\AfricanPith32.gif" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest
      Location="WebParts.xml" />
    <ElementFile
      Location="WebParts\CustomWebPart1.webpart" />
  </ElementManifests>
</Feature>

I’m not going to worry about changing anything here, I’m sure you get the idea.

WebParts.xml

Again a simple file that just points to each webpart contained in the feature.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="EIWebPart" List="113" Url="_catalogs/wp" Path="WebParts" RootWebOnly="True">
    <File Url="CustomWebPart1.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="EIWebPart" />
    </File>
  </Module>
</Elements>

CustomWebPart1.webpart

This file is one to watch, it contains all the information needed for the user to interact with the webpart.

<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="EIWebPart.CustomWebPart1, EIWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eb8e38decd6217c5" />
      <importErrorMessage>Error importing Web Part</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">Default title for CustomWebPart1</property>
        <property name="Description" type="string">A demo Web Part created by stsdev</property>
        <property name="ChromeState" type="chromestate">Normal</property>
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="ExportMode" type="exportmode">All</property>
      </properties>
    </data>
  </webPart>
</webParts>

You get certain properties by default but you can add other is you want.  A good example would be if you wanted to know which Database to connect to, you may have a <property name="DB" type="string">DevWebCDB</property> entry in this file.  While in your C# file you have a public property called DB {get; set;} which your code can use.  But the SharePoint admin can use the SharePoint UI to change this value to anything else later.  This is very handy, but out of scope of what I’m trying to do at the moment.

Ready to go!

All you need to do now is compile the solution, so your configuration should be set to “DebugBuild”.  If everything is building correctly you only need to change this to “DebugDeploy” and build the solution again to place your feature into the servers “12 Hive” directory.  Cool or what!!

image

If you look in the output window you’ll see a whole bunch of STSAdmin commands which is Visual Studio building and then deploying your feature.  You could do this manually, but if VS does all the work anyway, what’s the point of that?

Browse to one of the sites on your VM. 

image

Select “Site Actions” / “Site Settings” from the control menu.  Then select “Site collection Features” on the page. You’ll see that on the last column on the right hand side.

image

An here we go, our new webpart has been added as a resource that an be used by any site collection.  Click “Activate”.  Then browse back to the Americas home page. 

Select “Site Actions”/”Edit Page” and click “Add Webpart” at the top of the first Web Part Zone.

image

Scroll down a bit and you’ll see our webpart, click the check box next to it and click “Add”, then “Exit Page Edit”.

image

The Webpart should appear on the top of the page.

Friday, April 24, 2009

Short Rant on StringBuilder and String

I’ve been running into lots of code which seems to be using Strings to build up HTML or Error Messages.  Unfortunately there is lots of inconsistency in the way it’s been done, but for anyone interested here is my take on it.  First off let me say any hard coding HTML into a code behind is generally a bad idea (don’t get me wrong I’ve done it myself lots of time) so try to avoid it at much as possible.  If however you are unable to avoid it, please review the examples below and try to keep to using the “Good” technique.

The Good

Here is a reasonably good example of code for building HTML in a code behind if you can’t avoid it.  Using StringBuilder is faster and better suited for concatenation than the primitive String type.  Also using the AppendFormat function allows you to use the {#} place mark which ensures your value goes into the correct location, making the string far more readable and maintainable.

StringBuilder sbTemp = new StringBuilder();
sbTemp.AppendFormat("<a href=\”mailto:{0}\”>{1}</a>",
                                                          ConfigurationManager.AppSettings["IT_EMAIL"],
                                                          ConfigurationManager.AppSettings["IT_EMAIL"]);
litITEmail.Text = sbTemp.ToString();

 

The Bad

Here is a bad example of the same code using String Builder.  although it will accomplish the same goal as above it requires more code and is much more difficult to read or maintain.
sbTemp.Append("<a href=\"mailto:");
sbTemp.Append(ConfigurationManager.AppSettings["IT_EMAIL"]);
sbTemp.Append("\">");
sbTemp.Append(ConfigurationManager.AppSettings["IT_EMAIL"]);
sbTemp.Append("</a>");
litITEmail.Text = sbTemp.ToString();

 

The Down Right Ugly (and wrong on so many levels)

Here is the wrong way to do it and anyone doing this should be taken out and shot.
string s = "<a href=\"mailto:";
s += ConfigurationManager.AppSettings["IT_EMAIL"];
s += "\">";
s += ConfigurationManager.AppSettings["IT_EMAIL"];
s += "</a>";
litITEmail.Text = s;

Saturday, April 4, 2009

Crazy Open Source project!

I found out about this project the other day, ReactOS (http://www.reactos.org/en/index.html ) which is an open source version of Windows!  Apparently it will “eventually” replace MS Windows on the Users desktop without Users ever noticing the change. Oh my God!, is this a law suit waiting to happen or not?

It’s only in Alpha at the moment, but apparently it works well……

Friday, April 3, 2009

Web Testing Applications

One of the great things about the unit testing is that it breaks down tasks into small meaningful chunks; sadly testing web brings up a whole set of new issues.  With web applications the testing is usually based on a script or number of steps which could be spread over any number of different screens.  I’ve used a number of different tools to do this an all have issues and limitations and they all cost heaps of cash.  then I’ve discovered Selenium a free open source tool for doing just that.

Installing Selenium on your development machine

Selenium is a java based application so you need to install the latest Java Runtime which can be downloaded from the Sun website.  Run the installer taking all the standard defaults and this should place everything needed into your C:\Program Files\Java\ folder.

Next you need to download a copy of Selenium RC (selenium-remote-control-1.0-beta-2-dist.zip) from the application share or from the Selenium website to your drive. Open this file using Window Explorer or any Zip file reader and extract only the \selenium-remote-control-1.0-beta-2\selenium-server-1.0-beta-2 folder.

image

Extract these file to a folder d:\applications\selenium-server.

To start the service running you need to go to the command prompt and typing:

    d:
    cd d:\Applications\selenium-server\
    java –jar selenium-server.jar

image

You should start to see the output above showing that you now have a server running on port 4444.

Installing the Selenium Extension

Open the Fire Fox browser and select tools/Add-ons from the menu.

image

Select Get Add-Ons from the menu and type Selenium into the search text box and hit return. You should see Selenium IDE appear as the first item in the list. Select this and click the “Add to Firefox” button.  After installation restart Firefox.

image

There should now be a new item added to your Tools menu called Selenium IDE. 

Recording a test

While on the webpage select Selenium IDE from the Tools menu in Firefox.

image

A dialogue box should appear and you can notice that the red circle in the top right hand side indicates that you are now in recording mode.

On the page dome some work, in this case I placed the word “Fred” into the search field and clicked Search.

image

Select the “Page” text (or any text for that matter) on the page and right click.  You should see a submenu stating “Show All Available Commands” and within this “AssertTextPresent Page”.  Assert is the test in this case, you are testing that “Page” text will exist following the search.

image

Going back to your Selenium IDE should present you with 4 commands, one for each of the steps you’ve taken.  Click the red button in to top right of the screen to stop recording your steps and Save by click on File/Save from the menu.

Running the test

Once you have a saved test, you can run it at any time in the future by opening if from disk using File/Open from the menu.

image

Click the green play button on the tool bar to run your test, ensuring that the Base URL is correct.

image

If all the steps turn green you have successfully run the tests.

Adding the Selenium to Integration Server

The great thing about Selenium is that you can build each of these tests into the continuous integration server. 

image

From the options menu select Format/C# – Selenium RC.

image

This will present your test in C# code which can be cut/paste into the standard UnitTest infrastructure.