I'm using the DFS .NET Productivity Layer to insert a document to a folder path in Documentum that may or may not exist. We are using a custom folder type, that inherits from dm_folder. This means I can't use the ObjectService's CreatePath method; e.g., objectService.CreatePath("/mycabinet/invoices/2017/2017-05/2017-05-02"). I came up with a brute force method below that tries to create each folder in the path, ignoring errors any errors because the folders may already exist. After I call this method, I associate my document with the folder path and insert it.
This approach requires a lot of service calls and seems kind of messy. I'm curious if anyone is aware of a better approach. Thanks!
public void CreateCustomFolderPath(string folderPath, string folderType)
{
var folderPathItems = folderPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
// The first item in the path is the cabinet. There must be at least one subfolder
if (folderPathItems.Count() < 2)
{
throw new Exception($"There must be at least 2 items in the folderpath: {folderPath}");
}
// The first item in the path is the cabinet. We can assume that the cabinet
// exists and not try to create it. Furthermore, it is a different type of Documentum
// object
string parentPath = "/" + folderPathItems[0];
string childPath = string.Empty;
string childFolderName = string.Empty;
for (int i = 1; i < folderPathItems.Count(); i++)
{
try
{
childFolderName = folderPathItems[i];
childPath = parentPath + "/" + childFolderName;
// Create the object that represents the parent folder that already exists
ObjectIdentity parentPathIdentity = new ObjectIdentity(new ObjectPath(parentPath), _repository);
// Create the object that represents the child folder that needs to be created
DataObject childFolderDataObject = new DataObject(new ObjectIdentity(_repository), folderType);
PropertySet childFolderProperties = new PropertySet();
childFolderProperties.Set("object_name", childFolderName);
childFolderDataObject.Properties = childFolderProperties;
// create the relationship between the parent and folder
ReferenceRelationship parentFolderRelationship = new ReferenceRelationship()
{
Name = Relationship.RELATIONSHIP_FOLDER,
Target = parentPathIdentity,
TargetRole = Relationship.ROLE_PARENT
};
childFolderDataObject.Relationships.Add(parentFolderRelationship);
// Invoke the service to create the child folder
var serviceContext = CreateServiceContext(_repository, _userName, _password);
IObjectService objectService = ServiceFactory.Instance.GetRemoteService<IObjectService>(serviceContext, "core", _serviceUrl);
objectService.CreatePath("")
OperationOptions operationOptions = null;
DataPackage resultDataPackage = objectService.Create(new DataPackage(childFolderDataObject), operationOptions);
}
catch (Exception)
{
// ignore all errors and hope this works
// We are attempting to create each folder in the folder path every time an object is added
// so there will be many many errors because folders already exist
// Unfortunately, there is not a better way to do this.
}
// finally set the parentPath to childPath and create the next child folder in the loop
parentPath = childPath;
}
}