Archive

Archive for the ‘.Net’ Category

Registered for Microsoft PDC 09

August 26, 2009 Lee Dale Leave a comment

Registered to attend the Microsoft Professional Developers conference in Los Angeles in November. Hoping to learn a lot about programming for Windows 7 and Server R2 and also want to get a lot more knowledge on Visual Studio 2010 and .Net 4.0.

Looking forward to this along with the SharePoint Conference in Las Vegas in October and this is my first time to the big developer’s conferences in the U.S; it should be a good experience.

You can more info at the PDC site.

Localizing a SharePoint UI using ASP.Net Resource Files

August 5, 2009 Lee Dale 1 comment

I have a requirement for a project I’m working on to make all strings in the UI of a SharePoint 2007 site localized. The problem was that the rest of the site collection needed to stay in English, this meant all the system and admin pages needed to stay in English while all the user interface needed to be localized. The requirement also stated that an admin of the site collection could change this is the admin pages.

I immediately turned to ASP.Net resource files as this would achieve exactly what I wanted, I wanted to use the Regional Settings -> Locale setting in the site collection to determine the locale to show. The problem with this was that changing the sites Regional Settings Locale had no effect on the CurrentUICulture that the current ASP.Net thread was running under, this meant that ASP.Net would load the resource file for the user’s browser not the resource file for the locale of the site collection.

I used the following blog post from Mikhail Dikov which is a good example of how to use ASP.Net resource files in a SharePoint site. The blog post fails to mention however that for the culture to change within a SharePoint site you need the language packs installed and this would affect the whole site collection, admin pages and all.

So the following is an outline of how I achieved my goal:

  • First I created a default resource file which contained all the English strings and then resource files for each locale I wanted to support.
  • Next I deployed these resource files into my SharePoint web applications App_GlobalResources folder (see Mikhail’s blog post on how to deploy your resource files using a SharePoint feature).
  • I then added the following code to my page layout which changed the current ASP.Net threads UI culture to the same culture of the SharePoint site collection:

<script runat=”server”>
protected
override void InitializeCulture()

{

System.Threading.Thread.CurrentThread.CurrentUICulture = Microsoft.SharePoint.SPContext.Current.Web.Locale;
base
.InitializeCulture();

}

</
script>

This made sure that each time the page loads the ASP.Net culture was set to the correct site collection culture.

  • Now that the correct ASP.Net culture is selected in the page we can go ahead and localize our strings in the page layout like this:

    <asp:Literal runat=”server” Text=”<%$Resources:MyResourceFile,MyLocalizedStringKey %> />

    Note that we can also just use <%$Resources:MyResourceFile,MyLocalizedStringKey%>in any ASP.Net web controls property if you don’t want to use a Literal control.

And that’s all there is to it, I think it’s a clean and simple way to localize the SharePoint UI without using language packs and localizing the whole site collection including admin and system pages.

Categories: .Net, ASP.Net, C#, SharePoint

Web.config Transformations with Visual Studio 2010

February 27, 2009 Lee Dale Leave a comment

When deploying a web application across your organizations hosting environments you need to make sure you make the correct modifications to your web.config on each environment. Currently with Visual Studio 2008 this means hand modifying each web.config to make connection strings point to the right database servers, changing the debug compilation attribute to false etc. Doing this for each deployment can be a major pain and can allow bugs to slip into your environments, for instance you may forget to modify your connection string and have your production application pointing to your staging server or have your application underperform because you have left debug compilation on.

Thankfully the Visual Studio team has come up with a great feature to be included in the next release of the IDE that allows you to keep a copy of transformations for each web.config. Your transformations will get applied to your main web.config when the application is deployed to each environment. This should greatly improve the deployment experience and help keep deployment errors down to a minimum.

Channel9 have a video all about it so head over to there and check it out.

http://channel9.msdn.com/shows/10-4/10-4-Episode-10-Making-Web-Deployment-Easier/

Categories: .Net, ASP.Net, Visual Studio Tags: ,

New website: www.moggymart.com

February 3, 2009 Lee Dale Leave a comment

