Tag Archives: sharepoint

[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” »

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” »

[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 ;)

[Sharepoint] How to get the MySiteHost Url with code

Sometimes we need to reference the MySiteHost, to create a link or for any other reason.

To avoid hard-coding it or saving it on a resource file (which would need redeployment to change), we can use the UserProfileManager and retrieve it dynamically:

 public static string GetMySiteHostUrl()
     SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Web.Site);
     UserProfileManager upm = new UserProfileManager(context);
     return upm.MySiteHostUrl;

Note: It’s necessary to reference the Microsoft.Office.Server.UserProfiles Asssembly.


[Sharepoint] – DataContext with custom URL


One of these days I had to do some bugfixing on a Sharepoint project which used the Microsoft.SharePoint.Linq.DataContext class.

While trying to access a SPList belonging to Site Collection another Site Collection I always had an “Invalid Url Exception” right from the contructor, and other times I had exceptions when accessing the objects.

var context  = new DataContext(otherSiteColUrl);

The problem is that inside, the constructor, the URL is only taken into account if we have no HttpContext. If we do have one, the context URL (which means, the current site url) the custom url passed on the constructor is ignored.
Continue reading “[Sharepoint] – DataContext with custom URL” »

[TIP – Sharepoint] – CAML Query – Filtering Boolean fields

A few days ago I needed to build a CAML query that return all the items which have changed since the last execution. I have a Yes/No (boolean) field called “HasChanged” that when the item is changed turns to “True”.

The query would be something like this:

         <fieldref Name="HasChanged"></fieldref>
         <value Type="Boolean">true</value>

The truth is that this query will not work as you might think. For Sharepoint, Yes/No fields are represented by 1 and 0. As so, the query has to be like:
Continue reading “[TIP – Sharepoint] – CAML Query – Filtering Boolean fields” »