It's all about the answers!

Ask a question

Trying to get a collection of IFileItems from a known workspace and a known sandbox

Richard Good (872157) | asked Apr 30 '15, 5:38 a.m.
edited Apr 30 '15, 5:39 a.m.

Trying to use the following sort of code on all the files in a workspace, struggling to get hold of all the files in a workspace as a collection of IFileItems such that I can add tons of custom properties. Probably missing something obvious, help appreciated!! I have tried using scm for my task, but it takes about a second to add each property, which means my migrations take for ever, adding a tiny amount of data should not take a second a throw, hence using the java-api route.

IFileItem fileWorkingCopy = (IFileItem) aFile.getWorkingCopy();
	fileWorkingCopy.setUserProperty("MyProperty", "MyValue");
	fWorkspace.commit(fChangeSet, Collections
					.save(fileWorkingCopy)), fMonitor);

Surya Tripathi commented May 01 '15, 5:58 p.m. | edited May 01 '15, 5:58 p.m.

 Are you looking for code that you can use to get a list of file items?

IWorkspaceConnection connection; 
IWorkspaceConfiguration configuration = connection.configuration();
This will provide you a list of items in the workspace for each component.

Accepted answer

permanent link
Ralph Schoon (62.7k33643) | answered May 05 '15, 3:05 a.m.
For the reverse direction look at

and the related links if needed.
Richard Good selected this answer as the correct answer

Richard Good commented May 11 '15, 11:49 a.m.

Thanks Ralph. I think this is the way forward. I have written some code that I think should do the trick, will test and report back if I have any unexpected issues. Note that I did a search on all this and found your rsjazz links, but this scmlounge is blocked by the company firewall, because it thinks its a non work related blog I guess. Shame as it has the best example for my problem ;-)

Cheers Richard

3 other answers

permanent link
Ralph Schoon (62.7k33643) | answered May 05 '15, 3:02 a.m.
Please see for an example.

permanent link
Richard Good (872157) | answered May 26 '15, 5:12 a.m.
edited May 26 '15, 5:30 a.m.

I have had a chance to write the code, but am some how fundamentally misunderstanding something or have made a daft error I cannot see. The following code runs through, but no changes to the metadata are checked in to the workspace, stepped through the code and the right things are being extracted and passed to the set user properties code and nothing gives me an error, probably need to connect to something or disconnect to something to concrete the change. Anyway here is the relevant code, can someone tell me what is wrong with it. Note I am using global variables and a recursive procedure, then applying all the changes to the workspace at the same time using the save operations "ops" global variable filled out in the recursive procedure. Any pointers appreciated probably some daft mistake.

Note I expect my error is in this bit of code: -

v.setUserProperty(metaDataFieldsToExtract[i], metaDataStr);
// Save the change into a change set  
ISaveOp saveOp =;