A while back I decided to build a website for allowing people to post free classified advertisements for pets and animals; I decided to use ASP.Net 2.0 with AJAX Extensions and SQL Server 2005. User registration and authentication was done using the ASP.Net Forms Based Authentication with SQL Server Membership Providers.

As normally happens with side projects I gave up on it because I was too busy with my day to day projects and never touch it for a long while. Lately I picked it up again and decided to at least finish what I started so the first release looks like this www.moggymart.com

I plan to add more features when I get time and would be grateful for any feedback on the site.

Book review: Professional SharePoint 2007 Development

December 31, 2008 Lee Dale 1 comment

I will be frequently reviewing books on my blog from now on and I would like to start with a good wholesome SharePoint (MOSS 2007) development book entitled Professional SharePoint 2007 Development.

The book is written by some very knowledgeable SharePoint guys who have a vast amount of experience developing on the SharePoint platform. The book provides a good programmer to programmer learning experience from real world professionals who have been developing SharePoint solutions out in the wild for a number of years.

The authors of the book are: John Holiday, John Alexander, Jeff Julian, Eli Robillard, Brendon Schwartz, Matt Ranlett, J. Dan Attis, Adam Buenz and Tom Rizzo.

It’s a fairly large book some 716 pages long packed full of good solid SharePoint development techniques and good examples to guide you along the way.

The table of contents looks like this:

  • Chapter 1 – The Microsoft Application Platform and SharePoint
  • Chapter 2 – MOSS 2007 Overview for Developers
  • Chapter 3 – The SharePoint User Experience
  • Chapter 4 – WSS v3 Platform Services
  • Chapter 5 – Programming Windows SharePoint Services
  • Chapter 6 – A Sample Collaboration Solution
  • Chapter 7 – RSS, Blogs, and Wikis
  • Chapter 8 – Building Personalized Solutions
  • Chapter 9 – Using Enterprise Search
  • Chapter 10 – Using the Business Data Catalog
  • Chapter 11 – Building Document Management Solutions
  • Chapter 12 – Building Records Management Solutions
  • Chapter 13 – Building Web Content Management Solutions
  • Chapter 14 – Electronic Forms in MOSS 2007
  • Chapter 15 – Building Workflow Solutions
  • Chapter 16 – Business Intelligence and SharePoint Server 2007
  • Appendix A: Using the Microsoft Visual Studio 2005 Extension for Windows SharePoint Services 3.0

The book covers most if not all of the fundamentals to get you started building solutions with WSS 3.0 or MOSS 2007 and has whole chapters dedicated to building certain types of solutions such as using MOSS as a CMS platform and utilising the web content management features of the product.

I have had this book for a while now and find myself constantly referring back to it especially to lookup some XML I forgot.

As a summary this book covers a wide range of topics and focuses on teaching the right development techniques from an experienced developer’s point of view and is an excellent source to refer back to when you’re stuck. I would definitely recommend getting this if you are planning to do any development on the SharePoint platform.

I’ll be giving a copy of this book away as a prize in the near future but if you want to get your hands on it now then head over to http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0470117567.html to purchase it for a mere £31.99.

ISBN: 978-0-470-11756-9

Visual Studio 2010 will vastly improve Sharepoint development

November 25, 2008 Lee Dale Leave a comment

Microsoft is really improving the tooling support for Sharepoint developers in the next release of Visual Studio including being able to “F5″ debug from inside Visual Studio and VS handling all the package deployment, process attaching, app pool recycling etc which will be really helpful.

Also they will be providing lots of VS templates which give us some nice designers for things like WebParts and Features so we don’t have to worry about GUID matching and hand writing our XML files which should save bundles of time when setting up and new solution.

Channel9 have a cool video screen cast which gives a brief overview of what’s coming in VS 2010 for Sharepoint devs.

Visual Studio 2010 & .Net Framework 4.0

September 30, 2008 Lee Dale Leave a comment

So Microsoft has officially announced the next version of Visual Studio and the .Net Framework over on the MSDN site at http://msdn.microsoft.com/en-us/vstudio/products/cc948977.aspx .

