Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Archive for the ‘Uncategorized’ Category

Opening Web Parts Gallery Programmatically

leave a comment »

Navigating to the Web Part Gallery in the browser interface takes you to http://site collection URL/_layouts/_catalogs/wp/Forms/AllItems.aspx. Attempting to open the list through “_catalogs/wp” will fail:

SPList spWPG = spWeb.Lists["_catalogs/wp"];  // does not work

 Instead, you need to use the following code:

SPList spWPG = spWeb.Lists["Web Part Gallery"];


Written by Nick Grattan

June 23, 2011 at 12:21 pm

Re-ordering All-day Items in a Calendar

with 8 comments

By default, all-day items will be displayed in the order determined by their physical location in the database table:


A more natural order would be alphabetic – to change the sort order you’ll need to create a list template, modify the manifest file to add the sort order and then load a new list template. Here goes….

  • First navigate to a standard Calendar list and select the Settings + List Settings menu command.
  • Select the Save List as Template command.
  • Enter a name, such as “SortedCalendar” for the filename and template name and click OK.
  • Click the list template gallery link on the “Operation Completed Successfully” form.
  • You will be taken to the “List Template Gallery”. Click the “Sorted Calendar” link for the gallery you just created. Save this down to the file sytem.
  • Use Windows Explorer to rename the file from “SortedCalendar.stp” to “” – it really is a CAB file!
  • In Windows Explorer double click the CAB file – you will find a single file called mainifest.xml. Copy this into the file system.
  • Use an XML editor (such as Visual Studio) to open the XML file.
  • Search for the following line (look for calendar.aspx):

<View Name=”{895B6132-0327-4FAB-A34B-EFB57FD5E542}” DefaultView=”TRUE” Type=”CALENDAR” RecurrenceRowset=”TRUE” DisplayName=”Calendar” Url=”Lists/Calendar2/calendar.aspx” Level=”1″ BaseViewID=”2″ ContentTypeID=”0x” ImageUrl=”/_layouts/images/events.png”>

  • You now need to look for the query associated with this view. It will be about ten to fifteen lines below the line shown above and looks like:

                  <FieldRef Name=”EventDate”/>
                  <FieldRef Name=”EndDate”/>
                  <FieldRef Name=”RecurrenceID”/>
                  <Value Type=”DateTime”>

  •  Edit the CAML query to include a “OrderBy” clause:

                  <FieldRef Name=”EventDate”/>
                  <FieldRef Name=”EndDate”/>
                  <FieldRef Name=”RecurrenceID”/>
                  <Value Type=”DateTime”>
                <FieldRef Name=”Title”/>

This will cause the results to be sorted by the “Title” field.

  • Save the file manifest.xml.

The manifest.xml file now needs to be added back into the CAB file using the MAKECAB application. To make this tool more accessible run a Visual Studio Command Prompt:

  • Select the Start + All Program + Visual Studio 2005 + Visual Studio Tools + Visual Studio 2005 Command Prompt.
  • At the command prompt navigate to the folder where the manifest and CAB files are located and enter the following:

makecab manifest.xml

  • Rename “” to “SortedCalendar.stp”.
  • Return to the “List Template Gallery” in SharePoint.
  • Click the Upload button and upload the “SortedCalendar.stp” file.

You can now test the new list template by creating a new list– you will find the new listtemplate listed in the Tracking section on the “Create” form. All day events will now be alphabetically sorted by “Title”. Phew!

Written by Nick Grattan

January 23, 2008 at 9:13 pm

Following Styles in SharePoint Customization

leave a comment »

When customizing or writing add-ins for SharePoint it’s important to follow the styles such as ms-input or ms-descriptiontext on your components so that your pages will use modified CSS files, master pages etc.

You can download and install the Microsoft Developer Toolbar for IE 6 or 7 and click on the IE Developer Toolbar button. This displays the Developer Toolbar pane. Click the “mouse pointer” icon on the left side of the bar, select an item on the page and information about the HTML element will be displayed:



Written by Nick Grattan

November 4, 2007 at 6:26 pm

Posted in Uncategorized

Encyrpting SQL Server Connections

leave a comment »

In SQL Server 2005 connections between client and server can be encrypted using SSL even if a X.509 certificate has not been installed on the server. When a certificate is not present SQL Server will automatically generate one.

By default, the certificate chain up to a root CA will be checked, and if a recognised root CA is not found, the connection will fail with the following exception message:

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 31 – Encryption(ssl/tls) handshake failed)

You can request that the certificate check is not made by using the TrustServerCertificate connection string parameter. So, the full connection string to specify SSL encryption without checking the certificate is:

