cancel
Showing results for 
Search instead for 
Did you mean: 

Json for updating meta-data

arpit_gupta
Champ in-the-making
Champ in-the-making
I need to update meta data of my documents using Rest api(web script).

I am calling the following service with posting the json file containing the metadata.

http://192.168.100.106:8180/alfresco/service/api/metadata/node/workspace/SpacesStore/67f7081b-7236-4...

and getting the response as success. but still alfresco is not updating the meta data of the documents.
so i think there is the problem with my json format. i have tried various formats but none worked.

so can someone please help me what json is needed for updating meta-data of the documents.

Thanks in advance,

arpit
9 REPLIES 9

mrogers
Star Contributor
Star Contributor
Your URL seems to have been truncated.

Which script are you using and what are you posting to it?

arpit_gupta
Champ in-the-making
Champ in-the-making
Thanks for the reply

i am using default meta data storage script. Description can be found here http://wiki.alfresco.com/wiki/Repository_RESTful_API_Reference#Node_Metadata_Storage_Service.

arpit_gupta
Champ in-the-making
Champ in-the-making
Script used for the service is

File: org/alfresco/repository/metadata/metadata.post.json.js


main();

function main()
{
   if (url.templateArgs.store_type === null)
   {
      status.setCode(status.STATUS_BAD_REQUEST, "NodeRef missing");
      return;
   }

   // nodeRef input
   var storeType = url.templateArgs.store_type;
   var storeId = url.templateArgs.store_id;
   var id = url.templateArgs.id;
   var nodeRef = storeType + "://" + storeId + "/" + id;
   var node = search.findNode(nodeRef);
   if (node === null)
   {
      status.setCode(status.STATUS_NOT_FOUND, "Not a valid nodeRef: '" + nodeRef + "'");
      return null;
   }

   // Set passed-in properties
   if (json.has("properties"))
   {
      var properties = jsonToObject("properties");
      for (property in properties)
      {
         node.properties[property] = properties[property];
      }
   }

   // Set passed-in mimetype
   if (json.has("mimetype"))
   {
      node.mimetype = json.get("mimetype");
   }
  
   // Set passed-in tags
   if (json.has("tags"))
   {
      // Get the tags JSONArray and copy it to a native JavaScript array
      var jsonTags = json.get("tags"),
         tags = [];
     
      for (var t = 0; t < jsonTags.length(); t++)
      {
         tags.push(jsonTags.get(t));
      }

      if (tags.length > 0)
      {
         node.tags = tags;
      }
      else
      {
         node.clearTags();
      }
   }
   node.save();
}

function jsonToObject(p_name)
{
   var object = {};
   var jsonObj = json.get(p_name);
   var names = jsonObj.names();
   var name;
   for (var i = 0, j = names.length(); i < j; i++)
   {
      name = names.get(i);
      object[name] = jsonObj.get(name);
   }
   return object;
}

function jsonToArray(p_name)
{
   var array = [];
   var jsonArray = json.get(p_name);
   for (var i = 0, j = jsonArray.length(); i < j; i++)
   {
      array.push(jsonArray.get(i));
   }
   return array;
}

arpit_gupta
Champ in-the-making
Champ in-the-making
and the json i am posting is

{
    "properties":{
          "{http://www.alfresco.org/model/content/1.0}agreement_id':'1234",
         "{http://www.alfresco.org/model/content/1.0}property_id':'4567"
      }
}
but still it's not working.
am i missing something?
please help me out…

tremalnaik
Champ in-the-making
Champ in-the-making
Hi,

I have the same problem although I using Java and do not working in spite of this, the return message is always "success"…

arpit_gupta
Champ in-the-making
Champ in-the-making
Issue is resolved now.

I was not setting the request content type correctly.

StringEntity entity = new StringEntity(getMetaDataAsJson(metadata));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

This resolved my problem…

tremalnaik
Champ in-the-making
Champ in-the-making
Thank you so much!

This has been the clue to resolve my problem.

An example just in case somebody uses Abdera o similar to the operation in Java:


Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);

String uri = "http://localhost:8080/alfresco/service/api/metadata/node/workspace/SpaceStore/{id}?alf_ticket=ticket";

String jsonMessage ="{\"properties\":{\"cm:title\":\"Texas\",\"cm:description\":\"Descripción de Texas\"}}";
InputStream is = Utilidades.stringToInputStream(jsonMessage);

RequestOptions options = new RequestOptions();
options.setHeader("Content-Type", "application/json");

ClientResponse resp = client.post(uri, is, options);

how will you set multi value properties in the above format? Is there another way other than using json? Can the properties be set during upload or is it a separate call?

anujcb
Confirmed Champ
Confirmed Champ
The upload url:::http://127.0.0.1:8080/alfresco/service/api/metadata/node/workspace/SpaceStore/de17d530-1f13-4eb1-91b...


HTTP/1.1 404 Not Found
body
{
    "status" :
  {
    "code" : 404,
    "name" : "Not Found",
    "description" : "Requested resource is not available."
  }, 
 
  "message" : "Not a valid nodeRef: 'workspace:\/\/SpaceStore\/de17d530-1f13-4eb1-91b5-7049892a5055\/'", 
  "exception" : "",
 
  "callstack" :
  [
       
  ],
 
  "server" : "Community v5.0.0 (c r91299-b145) schema 8,009",
  "time" : "Apr 24, 2015 4:21:29 AM"
}


Here is the code


         urlString = "http://'+ipaddress+':8080/alfresco/service/api/metadata/node/workspace/SpaceStore/'+nodeId+'/?alf_ti..."
               + authTicket;
         System.out.println("The upload url:::" + urlString);
         client = new HttpClient();

         mPost = new PostMethod(urlString);
         itemNameValue=new ArrayList();
         itemNameValue.addAll(Arrays.asList(new String[] { "Item1","Item2"}));

         String jsonMessage ="{\"properties\":{\"rs:vendor\":\"Texas\",\"rs:itemName\":\"Descripción de Texas\"}}";
         InputStream stream = new ByteArrayInputStream(jsonMessage.getBytes(StandardCharsets.UTF_8));
         
         mPost.addRequestHeader("Content-Type", "application/json");
         mPost.setRequestBody(stream);
         

         statusCode1 = client.executeMethod(mPost);
         System.out.println("statusLine>>>" + statusCode1 + "……"
               + "\n status line \n" + mPost.getStatusLine() + "\nbody \n"
               + mPost.getResponseBodyAsString());
         mPost.releaseConnection();