I’m sure all us developers can’t wait to see what the next version of our beloved IDE has in store and what will be included in the .Net framework for version 4.0. Channel9 have a special area for Visual Studio over at http://channel9.msdn.com/VisualStudio/ and they will be running videos all this week focused on VS 2010.

Here is a link to the first video posted:

Norman Guadagno: Announcing Visual Studio Team System 2010

So go check out the site, watch the videos and get up to speed with what will be included in VS 2010.

Categories: .Net, Visual Studio Tags: ,

MOSS 2007 Forms Based Authentication using AD LDS and Windows Server 2008

September 11, 2008 Lee Dale 17 comments

Forms Based Authentication is an attractive option for companies running MOSS 2007 because it means you can give partners and external clients access to your Sharepoint sites without having to give them a full blown Windows domain account, the only drawbacks being it’s quite tricky to set up and most people only see SQL Server as an option for a user store which gives complications with regards to maintenance of them users and less flexibility should you need to migrate them users to a full blown Windows account later on.

I am currently running MOSS 2007 on Windows Server 2008 and I wanted to implement Forms Based authentication using AD LDS as the user store instead of SQL Server. The benefit for using AD LDS as a user store is that it is built on the same enterprise level authentication server as Active Directory and comes with a user management application that will be familiar to most Windows administrators unlike SQL Server which can only be administered using the Web Site Administration tool that comes with Visual Studio. You can also easily migrate an AD LDS user store to a full blown Active Directory domain controller if that requirement should arise in the future.

I couldn’t find much on the web on how to setup and configure AD LDS with Windows Server 2008 and as It’s a fairly tricky undertaking I thought I would write an article to hopefully give a clear and concise step by step guide on how to get it up and running.

Installing AD LDS

To start off with I am assuming you have MOSS 2007 installed on Windows Server 2008 already so the first step would be to add the AD LDS role.

  • Open up Server Manager and click on the roles section to the left.
  • Click on the “Add Roles” link on the right which should bring up the “Add Roles Wizard”.
  • Check the box next to Active Directory Lightweight directory services and click next as shown below.

  • Follow the wizard until the end and AD LDS should now be installed on your server.

Configuring AD LDS

Now we have AD LDS installed we need to create a user store.

  • Open up Administrative Tools and click on “Active Directory Lightweight Directory Services Setup Wizard” to start the setup process.
  • Click next on the first page and you will be presented with the step below, make sure “Unique instance” is selected and click next.

  • On the next step give your user store instance a name as shown below and click next.

  • Now you need to give your instance a port number so you can access it, recommended to leave the default values here.

  • Now we need to create a partition for our user store. Select “Yes, create an application directory partition” and enter a valid X.500 AD partition name. Make a note of this Partition name as you will need it later on.

  • The next step is to tell AD LDS where to physically store your data.

  • Choose which domain account you want AD LDS to run under, I have left selected the default Network Service Account.

  • Next tell AD LDS which account you would like to give admin rights to.

  • The last step is pretty important make sure you select the MS-User.ldif file to import; this makes sure you have the User classes available within your AD LDS store.

And thats it you now have a AD LDS store setup. Then next step is to add a user to your store.

  • Under Administrative tool you will now see ADSI Edit application, open this up.
  • Right click on the ADSI Edit node and select “Connect to” you will see the “Connection Settings” dialog box shown below.

  • Enter a name for your connection in the Name box.
  • Under Connection Point enter your Distinguished Name into the “Select or type a Distinguished Name or Naming Context:” box. This will be the Partition name you entered earlier on when creating the user store.
  • In the “Select or type a domain or server” box enter you domain name and port number you setup earlier.
  • Click the Ok button and you should now see your connection under the ADSI Edit node.
  • You can now add a user by right clicking on the Users container and selecting New -> object.
  • You will be presented with the dialog box shown below. Select the user class and click next.

  • On the next screen enter the users username into the value box and click next, then click finish.

  • You now have a user in your user store, to set the users password right click on the user and selected “Reset password”.
  • One more step you need to take is to enable the user by right clicking on the user and selecting “Properties”.
  • Scroll down to the ms-DS-UserAccountDisabled property and change it to TRUE.

