If you’re doing any sort of custom development within the MOSS framework, there might come a time when you’ll need to execute your code with elevated permissions. Meaning, instead of your code running under the context of the current user, you’ll want to run it using an account with a higher level of access.
Within MOSS, there is a method that will do the trick. The RunWithElevatedPrivileges method will run under the context of the Application Pool Identity account.
As an example: The code below was taken from an application page I developed recently. When running this code using my credentials, it executed without trouble. Once logged in as a user with Contribute access, I was running into an Access Denied SharePoint error.
1
2
3
4
5
| void DoSubmit(object sender, EventArgs e)
{
SPSite site = SPControl.GetContextSite(Context);
SPWebCollection webs = site.AllWebs;
} |
My next step was to attempt to run the DoSubmit method using elevated privileges as follows.
1
2
3
4
5
6
7
8
9
10
| void DoSubmit(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(GetExpiredPages);
}
void GetExpiredPages()
{
SPSite site = SPControl.GetContextSite(Context);
SPWebCollection webs = site.AllWebs;
} |
The above code was still producing an Access Denied error. It took me a moment to realize what I was doing wrong. The trick was that I needed to create a NEW SPSite object INSIDE the function that I am trying to run using elevated privileges. If I don’t recreate the SPSite object, I’ll essentially still be executing my code under the context of the current user.
1
2
3
4
5
| void GetExpiredPages()
{
SPSite site = new SPSite(SPControl.GetContextSite(Context).ID);
SPWebCollection webs = site.AllWebs;
} |
Recently, I ran across a post from Andrew Connell where he addresses the question of moving content created in a dev environment to another environment, such as QA, staging or production. While the focus of his post was to answer that question, he did bring up an interesting point. Is it possible to use a tool like subversion to source control your SharePoint master pages, page layouts, css files etc?
It most certainly is! Andrew’s blog post will get you started on understanding how to use features and solutions to move files from one environment to another, but for those of you out there need some assistance getting subversion source control up and running, please read on.
http://subversion.tigris.org/ is the home page for the subversion source control project. The first thing you’ll need to do is download subversion. Click on Downloads on the left navigation. If it’s not entirely clear, you have two options here: you can compile the source code release directly, or you can install one of the prepackaged binaries if there is one for your operating system. I am running a Windows Server, so there were a few prepackaged binaries that I could choose from. I didn’t want to have to compile source code. I tested out CollabNet, VisualSVN and TortoiseSVN. Below is a brief summary:
- CollabNet is an installer package you can download which will install subversion and optionally apache to your server. There is no GUI, so you’ll have to be familiar with running svn commands via the command prompt to setup your respositories. CollabNet also comes with a client for use with subversion.
- VisualSVN comes with both a server and a client, however, the difference between VisualSVN and CollabNet is that VisualSVN offers a GUI for creating repositories, managing users and managing groups. It’s really simple to use, and I’ve had no issues with it so far. The client portion installs as a plug-in for Visual Studio making it really simple to interact with subversion. It’s important to note that the server product is FREE, however, the client licenses are $49 USD per user.
- TortoiseSVN as far as I can tell is a free client plug-in for Windows Explorer that allows you to interact with subversion. Again, please note here that TortoiseSVN client does not interact with Visual Studio. You can use TortoiseSVN in conjunction with VisualSVN or CollabNet subversion servers. The solution I eventually settled on and now have implemented is VisualSVN server on my Windows Server box and TortoiseSVN for my clients.
- Download VisualSVN
- Download TortoiseSVN
- The install of VisualSVN will create a new entry in your Program Files menu called VisualSVN Server Manager. This GUI will give you the ability to create repositories, add users and configure groups. Right click on Repositories and click Create Repository. Give it a name and allow it to create the default structure. If you expand your repository, you will see 3 folders: trunk, tags and branches.
- You have a couple of options from here. You can add an existing project that’s already been developed or is a work in progress into subversion or you can create a new project that is source controlled before any development begins. I’ll outline the latter briefly, you can expect to see a post about source controlling existing projects later this week.
- Once you’ve created your repository, expand that repository, click on the Trunk folder and select Copy URL.
- Using TortoiseSVN, you can right click within any folder in Windows Explorer and select SVN Checkout from the context menu.
- Paste the url you copied in step 2 in the URL of repository: field. Select the checkout directory and click Ok.
If you’d like to create a custom theme to brand your SharePoint site, it’s relatively straight forward. Howevere, before you begin, I’d definitely recommend reading Heather Solomon’s blog post on Master Pages vs Themes. It’s a great discussion on the advantages and disadvantages of both methods. Comments are most welcome!
- To begin, make a copy of the JET theme folder located in
1
| C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\THEMES |
and paste it into the same folder. Name it CUSTOM, all uppercase.
- Within the new CUSTOM folder you just created, located the JET.INF file and rename it to CUSTOM.INF.
- Open the CUSTOM.INF file and replace the word Jet with Custom.

