Wednesday, July 15, 2009

Programmatically creating mailbox for an existing user in Exchange Server 2010

In this post I am going to demonstrate how we can leverage the Exchange Management Shell Commandlets with managed code to accomplish some basic administrative tasks like creating mailbox in Exchange Server 2010 for existing users.

Before getting started just ensure that you have Windows PowerShell SDK installed on your development machine. Developers working on Windows Server 2008 can skip this check because Windows PowerShell is now included as part of Windows Server 2008.

In your C# project, add reference to System.Management.Automation.dll assembly. Once you install PowerShell, you will find it on the following path C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\ . Somehow I was not able to find this assembly any where in program files folder on my Windows Server 2008 machine. So I manually took the DLL out from GAC and put it in a local folder and referenced it.

Add the following directive statements to your code

using System.Management.Automation.Runspaces;
using System.Management.Automation;
using System.Collections.ObjectModel;
using System.Collections;

Next Add the following methods to your code:

  • EnableMailbox- Creates mailbox for an existing user by using the Enable-Mailbox commandlet.

In this method I am creating and opening a Runspace that has access to the Exchange Management Shell. Next is to create an object of Pipeline class by using this runspace. Before creating a mailbox, it’s good to check whether the users already has a mailbox on exchange or not. So here I am calling a method MailBoxAlreadyExist (explained in next section) which checks for the users' mailbox and returns me true or false accordingly.

Further I am creating an instance of the Command class by using the name of the cmdlet I wish to run (Enable- Mailbox). This cmdlet is then added to the command collection of pipleline object. Invoking the pipeline object would run the command and create a mailbox in exchange.

 Enable Mailbox 
  • MailBoxAlreadyExist- Checks whether the mailbox for a give user identity already exists on Exchange Server. Here I am using the Get-User commandlet to get the required user(PSObject). Next using the lambda expressions, I am checking the value of property ‘RecipientType’. If the user already has a mailbox on Exchange, then this property returns a value UserMailbox else it would return User.
The code can be downloaded from here

Saturday, July 4, 2009

Overwriting Files in SharePoint using Module Element

As documented on msdn, the IgnoreIfAlreadyExists attribute in the file element provisions a file even if it already exists at the specified URL. However it doesn't works as expected.
Searching for solution on the web, I came across a fantastic blog post by John Leino where he has suggested a programmatic approach to workaround this issue.
John describes creating a feature receiver which upon activation would get all the files along with their properties from the module element. Based on these properties, the files would then be added/overwritten on their target locations.
I created a small POC using John’s approach and it really worked well !. But his code seems to break under the following conditions:
  • If you have multiple modules in your element.xml file.
  • If the target SharePoint library forces the checking and checkout policy.
So I extended John’s code to address these issues by adding/updating few methods.

Added a CheckOutStaus method to get the file checked out status:

For publishing sites the content needs to be approved before it can be seen by other site users (e.g. on the masterpage library). So I added a method CheckContentApproval to verify that whether content approval is enabled on the target SharePoint library:

Updated the UpdateFilesInModule method to looks like this:
So in the above method after from getting all the files and their properties from the module, we are doing two thing
  1. Getting the checkout status of the file.
  2. Checking whether content approval is enabled on the target SharePoint library and accordingly approving the file.
The entire source code can be downloaded from .

Saturday, June 20, 2009

How We Did It: So the recipe is out!!

Microsoft launched the SharePoint marketing Website on SharePoint Server 2007 last month. What’s interesting is the website interfaces a simple and an elegant look and feel, with Silverlight adding a jazz to the overall user experience.

From the manageability perceptive, the publishing infrastructure allows the content to be published and managed more quickly.

Our wonderful team Todd, Jomit, Meeta and Ritu and me - worked together to design and developed a whole bunch of components that fulfilled the overall requirements. Microsoft has also published the How We Did It article on MSDN highlighting the goals for rebuilding the website, the overall architecture and components used in the Website.

Saturday, May 2, 2009

Microsoft Office 14 for Web

Microsoft announced their new component in the Office system ‘The Office Web Applications’ at the PDC 2008. These would be available as a part the next version of Office which would be Office 14. The Office 14 for web is a group of light weight, cross platform, cross browser versions of applications like Word, Excel, PowerPoint, One Note etc.

Office Web Applications are all about giving people access to their familiar Office applications even when they are away from their desktop. So this does mean that in the next release what you would need to make a proposal document is just a browser.

Check this cool video where Antoine Leblond from the Office group introduces to some of the features of Office 14 for Web.


Friday, May 1, 2009

Making your SharePoint environment ready for Silverlight development

SharePoint and Silverlight can be integrated together to provide an interactive user experience. The data within SharePoint and various LOBs can be delivered to the end user in a compelling manner using Silverlight.

But making SharePoint Server 2007 environment ready for Silverlight development is a bit challenging and a time consuming process. There are several updates and components needed on the development machine before you can actually begin Silverlight development. For instance, to see a Silverlight application in action on a SharePoint Web page proper configurations need to be made in the web.config of the Web application.

To address the same challenges,
Advaiya has developed a tool Advaiya Development Scanner which analyzes a target machine for the necessary components, and reports what is already installed and what needs to be installed or updated in order to support Silverlight 2.0 development. Along with this it also makes the necessary web config modifications on the targeted Web applications.

The demo of the tool can be seen
here in action.

OOB publishing image field with hyperlink bug: Resolved

Couple of times SharePoint presents an odd behavior. I came across one of such behaviors recently where the publishing image field was getting rendered incorrectly when seen in the display mode. Instead of showing the image the publishing field shows the IMG tags.

Searching around the internet I found other people reporting the same issue followed by a workaround given by Stefan Gossner on his blog.

The workaround he suggested includes creating a custom field control which inherits from the RichImageField class.

After implementing the same I was able to get rid of this issue, but soon i discovered one more bug :-( . It was if you assign a hyperlink (e.g. to an image, then the publishing image field also renders the hyperlink along with the image.

This occurs specifically in case when the hyperlinks include the protocol with them (http or https etc.).

After inspecting this issue I found that it was all due to the malformed anchor tag.

A well formed HTML output should be:

<span dir=""><a href=""><img alt="" border="0" src="/PublishingImages/ add.gif" style="BORDER: 0px solid; "></a></span>

But with the issue it was:

<span dir=""><a href="<a href=" "> </a>" target="_blank"><img alt="" border="0" src="/PublishingImages/add.gif" style="BORDER: 0px solid; "></a></span>

To rectify this issue i took Steffan’s approach as the baseline and modified the code to look like this:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.Publishing.WebControls;

namespace CustomPublishingField
public class RichImageFieldControl: RichImageField
protected override void RenderFieldForDisplay(HtmlTextWriter output)
if (this.ListItemFieldValue != null)
StringBuilder item = new StringBuilder();
item.Append("<span dir=''>");

So what I did here is pretty simple. I am taking the value of the ListItemFieldvalue property of the control and wrapping it under span tags. This generates the correct html and as a result the image gets properly rendered with hyperlinks.