Add custom metadata with new file upload

cancel
Showing results for 
Search instead for 
Did you mean: 
goranche89
Active Member

Add custom metadata with new file upload

Jump to solution

Hello,
I'm using PortCmis with AtomPub 1.1 in .Net Core and I'm trying to upload a file to Alfresco. I'm trying to set custom metadata., but I'm getting this error: Type classification is unknown!
Here is my code:


parameters[SessionParameter.User] = "myuser";
parameters[SessionParameter.Password] = "mypassword";
parameters[SessionParameter.AtomPubUrl] = "http://my-address-where-is-alfresco/alfresco/api/-default-/public/cmis/versions/1.1/atom";
parameters[SessionParameter.BindingType] = BindingType.AtomPub;
parameters[SessionParameter.PreemptivAuthentication] = "True";

IDictionary<string, object> properties = new Dictionary<string, object>();
properties[PropertyIds.Name] = fileName;

IList<string> secondaryTypes = new List<string>();
secondaryTypes.Add("custom:classification");

properties[PropertyIds.SecondaryObjectTypeIds] = secondaryTypes;
properties["classification"] = "test classification";

After this I'm creating contentStream in a proper way and using it to create document.

var newDoc = folder.CreateDocument(properties, contentStream, VersioningState.None);

Does anyone has same error "Type classification is unknown!" ???


3 Solutions

Accepted Solutions
angelborroy
Alfresco Employee

Re: Add custom metadata with new file upload

Jump to solution

When adding an aspect (secondaryType), you need to add the prefix "P:" to your aspect name.

secondaryTypes.Add("P:custom:classification");

You need also to be sure that the model definition has been deployed in Alfresco Repository.

Hyland Developer Evangelist

View solution in original post

kaynezhang
Advanced

Re: Add custom metadata with new file upload

Jump to solution

Alfresco model is just an xml ,so you can create the xml model file in c# and upload the xml file into Company Home/Data Dictionary/Models using cmis api.

View solution in original post

kaynezhang
Advanced

Re: Add custom metadata with new file upload

Jump to solution

Yes ,upload your xml to Repository/Models. and you also need to set cmisSmiley SurprisedbjectTypeId to D:cm:dictionaryModel,and cm:modelActive property to true. Then you can use your types and aspects defined in your model.

View solution in original post

9 Replies
angelborroy
Alfresco Employee

Re: Add custom metadata with new file upload

Jump to solution

When adding an aspect (secondaryType), you need to add the prefix "P:" to your aspect name.

secondaryTypes.Add("P:custom:classification");

You need also to be sure that the model definition has been deployed in Alfresco Repository.

Hyland Developer Evangelist
goranche89
Active Member

Re: Add custom metadata with new file upload

Jump to solution

Thank you! Do you know if I can define and deploy model from C# code or that can be done only in Alfresco repository by Admin?

kaynezhang
Advanced

Re: Add custom metadata with new file upload

Jump to solution

Alfresco model is just an xml ,so you can create the xml model file in c# and upload the xml file into Company Home/Data Dictionary/Models using cmis api.

goranche89
Active Member

Re: Add custom metadata with new file upload

Jump to solution

Hi kaynezhang,
Is there any tutorial how to do it? I've created some xml model file, but I'm not sure where to upload it either with C# or with drag and drop.

EddieMay
Alfresco Employee

Re: Add custom metadata with new file upload

Jump to solution

Hi @goranche89,

There are several tutorials for creating models in Alfresco. A very recent one by @Jendert , Creating a simple Contract Management Solution, is a very good introduction.   There are also Jeff Pott's Alfresco Developer Tutorials.

With regard to using the Alfresco ReST API, there's Gavin Cornwell's blog series. & also @Jendert API tutorial (still a work in progress).

HTH

Digital Community Manager, Alfresco Software.
Problem solved? Click Accept as Solution!
goranche89
Active Member

Re: Add custom metadata with new file upload

Jump to solution

Hi,
I've uploaded an xml to Repository/Models. I like that approach to upload xml and than use it as a custom model. But I don't know is that enough or I must do something else? I see that in Admin Tools there is no my custom model, and that other approach is to simply create it with wizard., but I don't want that.
So to recapitulate, I've uploaded my xml model to Models and I can see that document as document in xml format. What is next step to be able to use that custom model for uploading files with aspects defined in that model?

kaynezhang
Advanced

Re: Add custom metadata with new file upload

Jump to solution

Yes ,upload your xml to Repository/Models. and you also need to set cmisSmiley SurprisedbjectTypeId to D:cm:dictionaryModel,and cm:modelActive property to true. Then you can use your types and aspects defined in your model.

goranche89
Active Member

Re: Add custom metadata with new file upload

Jump to solution

Thank you all! It works! Smiley Happy

ajkr195
Customer

Re: Add custom metadata with new file upload

Jump to solution

This is the solution that worked for me. I also had the same question and issue - Using REST, to upload file alongwith metadata here:

https://stackoverflow.com/questions/64176613/alfresco-how-to-add-set-content-of-the-file-while-doing...

I was trying to get it done using Spring RestTemplate but couldne make it. So I reached Alfresco support. And those guyz are awesome. They gave me the solution that worked flawlessly.

Here is the solution:

private static void postFileAndMetadataToAlfresco() throws IOException {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(POST_URL);
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials ("admin","adminpswd");
        httpPost.addHeader (new BasicScheme().authenticate(creds,httpPost, null));

        File payload = new File ("/path/to/my/file.pdf");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // Entity builder

        builder.addPart("filedata", new FileBody(payload)); // this is where I was struggling
        builder.addTextBody ("name", "thenamegoeshere");
        builder.addTextBody ("foo", "foo");
        builder.addTextBody ("bar", "bar");
        builder.addTextBody ("description", "descriptiongoeshere");

        builder.addTextBody ("overwrite", "true");

        HttpEntity entity = builder.build();

        httpPost.setHeader("Accept","application/json");
        httpPost.setEntity(entity);

        CloseableHttpResponse response = client.execute(httpPost); // Post the request and get response

        System.out.println(response.toString()); // Response print to console

        client.close();  // close the client
}

 Hoping it might help someone like me.