Start content type synchronisation from within a feature

November 7, 2011

The standard way to consume the content types from content type hub in your site collection. It is described for example here.  If you want  the content types available direct during creating of the site collection, this will not work for you. You try to start the Timer Job programmatically, but you will receive access denied exception. Because starting timer jobs is not allowed during feature activation. Here is a sample how you can consume content types immediately only for a specific site collection without permissions problems. However it includes a little bit of reflection.

using System;
using Microsoft.SharePoint;
using System.Reflection;

namespace NameSpace
{
public class ContentTypeSubscriber
{
public void Execute(SPSite site)
{
var taxonomyAssembly = Assembly.Load("Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
var subscriberType = taxonomyAssembly.GetType("Microsoft.SharePoint.Taxonomy.ContentTypeSync.Internal.Subscriber");

var processSiteMethodInfo = subscriberType.GetMethod("ProcessSite",
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new[] { typeof(SPSite) },
null);

var subscriberTypeConstructor = subscriberType.GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { }, null
);

var subscriberInstance = subscriberTypeConstructor.Invoke(new object[] { });
processSiteMethodInfo.Invoke(subscriberInstance, new Object[] { site });
}
}
}

Restoring site or list from a full farm backup

June 12, 2011

This post covers a scenario, when a user makes some irreversible changes and the only place where the undamaged data is, is the farm full backup. The scenarios can be list deletion, site deletion, permissions damage etc.
So here are the instructions:

  1. Locate the folder where your farm full backup is and open the file spbackup.xml
  2. Find the content database name in the file and in the found node  locate a line that looks like this:
    <SPParameter Key="SHAREPOINT2010:WSS_Content.dat"><![CDATA[00000055.bak]]></SPParameter>
    
  3. 00000055.bak is the name of the database backup of the content database.
  4. Go to SQL Management Studio and create a new database.
  5. Perform a restore into this database from the backup file mentioned above. You have to change the name of the ldf and mdf file and check the “overwrite the existing database” checkbox.
  6. Now open the Central administration. Click “Backup and Restore” tab and then “Recover data from an unattached content database” link.
  7. Type in the name of the currently restored database and click Next.
  8. Navigate to a list / site collection that you want to restore and click Next.
  9. On the following page type in where you want to store the exported data of the list / site collection and click Next.
  10. When the backup is created use the Import-SPWeb command to restore the data. The command should look like this:
Import-SPWeb -Path C:\temp\backup.bak -Identity http://localhost

RunWithElevatedPrivileges throws Access Denied Exception

May 8, 2011

There are more reasons why you can receive this exception. The obvious one is that you use SPContext in the elevated block. For more info see this post . I will  describe a less obvious reason in following article. The RunWithElevatedPrivileges method runs the code under the account of the application pool. In the productive environments has the application pool account only limited privileges. Generally the permissions are so defined that you can do anything you want in the current site however if you try to run e.g following code:

var webApp = SPWebApplication.Lookup(new Uri("http://localhost/sites"));
webApp.Sites.Add("http://localhost/sites/", "user", "email");

you will receive the access denied exception. So how to solve this problem:

  1. Define an account that has only the needed permissions to perform the desired operation. In this specific scenario, you can define a user that has self service site creation permissions and modify the code that it uses self service site creation.
  2. Run the code, that throws exception under the specified account. You can achieve this for example so, that you open the SPSite impersonated. See this.

SSD Disks and SharePoint 2010

February 8, 2011

I have recently set up a new virtualized SharePoint development machine with a SSD Disk (Model Kingston 128GB SSDNow V-Series V+ SATA2 2.5).  I measured the time it takes to load a start page of a site collection after iisreset. The virtualization software was virtual box. The tested site collection was Enterprise Wiki. Here is the configuration of the systems:
Host system

  • Windows 7 64bit
  • 8GB RAM

Virtualized Server

  • Windows 2008 Server 64bit R2
  • SharePoint 2010
  • 4GB RAM

Here are the results

System Time (seconds) 
with SSD disk 8
with standard disk 32 

Pretty impressive

DisplayName of SPField is not set

October 28, 2010

Today I came across rather interesting “feature” in SharePoint 2010. I created a new LookupField programmatically and set the Title to a value. This value was however not used. The internal name was used in the display. The more i

So after some digging in .Net Reflector I came up with this. The setter of the Title property is implemented like this:

public void set_Title(string value)
{
	if (!this.Title.Equals(value, StringComparison.Ordinal))
	{
		if (this.TitleResource != null)
		{
			this.TitleResource.Value = value;
		}
		if (CultureInfo.CurrentUICulture.LCID == this.Web.UICulture.LCID)
		{
			this.SetFieldAttributeValue("DisplayName", value);
		}
		else
		if (this.TitleResource != null)
		{
			this.SetFieldAttributeValue("DisplayName", this.TitleResource.GetValueForUICulture(this.Fields.Web.UICulture));
		}
	}
}

So the reason why this does not work is that I had a german site collection where and the server was english. In this case is the DiplayName not set. Cool
For comparison how it is implemented in SharePoint 2007:

public void set_Title(string value)
{
	this.SetFieldAttributeValue("DisplayName", value);
}

As you can see this could not have happened in the old version.
The solution is as follows. Switch the culture of the CurrentThread to the language of the SPWeb you want to work with.
Or do it through reflection

private static void SetFieldDisplayName(SPFieldLookup continuingProjectLookup, string displayName)
{
	Type baseType = continuingProjectLookup.GetType().BaseType;
	object obj = baseType.InvokeMember("SetFieldAttributeValue", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,	null, continuingProjectLookup, new object[] { "DisplayName", displayName });
	continuingProjectLookup.Update();
}

Quick tips for using Powershell in SharePoint

October 12, 2010

Here are some helpful powershell commands that I am afraid that I will forget:

This powershell command will install PowerShell ISE. A Pretty tool for editing powershell scripts:

Import-Module ServerManager;Add-WindowsFeature PowerShell-ISE

This powershell command will enable SharePoint commands:

Add-PsSnapin Microsoft.SharePoint.PowerShell

This command opens a new SPSite and root SPWeb

