Using SPContext anonymously

Paul Pascha, 14 december 2009

For one of our projects – a public facing site – we developed a custom control which renders an image. The control is placed within the MasterPage and the URL of the image to render is being determined by some rules. One of these rules is related to the PageLayout being used by the Page the user is visiting. For this rule to evaluate, we need the name of the PageLayout.

The most basic code I could think of for retrieving this value was the following

SPSecurity.RunWithElevatedPrivileges(delegate() {
  PublishingPage currentPage = PublishingPage.GetPublishingPage(SPContext.Current.ListItem);
  string pageLayoutName = currentPage.Layout.Name;
});

Running this code using Windows authentication works fine. Running this code in anonymous context however caused a challenge to appear when executing the line where pageLayoutName is assigned a value. The reason for this is that some objects referenced in the SPContext object were already created outside of the elevated context. So, to retrieve the name of the current PageLayout, I had to instantiate most of the objects needed to get to the PageLayout object manually. The following code worked for me:

SPSecurity.RunWithElevatedPrivileges(delegate() {
  using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Site.Url))
 
{
    using (SPWeb web = siteCollection.OpenWeb())
    {
      PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
      SPListItem pageListItem = ublishingWeb.PagesList.ItemsSPContext.Current.File.UniqueId];
      PublishingPage currentPage = PublishingPage.GetPublishingPage(pageListItem);
      PageLayout currentPageLayout = currentPage.Layout;
      currentPageLayoutName = currentPageLayout.Name;
    }
  }
});

The example above is obviously a very specific case in which we needed the name of the PageLayout used by the current Page. The key message here however is to watch out using SPContext (and other high-level SharePoint objects) within an RunWithElevatedPrivileges block while in anonymous context. It’s very likely to run into some differences compared to using the same elevated code in Windows authenication context.

14 juni 2010 desexterm

Hello, I am new to this forum so this is my initial post, happy to mee you all. Does any individual know how to play pokies on the web? Especially I am searching to play a game known as 50 lions. It is one of the most popular slot machine games accessible currently.
So if you know exactly where I can locate this I will appreciate your support. Thanks!
Check out the following videos and articles:
best pokie game
50 lions aussie slots
50 lions aussie slots

Geef een reactie

Jouw e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

*

De volgende HTML tags en attributen zijn toegestaan: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Categories

  • Tags

  • Blog Authors

    Amy
    anita
    Anneke
    Danny Burlage
    Dennis Vendel
    Freek Berson
    Gerard
    Iris
    Jasper Oosterveld
    Jean-Claude Chan
    Jorn
    Lab Chicks
    Luc Joziasse
    Maarten van Noort
    Maarten Wijsman
    Marlon
    Martijn Bellaard
    Natasja van Doorn
    Paul Pascha
    Peter Heuvelman
    Rick Slager
    Robert van Son
    Roeland Jimenez
    SanderZ
    Sjoerd Schudde
    Stefan van der Wiele
    Tim Heuperman
    Wortell
  • Archief