Extending your web application

Now we have our user store in place we need to tell Sharepoint that we want to allow access to our sites for these users. You will need a separate URL and web application so that MOSS can distinguish from internal and external users and present them with the right authentication mechanism. For example my internal URL is portal.athousandthreads.net which will use Windows authentication and my external URL is partner.athousandthreads.net which will use FBA authentication. Both URL’s point to the same SharePoint site but each use different mechanisms for authenticating users.

Our first step is to extend the internal portal.athousandthreads.net application.

  • Open Central Administration and go to “Application Management”
  • Click on “Create or extend Web Application”.
  • Click “Extend an existing web application”.
  • You will be presented with the screen below.

  • Change the web application to your internal web application, in my case portal.athousandthreads.net
  • Select create a new IIS website and enter a name.
  • Enter port 80 into the port textbox and your host header into the host header section, in my case partner.athousandthreads.net, this will be your external URL.
  • Under the Load Balanced URL section make sure you change the Zone dropdown to Internet, this is very important.
  • Click Ok and you extended web application will be created.
  • Make sure you can navigate to your external URL, if you don’t get anything then make sure you have made the right DNS or Host file entries on your server.


Modifying web.config files.

At a minimum we need to modify the Central Administration and external web application web.config files you will also need to modify the SSP and MySite web.config files if you intend to use FBA with these services.

  • Open up your external web application web.config file and make the following entry just before the system.web node.

  • Be sure to change the connectionString attribute to match your own domain, port and partition name.
  • Now make the following entry inside the system.web node.

  • Change the connectionUsername and connectionPassword attributes to match your own credentials.
  • Save the web.config file and now open up your Central Administration web.config file.
  • Insert exactly the same entries in this web.config file as before.
  • Change the defaultProvider attribute of the roleManager node to AspNetWindowsTokenRoleManger, this is to ensure you can still access Central Administration with Windows authentication.
  • Save the web.config file and do and IISRESET for good luck.

Configuring authentication providers.

All we need to do now is tell Sharepoint that our external web application (partner.athousandthreads.net) will be using Forms Based Authentication.

  • Open up Central Administration and go to Application Management.
  • Click Authentication Providers and click the Internet zone on next screen. You should then be presented with the screen below.

  • Change the authentication type to Forms and enter the Membership provider name and Role manager name from your web.config files in the respective boxes.
  • Make sure you select Yes under Enable Client Integration.
  • Click Save.


Adding FBA permissions.

Ok so now our external web application is using FBA and our Central Administration knows about our AD LDS store, we can now go ahead and add an FBA user to our web application.

  • Open up Central Administration and select Policy for Web Application.
  • Add a new user and you should now be able to enter an FBA user into the people picker box.
  • Once you have added a user to the web application policy you should be able to log into the internal web application using Windows authentication (portal.athousandthreads.com) and begin adding your FBA user permissions to the site.

There you have it, It’s a bit long winded and a little tricky but pretty cool when it’s finally set up and working, trying navigation to you external web application (partner.athousandthreads.net) and you should be presented with a FBA login screen.

kick it on DotNetKicks.com

Setting a Site’s locale in SharePoint using SPLocale class.

May 14, 2008 Lee Dale Leave a comment

A requirement I came across recently was to write a tool to set a site’s locale from US to UK using the object model.  Because I potentially needed to do this for hundreds of sites I opted to write a small command line tool that can be scripted to help with the task.

Below is an overview of how to use the SPLocale class to set the Locale of a site.  I have also provided download links for the compiled command line tool and its source code for you to take a look at.

First thing we need to do is grab an instance of the site:

SPSite site = new SPSite(“localhost”);

 

Next thing we do is get a collection of all the web sites under your site:

 

SPWebCollection webCollection = site.AllWebs;

 

We can now interate over this collection and set the Locale property to your new locale which will be an instance of the CultureInfo class. We create a new instance of the CultureInfo class by passing the new Locale’s LCID value to it’s constructor.

 

foreach (SPWeb web in webCollection)

