Try our conversational search powered by Generative AI!

Correct way to create ContentFolders?

Vote:
 

Hi there.

I'm trying to create ContentFolders programmatically but I am not sure I'm doing it the right way. It seems that I have to save the new folder with AccessRight.NoAccess to not get an AccessDeniedException (Current user is WebAdministrator) But then I can't perform any operations on the folder in the UI. Here's my code:

public ContentFolder CreateFolder(int parentFolderId, string folderName)
{
var parentFolder = GetFolder(parentFolderId);

if (parentFolder == null)
return null;

var newFolder = contentRepository.GetDefault<ContentFolder>(new ContentReference(parentFolder.ContentLink.ID));
newFolder.Name = folderName;

contentRepository.Save(newFolder, SaveAction.Publish, AccessLevel.Publish);

return newFolder;
}

 

Please advice
/Alex

#81386
Feb 17, 2014 0:38
Vote:
 

You can try with:

contentRepository.Save(newFolder, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

Regards.

/Q

#81394
Feb 17, 2014 4:29
Vote:
 

Yes. AccessLevel.NoAccess will work and keeps me from getting an exception when running the code but I cannot edit the folder from the UI.

Regards
Alex

#81425
Feb 17, 2014 11:20
Vote:
 

What is the parent in this case?

By default a newly created content item will inherit the access from it's parent. If you go into admin mode and checks the access rights for the item and it's parent, does the user (you??) have enough access to modify the item?

#81435
Feb 17, 2014 13:13
Vote:
 

Ok, did a little more research. The folders we are talking about are migrated from VPP. Before you could set access rights, change name or otherwise do stuff with the folders from the UI but now i can't seem to find the access editor and the edit choices on the folder are greyed out.

But, to answer your question, Johan: The parent in this case is a contentfolder located two steps down from the folder named "For this site". I am logged in as a WebAdmin user when the code runs and also when in the UI, so I think I should be allowed to.

#81448
Feb 17, 2014 15:59
Vote:
 

If anyone needs it here's the proper way to do it.

public ContentFolder CreateFolder(int parentFolderId, string folderName)
{
var parentFolder = GetFolder(parentFolderId);

if (parentFolder == null)
return null;

if (FolderExists(parentFolderId, folderName))
throw new DuplicateNameException();

var newFolder = contentRepository.GetDefault<ContentFolder>(parentFolderId.GetContentReference());
newFolder.Name = folderName;

var newFolderRef = contentRepository.Save(newFolder, SaveAction.Publish);
/*  Set access rights */

var securityDescriptor =
_securityRepository.Get(folderId.GetContentReference()).CreateWritableClone() as IContentSecurityDescriptor;

securityDescriptor.IsInherited = false;

securityDescriptor.AddEntry(new AccessControlEntry(someEpiServerUserId, AccessLevel.Delete | AccessLevel.Create | AccessLevel.Read | AccessLevel.Edit | AccessLevel.Publish, SecurityEntityType.User));
_securityRepository.Save(folderId.GetContentReference(), securityDescriptor, SecuritySaveType.Replace);



return newFolder;
}

#81620
Feb 20, 2014 16:46
* 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.