$spsite = new-object Microsoft.SharePoint.SPSite(“http://localhost&#8221;)

$spweb = $spsite.OpenWeb()

Creating Loops in SharePoint Designer 2010 Workflows

October 7, 2010

By default SPD 2010 does not allow possible infinite loops to be added to a workflow. Here is the guide how you can do it:

  1. Open SPD 2010
  2. Add a new Workflow
  3. Add new IfElseActivity and fill in the conditions and the actions you want to loop through
  4. Save the workflow
  5. Go to Alle Files -> Workflows -> Your workflow name  folder and open  “your workflow name.xoml” file as xml.
  6. Locate the lines that look like this:

<IfElseActivity x:Name="ID411">

<IfElseBranchActivity x:Name="ID412">

<IfElseBranchActivity.Condition>

<RuleConditionReference ConditionName="__Rule_ID412" />

</IfElseBranchActivity.Condition>

And replace if with something like this:


<WhileActivity x:Name="ID411">

<WhileActivity.Condition>

<RuleConditionReference ConditionName="__Rule_ID412" />

</WhileActivity.Condition>

&nbsp;

Keep in mind that you have to remove also the ending tag of the IfElseBranchActivity.  Keep also in mind that if you have multiple activities in the IfElseActivity taht you have to encapsulate these activities into one single SequenceActivity.

Now you can Publish your workflow back to SharePoint.

One drawback of this approach is that SPD does not support designing of a whileactivity in the worklow designer so you have to change the activities direct in the xoml file or you can replace the whileactivity again with ifelseactivity, do your changes and replace it again.

You have to also modify the web.config of the web app where you want to deploy your workflow because by default is the WhileActivity disabled. Locate following line


<authorizedType Assembly="System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="WhileActivity" Authorized="False" />

and set the “Authorized” attribute to True.

Adding a Webpart to home.aspx during Site creating in Enterprise Wiki

September 8, 2010

Consider following scenario:

Create a custom site template based on Enterprise Wiki (Publishing Site) and add a web part to the home page during site creating.

The creating of the template is not tricky and you can find the description here.  The tricky part is actually adding the web part to the homepage.

So here is how you can do it:

Delete the module that creates the home.aspx from the onet.xml. and create it manually in code behind (I used a feature receiver on a feature that is activated when the site is created. The method that adds the homepage can look like this.

public static void AddHomePageToDocumentLibrary(SPWeb web, string pageContent)
{
        var pubWeb = PublishingWeb.GetPublishingWeb(web);
        var pageLayout = pubWeb.GetAvailablePageLayouts()[0];
        PublishingPage newPage = pubWeb.GetPublishingPages().Add("home.aspx", pageLayout);
        newPage.ListItem[FieldId.PublishingPageContent] = pageContent;
        newPage.ListItem.Update();
        newPage.Update();
}

The page content should contain following div:

<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'><div class='ms-rtestate-read d127f8fd-2a2e-4495-9e0f-d32d5ca2e5c7' id='div_d127f8fd-2a2e-4495-9e0f-d32d5ca2e5c7'></div><div style='display:none' id='vid_d127f8fd-2a2e-4495-9e0f-d32d5ca2e5c7'></div></div>

This div is by run time replaced with an actual webpart. The guid d127f8fd-2a2e-4495-9e0f-d32d5ca2e5c7 is important as it identifies the webpart that should be added to the page.

Finally we have to add the webpart to the webpart zone.


public static void AddWebPartToHomePage(System.Web.UI.WebControls.WebParts.WebPart webPart, SPWeb web, Guid webpartId)
        {
            using (SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager("seiten/home.aspx", PersonalizationScope.Shared))
            {
                string storageKeyId = StorageKeyToID(webpartId);
                webPart.ID = storageKeyId;
                mgr.AddWebPart(webPart, "wpz", 0);
            }
        }

private static string StorageKeyToID(Guid storageKey)
        {
            if (!(Guid.Empty == storageKey))
            {
                return ("g_" + storageKey.ToString().Replace('-', '_'));
            }
            return string.Empty;
        }

The webpart is the webpart we want to add, web is the web that we create and the guid is the guid that identifies the webpart. In our case it is d127f8fd-2a2e-4495-9e0f-d32d5ca2e5c7. The “wpz” zone is the hidden webpart zone that SharePoint 2010 uses to store inline webparts.

I tried to create these webparts in onet.xml, but it did not work. The webparts were simply not displayed, though all attributes were filled.

References

http://donalconlon.wordpress.com/2010/05/04/sp2010-creating-a-wiki-page-using-the-om/

Pex and Moles Testing Framework for SharePoint

September 1, 2010

Here is a quickstart for working with unit testing framework for SharePoint. This Framework is called Moles. You can download it here. It is free academic use and Microsoft partners.

So how this Framework works: It allows you to replace any method with by you defined delegate. So here is a simplistic function:

Simple Intro

public static bool IsInFuture(DateTime date)
{
	return date > DateTime.Now;
}

So this function is not easily unit testable, because the DateTime.Now is not a constant value. What we can do with moles is following:

[TestMethod()]
[HostType("Moles")]
public static void IsInFutureTest()
{
      MDateTime.NowGet = () => new DateTime(2010,09,01);
      bool actual = SuperClass.IsInFuture(new DateTime(2010, 09,03));
      Assert.IsTrue(actual);
}

The interesting thing here is the MDateTime construct, that comes from moles and the function replaces the actual method call with by us defined function that returns a constant date.
So here is what we have to do when we want to use the MDateTime construct:

  1. Install Pex and Moles.
  2. Add a new item of type “Moles and Stubs for Testing”  into the unit test project.  This file has to be called mscorlib.moles, because  the DateTime class is located in the mscorlib.dll. When you add this file the moles Framework analyses the dll and create the stubs. These stubs are the classes with the “M” prefix.
  3. Add following attribute [HostType(“Moles”)] to the test method. This tells moles framework to replace the actual methods with custom delegates.
  4. For this type you have to also add following row to the assemblyinfo.cs of the test project [assembly: Microsoft.Moles.Framework.MoledType(typeof(DateTime))]

SharePoint Unit Testing

You can stub in this way all SharePoint objects e.g. SPContext, SPList etc. All you have to do is to add new moles file with the name Microsoft.SharePoint.moles. This has actually one drawback and it is that it is a lot of coding to fullfill all the expectations that you use in the method to be tested. There come behaviors for SharePoint in play. It offers stubs with predefined behaviors against you can unit test your code. In all these cases you  don’t access the physically SharePoint, in extreme there can be no SharePoint installed at all.

So where can you find Behaviors for SharePoint. They are part of the Moles project and you can find it on the machine where moles are installed on the following path \Program Files (x86)\Microsoft Moles\Documentation\behaviors.samples.zip\behaviors\Microsoft.SharePoint.Behaviors. All You have to do is to compile this project and you have an assembly called Microsoft.SharePoint.Behaviors.dll that you can reference in your test projects. Here is a short sample how you can work with SharePoint behaviors:

[TestMethod()]
[HostType("Moles")]
public static void  IsListNameCorrectTest()
{
BSPSite site = new BSPSite();
BSPWeb web = site.SetRootWeb();
BSPList list = web.Lists.SetOne();
list.Title = "TestList";
bool actual = MyClass.IsListNameCorrect(list);
Assert.IsTrue(actual);
}

public static class MyClass
{
public bool IsListNameCorrect(SPList list)
{
return list.Title.Length < 200;
}
}

Clean Code: A Handbook of Agile Software Craftsmanship

August 17, 2010

A really interesting book about doing software right. You can buy it here or you can find it in pdf form here.