{

      web.Locale = new System.Globalization.CultureInfo(2057);

      web.Update();

}

 

Thats basically all there is to it.

 

Downloads:

 

Excecutable – http://www.athousandthreads.com/blog/code/SPSiteLocaleExe.zip

 

Source Code – http://www.athousandthreads.com/blog/code/SPSiteLocaleSource.zip

 

Example of command line:  SPSiteLocale.exe set http://localhost 2057

 

 

Categories: .Net, C#, SharePoint

Programmatically removing all WebParts from a page in MOSS 2007

April 15, 2008 Lee Dale 12 comments

So I haven’t been around blogging for a while due to having a little downtime and finishing up a project I’ve been working on. I’ve just started a new project which involves migrating a very large SPS 2003 implementation to MOSS 2007.

Hopefully I’ll be back blogging regularly with some useful SharePoint related articles and to start off with I’m going to demonstrate how to delete all WebParts from a WebPart page programmatically.

This requirement came up recently as I needed to create a feature to delete all WebParts from the standard MySite template and add some custom WebParts when every MySite was provisioned.

The steps to accomplish this were as follows:

  1. Create a feature that when activated deleted all WebParts from the page and added my custom ones on.
  2. Create a feature stapler to staple my new feature to the SPSPERS template so the feature is activated each time a new MySite was provisioned.

I’m only going to cover step 1 here as there are plenty of great articles out there that explain feature stapling e.g. http://blogs.msdn.com/cjohnson/archive/2006/11/01/feature-stapling-in-wss-v3.aspx

Ok first we need to create a class that inherits from the SPFeatureReceiver class like so:

public class CustomMySiteWebPartsFeature : SPFeatureReceiver

The SPFeatureReceiver class is an abstract class with the following four abstract methods:

  • FeatureActivated(SPFeatureReceiverProperties properties)
  • FeatureDeactivating(SPFeatureReceiverProperties properties)
  • FeatureInstalled(SPFeatureReceiverProperties properties)
  • FeatureUninstalling(SPFeatureReceiverProperties properties)

We need to override all four of these methods but we only need to write our code in the FeatureActivated method so leave the others blank.

The first thing we do is get an instance of the SPWeb class which we can do by using the properties variable that is passed to the method.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    using
(SPWeb web = properties.Feature.Parent as SPWeb)

    {
    }

}

Next we need to check that this is definately a MySite page we are working on just in case the feature gets activated on another site. We can do this by checking the WebTempate property of our SPWeb instance.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    using
(SPWeb web = properties.Feature.Parent as SPWeb)

    {

        if
(web.WebTemplate == “SPSPERS” || web.WebTemplate == “SPSMSITEHOST”)

        {
       

    
}
}

To actually manipulate WebParts on our page we need to get an instance of the SPLimitedWebPartManager
class like this:

SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(“default.aspx”,
    PersonalizationScope.Shared);

And as you can see we call the GetLimitedWebPartManager method of our SPWeb instance for the page we want to manipulate.

Now we can begin to delete the WebParts. Below is the code I used to accomplish this.

List<Microsoft.SharePoint.WebPartPages.WebPart> webParts = new
List<Microsoft.SharePoint.WebPartPages.WebPart>();

foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in manager.WebParts)
{
    webParts.Add(webPart);
}

foreach (Microsoft.SharePoint.WebPartPages.WebPart webPart in webParts)
{
    manager.DeleteWebPart(webPart);
}

I first add all the WebParts to a collection then interate over that collection calling the DeleteWebPart method of our SPLimitedWebPartManager instance. We need to do this because calling DeleteWebPage while interating over the WebParts collection causes an exception as the collection has been altered during the enumeration.

When this is done we can use the AddWebPart method of the SPLimitedWebPartManager passing it an instance of the WebPart you want to add, the WebPartZone id you want to add the WebPart to and the index at which you want to add the WebPart within the zone.

After all this is done don’t forget to dispose the SPLimitedWebPartManager instance and call Update on the SPWeb instance:

manager.Dispose();
web.Update();

And that’s all there is to it.

Hope it was helpful.

Categories: .Net, C#, SharePoint