Development

Using TLS with SmtpClient

A rather small change to your code can increase security by sending E-Mails via an encrypted connection.

Recently I stumbled across code, that send E-Mails with the System.Net.Mail.SmtpClient class. That piece of code did not try to communicated encrypted with the receiving SMTP server. So I changed that, to enable a TLS connection.

try
{
  var message = new MailMessage();
  _smtpClient.EnableSsl = true;
  _smtpClient.Send(message);
}
catch (SmtpException ex)
{
  // if the recpient mailserver does not support SSL, send without encryption
  _smtpClient.EnableSsl = false;
  _smtpClient.Send(message);
}

The change in my code was to enable TLS be default, and turn it off in case the receiving SMTP server does not support it. Everything else is untouched, which results in a small change in code to increase security.

The solution for warming up SharePoint

Most SharePoint Farms will have a solution for the long loading time after an Application Pool recycle or iisreset running. There are many different ways to preload websites, so your users have faster load times. So why another solution?

There are some questions, that I think have not been dealt with before:

  • Most solutions require some sort of Timer to be started (e.g. a Scheduled Task)
  • When should the warmup occur?
  • What about multiple WebFrontend Servers?
  • How about Claims support?
  • Which URLs have to be called? What about extended WebApplications?
  • New WebApplications require the warmup tool to be adjusted
  • Manuell warmup after Deployments
  • What about search?
  • Did the Farm warmup correctly?

Years ago I developed a console application, which warms up SharePoint by calling each Site within a SiteCollection. It has bee updated multiple times with new features.

Building a Visual Studio project for SP2010/SP2013 (.NET 3.5/4.0)

In this post I will show you how you can use MSBuild to target your project for .NET 3.5 or .NET 4.0 and use a separate app.config file for each.

My Warmup Tool is supposed to work with SP2010 and SP2013. To achieve that compatibility, I have to change the TargetFramework of the project to be able to compile, as well as the app.config so the application uses the desired Framework. I didn’t want to change the values every time manually. An automated solution has to be possible. And it is. Some little changes to the project file and MSBuild will do all the work for you 🙂

SQL Access to Configuration DB required

In many cases you pass an URL string to connect to SharePoint. In my case I wanted to verify the URL by using this code:

  1: Uri requestUri;
  2: if (!Uri.TryCreate(absoluteUrl, UriKind.Absolute, out requestUri))
  3:   throw new ArgumentException(absoluteUrl + " is no a valid URL.");
  4: 
  5: SPWebApplication webApplication = SPWebApplication.Lookup(requestUri);

And here comes the “but”. I did not know that the account, which is executing the code, needs permissions to the Configuration Database!

When a Feature gets installed

Have you ever thought about the Features folder and when a folder will be created for one of you features? Well, I did 🙂

Why is this relevant, anyway? To be able to activate a feature on a given scope, it has to be installed first. That’s why.

  <td valign="top">
    <strong>Result</strong>
  </td>
</tr>

<tr>
  <td valign="top">
    stsadm -o addsolution
  </td>
  
  <td valign="top">
    The solution is added to the farm. Features are not available
  </td>
</tr>

<tr>
  <td valign="top">
    stsadm -o deploysolution
  </td>
  
  <td valign="top">
    Feature folders are created and the Features are available for activation
  </td>
</tr>

<tr>
  <td valign="top">
    stsadm -o installfeature
  </td>
  
  <td valign="top">
    A feature with ID xyz has already been installed in this farm.  Use the force attribute to explicitly re-install the feature.
  </td>
</tr>
Action

Great. After deploying the solution, the feature is automatically installed and can be used. I did expect this, because installing a feature is a rather uncommon task.

Activating Features after Solution Deployment via VS

Visual Studio allow a F5 Deployment. I guess you all know that. The part where you have to think carefully is, when you add Features to your project.

Should you activate “Activate On Default”? Well, it depends (as always). Usually I don’t enable that setting, because features tend to be activated on scopes you won’t expect.

The problem

Take a WebApplication scoped feature for example. It might create SafeControl entries for your controls. Do you really want them to be added to an Extranet WebApplication if your solution is solely for an Intranet Application?

SPQuery for my tasks