Here comes the code listing : -

 List<IWorkspaceHandle> workspaces = findConnectionByName(teamRepository,tempWorkSpace, IWorkspaceSearchCriteria.WORKSPACES, monitor);
 IWorkspaceHandle firstFound = null;
 IWorkspaceConnection connection = null;
 if (!workspaces.isEmpty()) {
  firstFound = workspaces.get(0);
  connection = wm.getWorkspaceConnection(firstFound, monitor);
        //If we find our workspace then we need to get our component from it - but component can be in many workspaces, but only one owner
        //things could go horribly wrong here, can't easily search for components on workspace
        if(connection != null) {
            IComponentHandle mycomp;
            IComponentSearchCriteria fred = IComponentSearchCriteria.FACTORY.newInstance();
            List<IComponentHandle> comps = wm.findComponents(fred, 20, monitor);
            mycomp = comps.get(0);
            if(myTeamArea != null) {
                wm.setComponentOwnerAndVisibility(mycomp, myTeamArea, IReadScope.FACTORY.createTeamAreaPrivateScope(), monitor);
            else {
                wm.setComponentOwnerAndVisibility(mycomp, projectArea, IReadScope.FACTORY.createProcessAreaScope(), monitor);
            IConfiguration compConfig = connection.configuration(mycomp);
            IFileContentManager contentManager = FileSystemCore.getContentManager(teamRepository);
            IConfigurationOpFactory opFactory = connection.configurationOpFactory();
            ops = new ArrayList<IWorkspaceConnection.IConfigurationOp>();
            // Fetch the items at the root of each component. We do this to initialize our
            // queue of stuff to download.
            Map<String, IVersionableHandle> handles = compConfig.childEntriesForRoot(null);
            List<IVersionable> items = compConfig.fetchCompleteItems(new ArrayList<IVersionableHandle>(handles.values()), null);
            //Need to get childEntries from handles map too as does not seem to recurse down, may need recursive procedure - bit of a pain!!                 
            getMetaDataRecurse(contentManager, compConfig, rootFolder + "/", items, opFactory);
            // create a change set for our metadata changes
            IComponent richardsComponent = findComponent(teamRepository, connection, componentAndEcProjName);
            IChangeSetHandle cs = getOrCreateChangeSetInComponent(connection,richardsComponent);
            System.out.println("Committing meta data to workspace");
            connection.commit(cs, ops, null);
            connection.closeChangeSets(Collections.singletonList(cs), null);
            SCMPlatform.getWorkspaceManager(teamRepository).setComment(cs, "Added Dimensions metadata", null);
 private static void getMetaDataRecurse(IFileContentManager contentManager, IConfiguration compConfig, String path,
   List<IVersionable> toLoad, IConfigurationOpFactory opFactory)
   throws FileNotFoundException, TeamRepositoryException, IOException {
          String[] metaDataFieldsToExtract = {"file-version", "item-status", "project", "item-spec", "item-uid", "project-uid"};
                Pattern[] MetaFieldPatterns = new Pattern[metaDataFieldsToExtract.length];
                for(int i=0;i<metaDataFieldsToExtract.length;i++ ) {
                     MetaFieldPatterns[i] = Pattern.compile("^" + metaDataFieldsToExtract[i] + "=(.*)",Pattern.MULTILINE );
  for (IVersionable v : toLoad) {
   if (v == null) {
    // The versionable will be null if we don't have permission to read it
   if (v instanceof IFolder) {
    // Write the directory
    String dirPath = path + v.getName() + "/";
    Map<String, IVersionableHandle> children = compConfig.childEntries((IFolderHandle)v, null);
    List<IVersionable> completeChildren = compConfig.fetchCompleteItems(new ArrayList<IVersionableHandle>(children.values()), null);
    getMetaDataRecurse(contentManager, compConfig, dirPath, completeChildren, opFactory);
   else if (v instanceof IFileItem) {
    // Get the file contents and write them into the directory
    IFileItem file = (IFileItem) v;
                                file = (IFileItem) file.getWorkingCopy();

                                //need to grab metadata at this point can do this by getting the name then runniing dmmetadata then adding properties from here                                                          
                                String FullFile= path + v.getName();
                                String dmMetaCommand =  dimensionsLocation + "\\dmmeta.exe get \"" + FullFile + "\" --file c:\\temp\\output.txt";
                                FileInputStream readTempFile = new FileInputStream(new File("c:\\temp\\output.txt"));
                                String metaDataFieldsToProcess = IOUtils.toString(readTempFile);

                                for(int i=0;i<metaDataFieldsToExtract.length;i++ ) {
                                    Matcher findMetaDataHex = MetaFieldPatterns[i].matcher(metaDataFieldsToProcess);
                                    if(findMetaDataHex.find()) {
                                        String metaDataStr =;
                                        if(Pattern.matches("^[A-F0-9]+$", metaDataStr) && metaDataStr.length() % 2==0 && metaDataStr.length()>4) {
                                             metaDataStr = hexToASCII(metaDataStr);
                                      v.setUserProperty(metaDataFieldsToExtract[i], metaDataStr);
                                // Save the change into a change set  
                                ISaveOp saveOp =;
   else {
    // The item could be a symlink or an even more exotic type of versionable. Skip it.
    System.err.println("Cannot load " + v.getName() + " of type " + v.getClass().getSimpleName());

permanent link
Richard Good (872157) | answered May 26 '15, 6:45 a.m.

Might have got my versionables and fileworkingcopies mixed up!

works a lot better when you change




Your answer

Register or to post your answer.