Skip to content

Code Snipit Management Tools — Why?

Perhaps it’s because it’s late while I’m writing this … perhaps I’ve missed some big revolution in software development but I don’t understand…

Why do we need code snipit tools?  Shouldn’t we have classes and methods (even if they’re static) rather than fancy tools to do the same copy-paste routine we’ve been telling developers not to do for 30 years?

Why can’t we all make an effort to put our code in reusable “chunks”?

How to Copy a Web Part

This how to describes the process for copying a web part.

  1. Open a SharePoint site.
  2. Make sure you’re in Shared Mode.
  3. Enter design mode
  4. On the web part to copy, click the down arrow on the right and select export.
  5. Save the file on your system
  6. Go to the site where you want the control added
  7. Enter Shared Mode
  8. Select Add Web Parts-Import
  9. Browse to the file and click import
  10. Drop the web part on the page.

You’ve now copied a web part from one web page to another.  Note that this won’t work for ListViewWebParts or ListFormWebParts.

How to Strong Name a Web Part

Strong Naming web parts helps to make sure that they can not be tampered with – just like signing any .NET assembly does.  However, because of the nature of SharePoint, there are a few additional steps that are required after the assembly is strong named to ensure that the web part can be deployed and correctly loaded.

The process involves two major steps: Strong Naming the Assembly, Changing the DWP file(s).

Strong Naming the Assembly

Strong naming the assembly is covered in a separate document. Signing the web part assembly itself is no different than any other .NET assembly. See How to Strong Name an Assembly

Changing the DWP File(s)

The DWP file that Visual Studio creates uses the name of the assembly without a strong name.  The fully identified assembly name must be used to load all strong named assemblies, so if the DWP file is not modified it will fail to load the assembly – and you’ll get an error message that the control is not marked as safe.

The quickest way to get the correct assembly definition in the DWP file is to use GACUtil – which installs the DLL into the GAC.  The process for getting the full assembly name and adding it into the DWP appears below:

  1. Open a command prompt and navigate to the project’s bin\debug directory.
  2. Type GACUTIL /I MyAssembly.dll where MyAssembly.DLL is the name of the assembly for the project.  You’ll see a message that the Assembly was successfully added to the cache.
  3. Type GACUTIL /u MyAssembly where MyAssembly is the name of the assembly for the project – without the DLL extension.  You will see a set of messages which show the full name of the assembly including it’s publickeytoken.
  4. Copy the Uninstalled line, including any additional information that appears on the next line to the clip board.
  5. Go to Visual Studio and open the DWP file.
  6. Paste the copied text between the and tags replacing what was there.

How to Create a Deployment CAB project

Deployment of web parts is done through CAB files.  These cab files contain a manifest.xml file that explains the contents of the CAB file, at least one DWP file which specifies the properties for a web part and normally one or more DLL files which are the executing code for a web part.
Visual Studio creates a template Manifest.xml and a sample DWP file for you when you create a new SharePoint project.  This “how to” document will show you how to create a CAB file project in Visual Studio.  Follow these steps to create the cab file project:
  1. Right click the solution in Visual Studio and select Add-New Project.
  2. In the Setup and Deployment Projects select Cab Project
  3. Enter a name for the project. Ideally this is the name of the web part project followed by “CAB”.
  4. Verify that the Location is the correct location.
  5. Click the OK button
  6. Right click the new cab project, Click Add-Project Output
  7. In the project drop down in the dialog that appears select the project file of the web part – if necessary.
  8. Click Primary Output and hold the control key down while clicking Content Files so that both are selected.
  9. Click the OK button.

If you need to add additional DWP files to the project, make sure to set their build action to content.  This can be done by selecting the DWP file, right clicking, selecting properties and setting build action to content.

How to Strong Name an Assembly

A strong name is a cryptographic public/private key pair that is applied to an assembly in order to prove that it has not been tampered with after compilation.  The process of strong naming an assembly has three components: creating the key, adding the key to the project, and adding the key to the assembly.  We’ll look at each part in turn.

Creating the Key

The process of creating the key is relatively straight forward as Visual Studio ships with a utility that creates the key for you.  Follow these steps to create the key:

  1. Start a Visual Studio Command prompt.
  2. Navigate to the project directory for the project.
  3. Enter the command ‘sn –k myproject.snk’ where myproject is the name of the project.  The key that is created will have the name of myproject.snk.
  4. Close the command prompt.

You now have a strong name key which can be added to the project.

Adding the Key to the Project

Now that you have the key, you need to add it to the project.  This is done to ensure that Visual Studio will manage the check in and check out process for you.  Follow these steps to add the key to the project:

  1. Right click the project, select Add, Add an existing item
  2. Change the Files of type drop down to All Files (*.*)
  3. Select the myproject.snk file and Click the OK button.  As before myproject should be replaced with your project name.
  4. Select File-Save All from the menu.

You’ve now added the key to the project.  It will be checked in and out when you select those commands in Visual Studio.

Adding the Key to the Assembly

