February 19th, 2015
  • Programmically change items
  • EPiServer
  • NullReferenceException
  • Html.RenderAction

Programmatically changing EPiServer items - The nice way

When you use MVC and you use Html.RenderAction within your view, you cannot use the 'Alloy' logic to programmatically add, update or remove pages and/or blocks in EPiServer within your action or constructor. If you do, you can expect an 'Object reference not set to an instance of an object' upon your 'Html.RenderAction' line when you try to edit the page in the EPiServer backend.

With the 'Alloy' logic I mean:

PrincipalInfo.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Task"), new[] { "Administrators" });
/* Update, modify or delete EPiServer items */
PrincipalInfo.CurrentPrincipal = null

This NullReferenceException occurs because you dump your EPiServer user for the rest of your request and this causes EPiServer to not know which language, etc. to use to render the action.

But even without the NullReferenceException, this is not the right way to remove the temporary created administrator principal.

You could better create a SecurityDisabler:

using EPiServer.Security;
using System;
using System.Security.Principal;

namespace Your.Namespace
{
    public class SecurityDisabler : IDisposable
    {
        private readonly IPrincipal _principal;

        public SecurityDisabler()
        {
            _principal = PrincipalInfo.CurrentPrincipal;
            PrincipalInfo.CurrentPrincipal = new GenericPrincipal(
            new GenericIdentity("Task"),
            new[] { "Administrators" });
        }

        public void Dispose()
        {
            PrincipalInfo.CurrentPrincipal = _principal;
        }
    }
}

Using it you can change EPiServer pages and/or blocks programmatically without ever having any side effects:

using (new SecurityDisabler())
{
    /* Update, modify or delete EPiServer items */
}
< Previous post Overview posts Next post >
Xbdypg https://newfasttadalafil.com/ - Cialis <a href=https://newfasttadalafil.com/>viagra cialis online</a> Causes almost all cases are due to impaired absorption Niscfc Potenzmittel Cialis Billig Druuhz https://newfasttadalafil.com/ - buying cialis online safe
scource, June 12th, 2022
https://bestadalafil.com/ - generic for cialis Theyre Lecture The Master PlanPublic Health and Policy iStockphotoThinkstock. Qckklq <a href="https://bestadalafil.com/">Cialis</a> Accutane 40 Mg Online Imizut Cialis And Viagra Online https://bestadalafil.com/ - Cialis
incuple, April 15th, 2022
https://bestadalafil.com/ - cheap generic cialis Levitra Contraindications Exayjl <a href="https://bestadalafil.com/">brand name cialis online</a> Lande R. https://bestadalafil.com/ - cialis prescription online bones another name for somatotropin is growth hormone
orawifT, April 12th, 2022
http://slkjfdf.net/ - Wuxotg <a href="http://slkjfdf.net/">Ucizev</a> jya.pbfa.laurenshuizer.nl.tml.oq http://slkjfdf.net/
ocupagu, March 26th, 2022

Leave a comment

= Thanks for your comment =