Hidden fields, and controlling them with the Object Model

SharePoint’s fields can be ‘hidden’ or shown, by setting the SPField.Hidden property. That’s great, but sadly it isn’t that simple. You might want a field hidden, but allow administrators, etc., to ‘unhide’ the field. Then again, sometimes you might want your hidden field to be really hidden, and never ‘unhidden’.

That is actually what SharePoint allows. The SPField.Hidden property also relies on a second property ‘CanToggleHidden’. You can see this in the CAML definition of a field. So you could define a field like:

<Field Type="Text" ID="{449cf8bc-88ce-445a-ac55-11ea0cb71fed}" Hidden="TRUE" CanToggleHidden="TRUE" ... >

Okay, that’s fine – that’d give us a field which is hidden, but we can unhide. Note that by default CanToggleHidden is false, and this led to my problem. Continue reading “Hidden fields, and controlling them with the Object Model”

Hidden fields, and controlling them with the Object Model

Determine if a user is a farm administrator

Sometimes you just need to know if a user is a farm admin; conveniently SharePoint provides a couple of static methods on the SPFarm object to check this:

if( SPFarm.CurrentUserIsAdministrator() ) { ... }

Neat, but one tip – it’s not obvious but it seems that if you want to check this within a content web application, you have to use the method that accepts a boolean and that bool needs to be true:

if( SPFarm.CurrentUserIsAdministrator(true) ) { ... }

Otherwise you will only ever get a ‘false’ response.

Determine if a user is a farm administrator

Tokenisation and the Ribbon

A useful post on the MS SharePoint developer blog – “Tokenization in the SharePoint 2010 Server Ribbon“. I always find it a little tricky remembering what token works in which place; this explains it.

In general, and as a reference for myself:

Location ListUrlDir ItemId ItemUrl RecurrenceId SiteUrl ListId Source SelectedListId SelectedItemId
List View Yes No No No Yes Yes Yes Yes Yes
List Form Yes Yes Yes Yes Yes Yes Yes No No
Tokenisation and the Ribbon

Annoyance: GUIDs for Views must be upper case

So, I have a list where I’m using one of the views for my own nefarious purposes; I don’t want users to be able to see it normally, so I set it to hidden in my List Schema. That was fine, and worked.

However, I do need administrators to be able to edit that view – so I gave them a link that would take the to the ViewEdit.aspx page. This worked, except that whenever you tried to save any changes to the view, all you got was an error:

This seemed spurious – “View does not exist” – we’d just looked at the view, so how can it not exist? I tried unhiding the view, and testing, and I found that if I went by my link then I couldn’t save changes, but if I went by the standard UI, everything worked.

Great.

In the end I started comparing differences between the pages you got by my link and the UI. What I found was that some of the GUIDs in the page were, like my link, in lower case.

And that was the problem – the GUIDs in the URL needed to be upper case.

I surmise that someone is comparing strings during the ViewEdit save, rather than the GUIDs that they actually are. #FAIL.

Annoyance: GUIDs for Views must be upper case

Bloody Stupid SPWeb properties

So, like many SharePoint objects, the SPWeb object has a property bag. Unlike many, it has two – Properties and AllProperties.

The problem I encountered with this was that I was trying to set some Search Center settings on a newly created Site Collection, and their capitalization was being persisted wrongly:

newRootWeb.AllProperties["SRCH_ENH_FTR_URL"] = settings.SearchCenterUrl;

This would be persisted as lower case – which was wrong. Lower case capitalisation isn’t the same upper. Nice. Continue reading “Bloody Stupid SPWeb properties”

Bloody Stupid SPWeb properties

List Type Icons for Custom List Definitions

In SharePoint, different types of list can have different icons:

That’s fine, but SharePoint 2010 adds a second icon – one that’s used in the Silverlight control for creating new Sites/Lists:

I wanted to specify that for a list definition I was upgrading from a 2007 version. But there doesn’t seem to be a property to set the name/url for this file.

In SharePoint 2007 the icons were usually .GIF files. In SharePoint 2010, those still seem to be in the IMAGES folder in 14-Hive, but there are .PNGs of the same images too. There are also the larger PNGs used by the SilverLight control. The small sized ones all seem to be called ‘ITxxx.PNG’, and the larger ‘LTxxx.PNG’:

So, I tried changing the Image property of my list definition to use a file with a .png extension (e.g. ITandy.png )

I converted my GIF to PNG format, and I created a 64×64 pixel PNG for the SilverLight control. I called this new PNG file LTandy.png – and low, the SilverLight control picked it up. It seems that the Silverlight dialog replaces the IT at the start of the image file name with an LT when looking for an icon – but only for lists where the smaller icon also has the .png extention.

Therefore, I’d recommend always using PNGs for icons in SharePoint now.

List Type Icons for Custom List Definitions

Icons for SharePoint 2010's ribbon

Came across something interesting today – I went looking for an icon in SharePoint 2010’s ribbon, and came to a file called formatmap32x32.png. Interestingly, it contains:

So, it actually holds lots of icons, but the image is cropped using CSS at display time to only show one of those icons. Nice to be able to see a bunch of the icons in one place easily.

(Though not all buttons come from this file – other icons are ‘stand alone’)

Icons for SharePoint 2010's ribbon

New Ribbon Tab Groups and their Templates

I had a curious problem today. I was registering Custom Actions to create a TabGroup with a single Button control on SharePoint’s ribbon:

I did this following Chris O’Brien’s post about “Adding ribbon items to existing tabs/groups“. It worked well, except that as we’re not registering the custom action against a particular list type (e.g. “101”), this button will appear on any list using the tab specified in the CommandUIDefinition‘s Location. Continue reading “New Ribbon Tab Groups and their Templates”

New Ribbon Tab Groups and their Templates

Getting levels of the SharePoint Heirarchy and their Exceptions

Something that I have to do time and again is get some element of SharePoint’s heirarchy, such as a site collection, site, list or item. This is pretty typical – that’s why we all use USING to ensure proper disposal of SPSites and SPWebs, right? But what happens if the thing you’re after isn’t there? What exception get’s thrown? Well, this should be pretty clear:

try
{
    //FileNotFoundException if doesn't exist
    using (SPSite site = new SPSite(siteGuid))
    {
        //FileNotFoundException if doesn't exist
        using (SPWeb web = site.OpenWeb(webGuid))
        {
            //SPException if doesn't exist
            SPList list = web.Lists[listGuid];

            //ArgumentException if doesn't exist
            SPListItem item = list.GetItemByUniqueId(itemGuid);
        }
    }
}
catch (System.IO.FileNotFoundException fileEx2)
{
    // Site or Site Collection Not Found
}
catch (SPException spEx2)
{
    // List not found
}
catch (ArgumentException argEx2)
{
    // Item not found
}

Hopefully that might prove useful to someone – and a good reminder for me.

Getting levels of the SharePoint Heirarchy and their Exceptions