The final step in the creation of a strong name is to add the key to the assemblyinfo.cs file.  This file was already added to your project for you.  Here’s how to add the key to the assembly:

  1. Open the AssemblyInfo.cs file.
  2. Locate the line [assembly: AssemblyKeyFile(“”)]
  3. Swap the set of double quotes with @”..\..\myproject.snk” so that the line looks like [assembly: AssemblyKeyFile(@”..\..\myproject.snk”)].  As above, replace myproject with your project name.  Note that the at sign says to C# that you don’t want to use escape characters.  The pairs of periods are parent directory transitions.  These are needed since the signing process takes place in either the bin\debug or bin\release directories – thus the key file, which is in the root of the project, is two levels above the assembly.
  4. Save the file.

You’re successfully added a strong name to an assembly.

Article: Concrete Code for a Quick Configuration

In my last article, ” Caught Up in Code, or Quick Configuration”, I proposed that you build more configuration based software solutions and less software that is governed by the relatively rigid rules of code alone. That article focused on the concept of configuration-based development. In this article the focus is on using the patterns for implementing a configuration-based solution.

A Few Examples

Before we drop into the details of a code sample, here are a few quick examples of how you can apply the configuration based pattern in the code you’re writing.:

  • Factory Pattern – Using reflection, generics, interfaces, and a configuration file, you can create a strongly typed factory that can generate objects of any type. You wouldn’t have to worry about ever building another factory class.
  • Optional Display – User Interface trimming, in other words, removing items from a menu when the user doesn’t have access to them is a time consuming process. However, a control can be built uses a list to determine the menu options and performs the security check as a part of drawing the menu. You wouldn’t have to worry about trimming my UI because the control manages security automatically.

Unghosting — themes are not your friend

One of the things that I was reminded of today was that anytime you apply a theme to a SharePoint site the site gets unghosted.  I can’t wait until this isn’t an issue…

Article: A Sensible Framework for SharePoint Intranet Navigation

It’s never the first thing that I get asked on an engagement. It’s never the burning question in the minds of my customer as they seek out a consultant to help them with SharePoint. However, sooner or later it eventually comes to the topic of navigation. Although the fundamental concepts behind a sensible navigation framework are simple, they are not well understood by developers or designers. This article is designed for both audiences as they seek to make SharePoint usable.

Foolish Consistency is the Hobgoblin of Little Minds

Emerson (who said “Foolish consistency is the hobgoblin of little minds”) is a personal favorite author of mine;
however, he didn’t have to train hoards of users how to use a new interface. There’s definitely something to be
said for creating the kind of navigation that users are accustomed to. In the world of the Web that means
navigation belongs on the top and on the left.

Nearly every non-marketing Web site will follow a pattern with navigation on the top and on the left-hand side.
While what is on the top and left side differs, the fact that these spaces are reserved for navigation normally does
not. So if you’ve got a wild side, or feel like training scores of users, you can move the navigation — otherwise
we’re more or less stuck with the accepted norm.

Please enjoy a free copy of this article, also located on our Gifts page

Picking up the SharePoint Services Navigation Controls

When you get into modifying site definitions you’ll run into a few interesting challenges.  Not the least of which are all of the different versions of a page that you’ll have to update in order to make the whole site to fit together.  One of the interesting aspects is the list pages.  Each list has it’s own set of pages.

On those list pages are some web controls that control view navigation and another one that controls the related links — both of them are on the left side of the page.  If you want to get to one master page without modifications these web controls present a problem.  (In other words, I want to manage one aspx page and be able to copy that into each of the files in each of the lists.)  Those pesky web controls make that not possible.

However, there is a solution.  You can encapsulate the ViewNavigation and RelatedLinks classes in the Microsoft.SharePoint assembly into web parts and use them as web parts.  In that way, you can change the left column to a site navigation web part zone and then insert web parts for ViewNavigation and RelatedLinks on the pages that happen to be list pages.  Then you can insert your own navigation above or below these standard web controls — and you can swap them out later without having to deal with the “thou shall not modify a site definition after it has been created rule.”  (I may have paraphrased that a bit.)

In general it looks like the WebControls in the Microsoft.SharePoint.WebControls namespace will encapsulate well into web parts so you can reuse the built in web controls — even if you want to move to a web part basic design.

As a sidebar, the Microsoft.SharePoint.Portal.WebControls seem to all derive from WebPart so you won’t even have to encapsulate them.  All that’s necessary there is to create a DWP file.

The common administrative pages are still a challenge — but it’s getting easier bit by bit to create a cohesive site definition.

By the way, here’s the entire file I used to encapsulate the RelatedTasks web control…

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace SPBuiltInControls
/// <summary>
/// Shell for Related Tasks web Control
/// </summary>
[ToolboxData(“<{0}:RelatedTasks runat=server></{0}:RelatedTasks>”),
public class RelatedTasks : Microsoft.SharePoint.WebPartPages.WebPart
protected override void CreateChildControls()
base.CreateChildControls ();
Microsoft.SharePoint.WebControls.RelatedTasks related = new Microsoft.SharePoint.WebControls.RelatedTasks();

… I got lazy and didn’t even bother removing the extra using statements…

Recent Posts

Public Speaking