• Home
  • About
Subscribe: Posts | Comments | E-mail
  • SharePointTopics on Microsoft SharePoint Technology
  • Silverlight, WPFSilverlight, Windows Presentation Foundation
  • MiscellaneousRandom bits and rants

Rouslan Grabar on NET, SharePoint, SilverLight, WPF and other technology related topics

Posts Tagged ‘tricks’


Posted on April 4, 2009 - by Rouslan Grabar

Expect the unexpected to happen while using SPSecurity.RunWithElevatedPrivileges method

While building small feature that takes advantage of new accelerator feature in Internet Explorer 8, I’ve encountered a weird side effect of using SPSecurity.RunWithElevatedPrivileges method.

I used this method to be able to write IE8 OpenService Accelerator XML file inside LAYOUTS/Feature_Name folder. The code was using user’s current context to determine the Url of SPWeb object for which to create the XML file.

In my code I misplaced the scope at which I obtained the web’s Url and got unexpected results on the production server. Below is the snippet from my actual code:

//here is where I moved offending url variable initialization
string webPreviewUrl = w.Url;
Guid siteId = w.Site.ID;
Guid webId = w.ID;
SPSecurity.RunWithElevatedPrivileges(delegate
{
	using (SPSite site = new SPSite(siteId))
	{
		using (SPWeb web = site.OpenWeb(webId))
		{
			//this is where I had my webUrl initialy
			//string webPreviewUrl = web.Url;
			...
		}
	}
	...
});

When the buggy version of code was generating the XML file webPreviewUrl contained not the public address, but internal address of the web front-end. This is because the instance of SPWeb object is not equivalent and its properties differ for different users. When we obtain instance of SPWeb for account with Full Access rights, the instance is being set up like we are sitting in the server room and using the console to access the web server, hence the SPWeb.Url property contains local URL address in it.

PS: Please note the Guids outside the using statements. These Guids are there not by an accident! :)



  • Recent Posts

    • Доклад по PerformancePoint Server 2007 на сайте TechDays.ru
    • How to allow only Folder items to be created in the List root folder
    • Move list item into a subfolder in the same list
    • Краткий обзор возможностей SharePoint 2010 для конечных пользователей
    • Get Free Typemock licenses – ASP.NET bundle launch
  • Tags

    Для новичков Customization Developer Utility Fileds For Beginners IIS mac os x Misc MOSS MSDN photosynth Powershell Russian Screencast SharePoint SharePoint Designer Silverlight, WPF Snippet SPItemEventReceiver techdays timer job Toolbox tricks troubleshooting Virtualisation VSeWSS wcf web service Windows windows authentication WPF WSS
  • Blogroll

    • My personal journal in Russian.
    • Picture Downloader Software
    • Russian SharePoint Community
  • Archives

    • November 2009
    • October 2009
    • July 2009
    • May 2009
    • April 2009
    • March 2009
© 2009 Rouslan Grabar. All Rights Reserved.