Copying Folders

The Documents and Media API lets you copy folders within a repository. You can’t, however, copy a folder between different repositories. Also note that copying a folder also copies its contents.

To copy a folder, use the DLAppService method copyFolder:

copyFolder(long repositoryId, long sourceFolderId, long parentFolderId, String name, 
        String description, ServiceContext serviceContext)

For a full description of the method and its parameters, see its Javadoc.

Follow these steps to use this method to copy a folder:

  1. Get a reference to DLAppService:

    private DLAppService _dlAppService;

    For more information on this, see the section on getting a service reference in the getting started tutorial.

  2. Get the data needed to populate the copyFolder method’s arguments. How you do this depends on your use case. The copy operation in this example takes place in the default Site repository and retains the folder’s existing name and description. It therefore needs the folder’s group ID (to specify the default site repository), name, and description. Also note that because the destination folder in this example is the repository’s root folder, the parent folder ID isn’t needed—Liferay DXP supplies a constant for specifying a repository’s root folder.

    In the following code, ParamUtil gets the folder’s ID from the request (javax.portlet.ActionRequest), and the service reference’s getFolder method gets the corresponding folder object. The folder’s getGroupId(), getName(), and getDescription() methods then get the folder’s group ID, name, and description, respectively:

    long folderId = ParamUtil.getLong(actionRequest, "folderId");
    Folder folder = _dlAppService.getFolder(folderId);
    long groupId = folder.getGroupId();
    String folderName = folder.getName();
    String folderDescription = folder.getDescription();
    ServiceContext serviceContext = ServiceContextFactory.getInstance(
                DLFolder.class.getName(), actionRequest);

    For more information on getting repository and folder IDs, see the getting started tutorial’s sections on specifying repositories and folders. For more information on ServiceContext, see the tutorial Understanding ServiceContext.

  3. Call the service reference’s copyFolder method with the data from the previous step. Note that this example uses the DLFolderConstants constant DEFAULT_PARENT_FOLDER_ID to specify the repository’s root folder as the destination folder:

            groupId, folderId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, 
            folderName, folderDescription, serviceContext);

Keep in mind that you can change any of these values to suit your copy operation. For example, if your copy takes place in a repository other than the default Site repository, you would specify that repository’s ID in place of the group ID. You could also specify a different destination folder, and/or change the new folder’s name and/or description.

Getting Started with the Documents and Media API

Understanding Service Context

Creating Folders

Updating Folders

Deleting Folders

Moving Folders and Files

« Copying and Moving EntitiesMoving Folders and Files »
Was this article helpful?
0 out of 0 found this helpful