SaaS CMS has officially launched! Learn more now.

Account access right does not match while the account used programmatically


I have created an account and assigned full access to it for the whole tree. When I try to use that account programmatically in a scheduled job by calling 

PrincipalInfo.CurrentPrincipal = PrincipalInfo.CreatePrincipal(ConfigurationManager.AppSettings["ScheduledJob.AdminUser"]);

I get an exception 

Access was denied to content X. The current user does not have sufficient rights to delete the page with id X.

while I try to call _contentRepository.MoveToWastebasket(content.ContentLink). But when I manually log in the edit mode with the same account, I have no problems with moving content to waste basket.

How should I use the account in order to be able to move content to the waste basket programmatically?

Edited, May 06, 2017 0:00

Does it work if you set Thread.CurrentPrincipal ?

May 06, 2017 0:19

It seems to be working! Thanks!

May 06, 2017 0:52

Any special permissions are required to delete media? I still get that exception when I try to move an image to the waste basket...

May 06, 2017 1:10

I was being glad too early... I am still experiencing the exception... frown

May 06, 2017 9:06

Moving media to the waste basket is a bit different.

You need to set PrincipalInfo.CurrentPrincipal and Delete permission on "For all sites" folder (or the folder that contains media).
If you're deleting content programmatically, you could skip the waste basket and override access rights like this:
_contentRepository.Delete(content.ContentLink, false, AccessLevel.NoAccess);

The same code can be used for all content types.

May 06, 2017 9:16

I see. I will try with skipping the waste basket.

May 06, 2017 9:46

It seems to be working but I'm still testing. One question though. Do I still need to set a principal if I skip moving to the waste basket?

May 06, 2017 22:48


May 06, 2017 23:09

this should help 

_contentRepository.Move(existingPersonPage.ContentLink, ContentReference.WasteBasket, AccessLevel.NoAccess, AccessLevel.NoAccess);

Jun 28, 2019 11:13
Laurent - Apr 11, 2023 13:36
Was looking for this! Thank you Nitin Anand
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.