SharePoint Internals – Hristo Pavlov’s Blog

27 February, 2009

Running execadmsvcjobs through code

Filed under: SharePoint — Tags: , , , , , , , — hristopavlov @ 4:50 am

Well there are many ways you could achieve this and this is just one of  them. It simply creates a new instance of the SPExecuteAdministrationServiceJobs class and calls its Run method. This will start and wait for all currently registered and/or running timer jobs to finish. The code is below:

string stsadmPath = SPUtility.GetGenericSetupPath(“BIN”);

Assembly stsadmAsm = Assembly.LoadFile(Path.GetFullPath(stsadmPath + “\\STSADM.EXE”));

Type admSvcJobType = stsadmAsm.GetType(“Microsoft.SharePoint.StsAdmin.SPExecuteAdministrationServiceJobs”);

ConstructorInfo ci = admSvcJobType.GetConstructor(new Type[] { typeof(SPGlobalAdmin) });

using (SPGlobalAdmin admin = new SPGlobalAdmin())


    object svcjobs = ci.Invoke(new object[] { admin });

    MethodInfo runMi = admSvcJobType.GetMethod(“Run”, BindingFlags.Instance | BindingFlags.Public);


    runMi.Invoke(svcjobs, new object[] { new StringDictionary() });


One place this could be handy is when you provision a web application on multiple front ends using SPWebApplication.ProvisionGlobaly(). This method creates a timer job and returns immediately. However if you need the web application to be created before you continue you need to wait for the timer job(s) to finish. One way to do this programmatically is using the code above.


7 August, 2008

Changing the default list content type programatically

This is something I wanted to do today and couldn’t find any obvious way to accomplish it. Checking the code behind of the ChangeContentTypeOrder.aspx page revealed how it can be done. You need to build a SPContentType[] array or List<SPContentType> list that contains the content types in the order you like. The first content type in the list will be used as a default content type for the SPList. Once you have the array/list you need to assign it to the SPFolder.UniqueContentTypeOrder member of the RootFolder of the list and then call SPFolder.Update() to apply your new order.

You cannot use any content types that inherit from one of these two content types:

SPBuiltInContentTypeId.Folder – 0x0120


The dummy example below reverses the order of the content types of a list and also uses another cool thing I learned from checking the code behind of the ChangeContentTypeOrder.aspx page – the cool .NET sorting method Array.Sort<TKey, TValue> that sorts two arrays simultaneously based on the first array. Of course I could have also used Array.Reverse() method to do the same.

SPContentType[] newContentTypeOrderArray = new SPContentType[list.RootFolder.ContentTypeOrder.Count];

int[] newContentTypeOrderIndexes = new int[list.RootFolder.ContentTypeOrder.Count];


for (int i = 0; i < list.RootFolder.ContentTypeOrder.Count; i++)


    newContentTypeOrderArray[i] = list.RootFolder.ContentTypeOrder[i];

    newContentTypeOrderIndexes[i] = -i;



Array.Sort<int, SPContentType>(newContentTypeOrderIndexes, newContentTypeOrderArray);


list.RootFolder.UniqueContentTypeOrder = newContentTypeOrderArray;


As mentioned above you should be also aware that the “Folder” and “Untyped Document” content types are not shown on the “Change New Button Order and Default Content Type” page however they may become a default content type of the list if you delete all other content types or make one of them default content type programmatically. In this situation if the default content type is “Folder” for example and you upload a file to the document library the file’s content type will be “Folder” – probably not something you really wanted to happen 🙂

Finally if you delete the “Folder” content type from a list you may experience some other strange things. See my EditForm Not Shown When Uploading a File post for details.

17 July, 2008

Managing ‘Managed Paths’ through code

Filed under: SharePoint — Tags: , — hristopavlov @ 3:24 am

Today I needed to check through code whether a managed path is defined and couldn’t find any sample code or information on the net so I decided to investigate this myself.

I starting from the place where I know managed paths are shown and this is the “Defined Managed Paths” page in the Central Administration. The page is actually _admin/scprefix.aspx. I located this page in the 12 hive under 12\TEMPLATE\ADMIN and opened it up in Notepad to see which was the code behind class. It was Microsoft.SharePoint.ApplicationPages.SscPrefixPage:

<%@ Assembly Name=”Microsoft.SharePoint.ApplicationPages.Administration, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c”%>

<%@ Page Language=”C#” Inherits=”Microsoft.SharePoint.ApplicationPages.SscPrefixPage” MasterPageFile=”~/_admin/admin.master”      %>

I then located and opened the Microsoft.SharePoint.ApplicationPages.Administration assembly in Reflector and found the code behind class. After examining it for a while I found that there is a public class available that represents the collection of all managed paths inside a web application and this class is Microsoft.SharePoint.Administration.SPPrefixCollection. Then I noticed that this class has a constructor that accepts an SPWebApplication and initializes the managed paths for this web application. Unfortunately the constructor was with internal visibility. I almost started writing reflection code to call it but first decided to check the “Analyze” option in Reflector and see if any other classes are calling this constructor. Then I discovered that (logically) the SPWebApplication calls it and even has a member that represents the managed paths. This member is called Prefixes. Looking at the methods of the SPPrefixCollection class I found that checking for existing manages paths, adding new ones or deleting existing ones is supported. This is exactly what I was looking for.

// Adds the specified prefix to the collection.

public SPPrefix Add(string strPrefix, SPPrefixType type);


// Returns a Boolean value that indicates whether the collection contains the specified prefix.

public bool Contains(string strPrefix);


// Deletes the specified prefix from the collection.

public void Delete(string strPrefix);


// Deletes the specified array of prefixes from the collection.

public void Delete(string[] strPrefixes);

Now I can go and finish my code and start packing up for my 2 weeks holiday. Oh Yeah!

Blog at