- Within this folder, you will find a theme.css file. This is where you’ll be making your edits to the css to acheive the look you desire.
- Up until this point, SharePoint is still not aware of the new theme you’ve just created. There’s one final edit you’ll have to make to the SPTHEMES.XML file.
- Within the
1
| C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1033 |
folder, open the SPTHEMES.XML file and add a new entry for your newly created theme. If you want this list to remain alphabetical, make sure you place the entry alphabetically in the list.

- Obviously, we don’t have a thcustom.gif image at this point in time because we have not actually made any changes to the theme.css file. Once you’ve completed that, and you’ve achieved the look and feel that you’d like, you’ll want to take a screenshot and create an image and store it in the located indicted in the SPTHEMES.XML file. Just a note, typically, I’ll open up an existing image, like the thjet.gif in Photoshop or some similar program to determine the image size. I then resize my screenshot to match. If someone has a better suggestion for doing this, I’d love to hear it!
- Save your file and reset the IIS pool for that web app. Alternatively, you can do an entire IIS reset. The syntax for recycling the application pool is as follows (Resource for this was Developer’s Guide To Windows SharePoint Services 3.0):
Run the following command where SharePointAppPool is the name of the application pool that needs to be reset:
1
| %systemroot%\system32\iisapp.vbs /a SharePointAppPool /r |
You can also right click on the application pool in IIS and choose Recycle from the context menu
- One thing to note. I’ve found that when making edits to my theme, they don’t show up right away. After saving the theme.css file, within SharePoint, I have to switch to another theme, then change back to my custom theme and do a page refresh before I see my changes take effect. I am not sure what causes this behavior, but just keep that in mind when testing out your changes. You won’t see them by simply refreshing the page while your theme is active.
In this example, I will focus specifically on a scenario where I had to write code that would overwrite the CustomQuickAccess.xml file located within the masterpage gallery.
Typically, up until this point, I had been dealing with simply uploading new files to a SharePoint library when a feature was activated. Simple enough, however, what happens if you’re trying to overwrite a file that already exists? I’ve attached the file to this post here for reference, but let’s go over the basics for doing something of this sort.
- The first step was to create a new feature and overwrite the FeatureActivated method. I’ll assume you’re familiar with this already, but if not, feel free to drop me a note and I’ll assist where I can.
- The code inside of the FeatureActivated method is pretty straightforward. Create a web object, declare a variable to store the file system path where the feature was activated so that we can grab the file we want to overwrite with and finally, call another method to do the file overwriting, with elevated privileges.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| override void FeatureActivated(SPFeatureReceiverProperties properties)
{
WriteMessageToEventLog(“Feature Activatedâ€);
try
{
web = (SPWeb)properties.Feature.Parent;
// set the directory path on the file system where the feature was activated
directoryPath = properties.Definition.RootDirectory;
// run with elevated permissions so we can overwrite the file
SPSecurity.RunWithElevatedPrivileges(OverwriteFile);
}
catch (Exception ex)
{
WriteMessageToEventLog(ex.ToString());
}
} |
- Now let’s look at the OverwriteFile method. What we’re doing here is reading the contents of the file into a byte array and uploading that file into the Editing Menu folder, specifiying that we want to overwrite the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| public void OverwriteFile()
{
// create a new site object so that elevation works properly
SPSite site = new SPSite(web.Site.ID);
// copy to location
string url = null;
// get the url to the local file
string[] localFile = System.IO.Directory.GetFiles(directoryPath, â€*.xmlâ€, System.IO.SearchOption.TopDirectoryOnly);
// define a fstream object so we can read the contents of the file into a byte array
FileStream fstream = File.OpenRead(localFile[0]);
byte[] contents = new byte[fstream.Length];
fstream.Read(contents, 0, (int)fstream.Length);
fstream.Close();
// get a handle to the master page gallery
SPList masterPageGallery = site.OpenWeb().Lists[“Master Page Galleryâ€];
// get a handle to the folder we want to upload the file to
SPFolder editingMenuFolder = masterPageGallery.RootFolder.SubFolders[“Editing Menuâ€];
SPFile customQuickAccessFile = editingMenuFolder.Files[“CustomQuickAccess.xmlâ€];
// build the destination copy url
url = site.Url + â€/†+ editingMenuFolder.Url + > â€/â€;
// check out the file, replace it with the modified one, and check it back in, publish and approve
customQuickAccessFile.CheckOut();
customQuickAccessFile.CopyTo(url + “CustomQuickAccess_Original.xmlâ€, true);
WriteMessageToEventLog(editingMenuFolder.ServerRelativeUrl);
WriteMessageToEventLog(editingMenuFolder.Url);
// check in new file
customQuickAccessFile = editingMenuFolder.Files.Add(url + “CustomQuickAccess.xmlâ€, contents, true);
customQuickAccessFile.CheckIn(“File over-written by activiating the Published Page View Featureâ€);
customQuickAccessFile.Publish(“File published by activating the Published Page View Featureâ€);
customQuickAccessFile.Approve(“Approved by Published Page View Featureâ€);
site.Close();
} |