Account access right does not match while the account used programmatically

Vote:
 

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?

#178271
Edited, May 06, 2017 0:00
Vote:
 

Does it work if you set Thread.CurrentPrincipal ?

#178272
May 06, 2017 0:19
Vote:
 

It seems to be working! Thanks!

#178273
May 06, 2017 0:52
Vote:
 

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

#178274
May 06, 2017 1:10
Vote:
 

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

#178275
May 06, 2017 9:06
Vote:
 

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.

#178276
May 06, 2017 9:16
Vote:
 

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

#178277
May 06, 2017 9:46
Vote:
 

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?

#178280
May 06, 2017 22:48
Vote:
 

No 

#178281
May 06, 2017 23:09
Vote:
 

this should help 

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

#205101
Jun 28, 2019 11:13