Developing solution with multiple languages (or a language which is not English) sometimes can be a bit painful. To configure a Webpart to display only my tasks, I would filter for [Me] or [Ich].

image

To achieve the same via code / CAML, you can filter by UserID and not the string “Me”.

  1: <Where>
  2:   <Eq>
  3:     <FieldRef Name="AssignedTo" />
  4:     <Value Type="Integer">
  5:       <UserID />
  6:     </Value>
  7:   </Eq>
  8: </Where>
  9: <OrderBy>
 10:   <FieldRef Name="Status" />
 11:   <FieldRef Name="Priority" />
 12: </OrderBy>

This is just a reminder for me, so I can find the information more quickly. But maybe this is useful for some of you as well 🙂

Do long running operations on SPListItem creation

Events on SPListItems like ItemAdding or ItemAdded are nothing new. Many of you have already used them. Recently I had a requirement to create a new SPSite, when an item in a list has been created. So an ItemReceiver was my choice.

But the customer wants something special 🙂 During the creation process, which takes some seconds, the user should see a loading animation.

image

Now Available: Office Developer Tools for Visual Studio 2012

Finally! Now Available: Office Developer Tools for Visual Studio 2012

There are some points to mention, where the final release of the tools differ from previous preview releases:

  • validation experience that helps you to find and fix common errors prior to submitting your apps to the Office Store
  • A continuous integration workflow
  • Windows Azure cloud service projects for creating provider-hosted Apps
  • A dramatically improved Workflow designer

The download link: http://aka.ms/OfficeDevToolsForVS2012

Using .NET 4 with SharePoint 2013

A while ago, I wrote an article about performing operations parallel with SharePoint 2010 (.NET 3.5). –> Execute code in multiple threads (even with SharePoint)

Since I am not the only guy with this kind of “problems”, others are writing about SharePoint and .NET. Especially .NET 4.5 and SharePoint 2013.

Stephane Eyskens hat posted a nice 6-post series about .NET within SharePoint 2013.

If you stumble across more great resources, please leave a comment.

Caching objects with HttpRuntime

I won’t go into the arguments for using a caching mechanism or not. This post is simply an example for an easy way to cache data.

So if you want to store some object in the cache, you can do so very easy.

var localizedString = Caching.EnsureObject(resourceName,
                                           () => GetOperation(parameter));

As you can see, it really doesn’t matter what type of object the cache will store.

class Caching
{
   private static readonly TimeSpan Duration = new TimeSpan(1, , );

   /// <summary>
   /// return cached value, or add and return from cache
   /// </summary>
   /// <typeparam name="T"></typeparam>
   /// <param name="cacheKey"></param>
   /// <param name="getObject"></param>
   /// <returns></returns>
   internal static T EnsureObject<T>(string cacheKey, Func<T> getObject)
   {
      var value = HttpRuntime.Cache[cacheKey];
      if (value != null)
      {
         return (T) value;
      }

      value = getObject.Invoke();

      if (value != null)
      {
         var expiration = DateTime.UtcNow.Add(Duration);
         HttpRuntime.Cache.Insert(cacheKey, value, null, expiration, Cache.NoSlidingExpiration);
      }
      return (T) value;
   }

Adjust the duration, or pass it as parameter. Additionally you could pass another delegate for Exception-Handling. This example is meant as a reminder to think about caching again…

Execute code in multiple threads (even with SharePoint)

Since SharePoint 2010 uses .NET 3.5, you can not use the fancy new functions from .NET 4 🙂

So if we need e.g. multi-threaded execution of code, we’ll need to write the code ourselves. But, as you can see, this really isn’t so hard. The basic idea behind this solution of executing code parallel in threads, is that you have an IEnumerable of some kind. This can be a List, or any other IEnumerable.

Major Update to the Fileserveraccess Web Part

In 2008 I’ve released a Web Part, which enables your users to access files on your fileservers through SharePoint. Original post. This Web Part has been downloaded many times. With this new version, I’ve tried to deal with the most asked questions (like Kerberos), which will make the Web Part easier to use. Naturally new features have been implemented, to get you to upgrade to the new version.

With this release, the Web Part requires SharePoint Foundation / Server 2010. For the users who are still using WSS V3, please stick to the old version, or upgrade your farm 🙂