Add a security trimmed node to Sharepoint top bar navigation

After programatically provisioning a new SPWeb object, it is sometimes necessary to create a new entry on the root web top bar menu.
So far so good, but what happens if the subsite has different permissions from the root site. What if someone with access to the root site sees the menu entry for a site that he’s not entitled to see?

The following piece of code allows the creation of the menu node with swcurity trimming: only the users with permissions on the sub-sites will see the nodes on the menu.

Lets take for example the baseWeb onject as the root SPWeb and the provisionedWeb object as the recent provisioned one.

public void aaa()
        var menuNodes= baseWeb.Navigation.TopNavigationBar;
	// Check for an existing link to the subsite.
	SPNavigationNode newNode = menuNodes
		.FirstOrDefault(n => n.Url.Equals(siteNew.ServerRelativeUrl));
	// No link, so add one.
	if (newNode == null)
		// Create the node.
		newNode = new SPNavigationNode(provisionedWeb .Title, provisionedWeb .ServerRelativeUrl);
		// Add it to the collection.
		newNode = menuNodes.AddAsLast(newNode);

[Sharepoint] Adding a field to a list view with code -> don’t use the DefaultView property

Today we faced a strange problem doing  a task as simple as adding a field to the default view (“All items”) with code.
The code would run inside a feature activation and it was something like this:

 var view = list.DefaultView;

But… even after the update, the viewfields property remained unchanged…. The view reference was pointing to “All Items”, which was correct, all the existing viewfields were retrieved correctly, but we were unable to add one.

The next logical thing to do was to reference view differently. Referencing it by it’s name did the job. So the working code doesn’t change a lot:

 var view = list.Views["All Items"];

But still it made us loose precious minutes of our time to get to the solution…
Hope it’s useful to you!


[Sharepoint] Target a single organizational unit of the Active Directory from a Site Collection

One of these days I had a challenging request: create a “multi-tenant” sharepoint app using a single instance and the same Active Directory.

This application was supposed to allow several companies to use a specific part of it.

For scalabilty reasons it was decided that this segregation could not be done at web application level – what would have made things easier- so the separation will be done at site collection level.

Continue reading “[Sharepoint] Target a single organizational unit of the Active Directory from a Site Collection” »

Referencing JSON.NET in Windows Phone 8

Sometimes we just want to knock our heads against the wall until the wall cracks or we loose our senses…

That’s how I feel now, after loosing more than 30 minutes figuring out how reference JSON.NET in a Windows Phone 8 Project.

So, I took the Silverlight assembly, and added it to the project and this is what I got:  a nice “A reference to a higher version or incompatible assembly cannot be added to the project.” error message..

Continue reading “Referencing JSON.NET in Windows Phone 8” »

Book Review – Aptana Studio Beginner’s Guide

As I’ve previously said here , Aptana 3 is a Great IDE for web development.

As all powerful tools, it can be confusing at the beginning. Luckily Pack Publishing has just released a new book called Aptana Studio Beginner’s Guide which can help you improving your Aptana skills.


From simple tasks, to heavy customization of the IDE, this book will guide you, through an example based approach, to boosting your productivity and accelerating your tasks with Aptana.

Sharepoint group description – add link or other HTML

One of these days I needed to provide rich sharepoint group descriptions, with html, and even a link.

Setting the description directly on the SPGroup object had the HTML being escaped and displayed on the group list page, which was not the wanted result.

So, how to get the rich description, like on OOTB sharepoint groups?
Continue reading “Sharepoint group description – add link or other HTML” »

[Windows 8] [Workaround] – Javascript runtime Error: Object doesn’t support property or method ‘itemFromDescription’

Lately I’ve been playing with the Windows 8 developer tools, being focused on the HTML5 + Javascript version.

Although the paradigm changes a bit (being a pure OOP developer) I must say I’m well impressed with it.

The labs are a great place to start, and will provide a general knowledge of the main features of the windows store apps.

One of these features is the semantic zoom. While I was trying to use it on a experience project of my own, I got the nice exception “Javascript runtime Error: Object doesn’t support property or method ‘itemFromDescription'” when zooming out.

When zooming out, the position of each item will be retrieved. The action happens on the ui.js file:

                if (typeof key === "string" && this._dataSource.itemFromKey) {
                    itemPromise = this._dataSource.itemFromKey(key, (this._isZoomedOut ? {
                        groupMemberKey: item.key,
                        groupMemberIndex: item.index
                    } : null));
                } else {
                    var description = (this._isZoomedOut ? item.groupDescription : item.firstItemDescription);

                    if (WinJS.validation) {
                        if (description === undefined) {
                            throw new WinJS.ErrorFromName("WinJS.UI.ListView.InvalidItem", strings.listViewInvalidItem);

                    itemPromise = this._dataSource.itemFromDescription(description);

If you look closely, you’ll see that if the type of the key used for the group is not a string, the fallback will be the “item.groupDescription” (zoomed out) which obviously can be undefined (depending on the group object structure).

The workaround is to convert the group key to string:

var groupedItems = list.createGrouped(
    function groupKeySelector(item) {
         return item.departmentId.toString();

And that’s it. It doesn’t solve the problem, but it goes around it.

[c#] Extending Enums on .NET

One of these days I had the need to create and extensible enum. Extensible in a way that allows people to add items to it, but outside the assembly where the enum is declared.

The short answer to this is that enums are not extensible Triste

It become clear that I would need to find a workaround. Since we had some legacy code already working, it was necessary to keep the normal usage of the enum (i.e. switch – case).

The following solution may not be the better one, but it’s working good, allowing the extension of the enum and still preserver their traditional usage on the switch-case blocks.

Note: the following code is used to “replace” enums deriving from INT. Deriving from other datatype involves changing the operators.

Continue reading “ Extending Enums on .NET” »

[Sharepoint] Hide QuickLauch on feature activation

Sometimes, when applying branding to Sharepoint sites, it’s necessary to hide the quicklaunch bar.

Some people do that by overriding the css styles, and basically making it invisible. This is a good approach for when, on certain pages, we need to display the quicklaunch, while on the others we need to hide it.

Please note that the quicklaunch it’s always there, with this approach. It’s simply not visible.

For a situation where we’ll never want to display the quicklaunch, we can simply deactivate it, with a feature.

Let’s say we create a feature called “HideQuickLaunch”, scoped for a site (Web). On the event receiver we can override the following methods:

//disable quicklaunch on feature activation
public override void FeatureActivated(SPFeatureReceiverProperties properties)
    using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
        currentWeb.AllowUnsafeUpdates = true;
        currentWeb.QuickLaunchEnabled = false;
        currentWeb.AllowUnsafeUpdates = false;

//enable quicklaunch on feature deactivation
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
        currentWeb.AllowUnsafeUpdates = true;
        currentWeb.QuickLaunchEnabled = false;
        currentWeb.AllowUnsafeUpdates = false;

Hope it helps ;)