string connectionstring =
            “Server=(local);Database=AdventureWorks; Integrated Security=SSPI;Encrypt=true;TrustServerCertificate=true”;

It’s best to install a recognised certificate, but this approach is better than sending data in plain text across insecure networks.

Written by Nick Grattan

August 8, 2007 at 8:41 pm

Posted in Uncategorized

Bringing Impersonation to SQL Server 2005

with 6 comments

Frequently I find that I would like to execute TSQL commands under the context of another login, for example, when testing a database user’s permissions on objects. The SQL Server 2005 EXECUTE AS statement allows us to do this. To show how this works, first create a login and a user for the AdventureWorks database and test the current login and user name:


CREATE LOGIN OrdinaryLogin WITH PASSWORD = ‘Password’


CREATE USER OrdinaryUser FOR LOGIN OrdinaryLogin




This reports ‘W2003\Administrator’ and ‘dbo’ for me. Now, impersonate the login that’s just been created and test the current login and user name again:


EXECUTE AS LOGIN = ‘OrdinaryLogin’;



This now reports ‘OrdinaryLogin’ and ‘OrdinaryUser’. TSQL commands can now be executed in the security context of ‘OrdinaryUser’. Finally, you can revert back to the previous login by executing this statement:




When using EXECUTE AS the current user must have IMPERSONATE permissions on the target login; ‘dbo’ already has this permission for all database users but the permission can be granted to other users:




This now allows ‘OrdinaryUser’ to impersonate ‘AnotherUser’. SQL Server 2005 also provides the SETUSER to perform a similar task, but this can only be executed by sysadmin/db_owner roles members.

Written by Nick Grattan

July 24, 2007 at 7:30 am

Posted in Uncategorized

Embedding InfoPath Forms Service Forms in SharePoint

with 5 comments

This post has been replaced with this one! This provides a link to a comprehensive document describing this technique.

Question: When using InfoPath Forms with the InfoPath Forms Service the form displayed in the browser replaces the SharePoint Form from which it was launched. Is there a way of embedding the form in an SharePoint page so that users  will not close the browser once they’ve finished with the form and exit from the SharePoint application?

Answer: Yes! Here’s a brief description of the steps to do this. The “XmlFormView” web part can be used to host an InfoPath Server form on a Web Part page within a SharePoint application. This web part is located  in the assembly Microsoft.Office.InfoPath.Server.dll and the web part will need  to be hosted in your site by adding an entry in web.config’s “SafeControls” element.

Once this has been done you can publish an InfoPath template to a SharePoint library and make it available as the template for a form library. Then, create a Web Part page in your site and add an instance of the “XmlFormView” library. Configure this web part to reference the InfoPath template through the “XmlLocation” property.

Finally, you will need to add a “Submit” button on the form as you will need to save the form’s data once the user has completed the form.

Written by Nick Grattan

July 19, 2007 at 8:27 am

Posted in Uncategorized

Sandboxing an Assembly

leave a comment »

If your application is loading an assembly and then executing code within that assembly you may want to “sandbox” that assembly and run the assembly in the “Internet Zone”. This will control access to local resources, such as the file system and the registry. To do this, the assembly must be loaded into an AppDomain, and this has the additional advantage that the assembly can be unloaded once execution is completed.

The following ‘using’ statements will be required for these code examples:

using System.Security;

using System.Security.Policy;

using System.Security.Permissions;

First, you need to create an evidence object which specifies the “Internet Zone”:

object[] hostEvidence = { new Zone(SecurityZone.Internet) };

Evidence intEvidence = new Evidence(hostEvidence, null);

The AppDomain can now be created and the assembly loaded into the AppDomain:

AppDomain ad = AppDomain.CreateDomain(“AddIns”, null);


TestLib.TestLib remoteWorker = (TestLib.TestLib)




               false, // don’t ignore case

               0, // binding attributes

               null, // use default binder

               null, // args passed to constructor (none)

               null, // use culture from current thread  

               null, // no activation attributes

               intEvidence); //evidence

In this case the assembly is loaded from a file  called”TestLib.DLL”. By calling CreateInstanceFromAndUnwrap an instance of the class “TestLib” is created and through this instance methods in the class can be called:

double l;

l = remoteWorker.theMethod();

This assumes that the class TestLib has a method called ‘theMethod’ that takes no arguments and returns a double.

You might be tempted to apply the evidence to the AppDomain – the second parameter in CreateDomain is null in the code above but a reference to the evidence can be passed. However, when the AppDomain tries to load the assembly it will fail as the AppDomain has no access to the file system and so cannot load the assembly!

Written by Nick Grattan

July 13, 2007 at 1:53 pm

Posted in Uncategorized