cancel
Showing results for 
Search instead for 
Did you mean: 

Create a link to a node (file or folder)

sergio
Champ in-the-making
Champ in-the-making
Dear WS-ers,

I have to create programmatically a link to an existing node into another node of the space. I have not yet found any documentation or useful example to solve this problem.

Is there anyone who tried and done this task succesfully?

Is there any piece of code available?

Hoping someone could help me,

best regards,

Sergio
13 REPLIES 13

sergio
Champ in-the-making
Champ in-the-making
Is there anyone who has experience in creating link to files or folders at run-time using web-services APIs?

Regards,

Sergio

kevinr
Star Contributor
Star Contributor
I've not personally used the WebServices API, but i can tell you how a link is constructed in the model - assuming you know how to create types and set properties in WebServices then it should be no problem for you Smiley Happy

A link to a file is of type "app:filelink" (which extends the base 'cm:link' type). The object has a single NodeRef value property called "cm:destination" which you set to the object you wish to link to -that's it. Creating a link to a folder is very similar expect you create a node of type "app:folderlink" and set the same property.

Hope this helps,

Kevin

sergio
Champ in-the-making
Champ in-the-making
Many thanks Kevin.

I will try on your suggestions.

All the best,

Sergio

sergio
Champ in-the-making
Champ in-the-making
Hi all.

Due to the kind suggestions from Kevin here you can find an example of code that solves the problem of creating a link to a file using WS APIs.

public void wsCreateLink(String sourceNode,
                                         String destinationNode)
   {
      String storeSchema = Constants.WORKSPACE_STORE;
     String storeAddress = "SpacesStore";
     final Store STORE = new Store(storeSchema, storeAddress);
     

     try
     {
        //node to link
        Reference ref = new Reference();
        ref.setStore(STORE);
        ref.setUuid(sourceNode);
        
        //node destination 14c29a12-7be0-11db-83b6-f5ef9685f99e
        ParentReference destinationFolder = new ParentReference();
        destinationFolder.setStore(STORE);
        destinationFolder.setUuid(destinationNode);
        destinationFolder.setAssociationType(Constants.ASSOC_CONTAINS);
        destinationFolder.setChildName("poppi");
     
      Predicate pred = new Predicate();
      pred.setStore(STORE);
      pred.setNodes(new Reference[]{ref});
     
     
      NamedValue[] prop = new NamedValue[4];
      prop[0] = new NamedValue();
      prop[0].setName(Constants.PROP_NAME);
      prop[0].setValue("Name");
      prop[0].setIsMultiValue(false);
      prop[1] = new NamedValue();
      prop[1].setName(Constants.PROP_TITLE);
      prop[1].setValue("Title");
      prop[1].setIsMultiValue(false);
      prop[2] = new NamedValue();
      prop[2].setName(Constants.PROP_DESCRIPTION);
      prop[2].setValue("Description");
      prop[2].setIsMultiValue(false);
      prop[3] = new NamedValue();
      prop[3].setName("{http://www.alfresco.org/model/content/1.0}destination");
      prop[2].setName(Constants.PROP_DESCRIPTION);
      prop[2].setValue("Descrizione");
      prop[2].setIsMultiValue(false);
      prop[3].setValue("workspace://SpacesStore/75710e09-83a4-11db-a5ec-7f23e300cdcd");
      prop[3].setIsMultiValue(false);
     
      CMLCreate create = new CMLCreate();
      create.setParent(destinationFolder);
      create.setAssociationType(Constants.ASSOC_CONTAINS);
      create.setType("{http://www.alfresco.org/model/application/1.0}filelink");
      create.setProperty(prop);
      create.setChildName("Childname");
     
      CML cml = new CML();
      cml.setCreate(new CMLCreate[]{create});
     
      UpdateResult[] result = repositoryService.update(cml);   
      }
     catch (Exception e1)
     {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
   }
The code works well. the only problem that remains unsolved for me (please let me know if someone has knowledge about it) is how to show into the web client the link icon instead of the normal icon. This is only a detail but it could be useful would like to provide me with a solution.

Many thanks to all.

Hoping havind done a useful thing for others,

all the best.

Sergio

fantastic340
Champ in-the-making
Champ in-the-making
Hi sergio,
Could you tell me which class and library were you used in "repositoryService.update(cml)"?
thanks

kaynezhang
World-Class Innovator
World-Class Innovator
web-service-client jar and org.alfresco.webservice.repository.RepositoryServiceSoapPort class

sergio
Champ in-the-making
Champ in-the-making
If someone wants to show the link icon into web client pages the only thing to is setting the QName(name) property for the new link node with the extension ".lnk", as shown in the following piece of code:

prop[0].setValue("Name.lnk");

The web client recognizes automatically the link and shows it using the special icon for links.

Hoping this could be useful,


Cheers,

Sergio

kevinr
Star Contributor
Star Contributor
Thanks for sharing the code! Smiley Happy

Kevin

sergio
Champ in-the-making
Champ in-the-making
Not at all Kevin!

It is a pleasure for me to share with the community everything I think it could be useful. I think that only by this way it is possible to improve the personal knowledge and the efficiency.

All the best,

Sergio