Upload a newer version of document from within Alfresco repository - alfresco

I have a two documents (a.pdf and b.pdf) in the Alfresco repository. I want to apply/upload b.pdf as a new version of a.pdf. Is there any way that I can do this using Javascript or a java code?
Thank you

Create Document, Make it Versionable, Modify It
Creates a document, makes it versionable, checks it out, modifies the content of the working copy, checks it in again and then repeats the process but checks in the document with a version history note and as a major version increment:
// create file, make it versionable
var doc = userhome.createFile('checkmeout.txt');
doc.addAspect('cm:versionable');
doc.content = 'original text';
// check it out and update content on the working copy
var workingCopy = doc.checkout();
workingCopy.content = 'updated text 1';
// check it in
doc = workingCopy.checkin();
// check it out again
workingCopy = doc.checkout();
workingCopy.content = 'updated text 2';
// check it in again, but with a version history note and as major version increment
doc = workingCopy.checkin('a history note', true);
You can take reference from below javascript api link
https://hub.alfresco.com/t5/alfresco-content-services-hub/javascript-api-cookbook/ba-p/293260

There's a version service bean that can be used to apply versioning to documents (nodes) that can be used in a custom bean. Don't forget to inject the versionService in your service-context.xml btw <property name="versionService" ref="VersionService"/>
If I remember correctly you may have to enable a versionable aspect (cm:versionable) on the node in question. If this has already been enabled then there's also the ability to view the VersionHistory for the node.
Create 2nd version and copy over with the contents of node 2.

Related

alfresco - how to add a nodeRef as the new version of another node using java?

Hi I am new to alfresco so i appreciate your help.
I am trying to add a new node ref as the new version of a document while editing his properties.
My scenario
i can create a PDF file with his properties as the content,after updating his properties, I am doing this with behavior/policies NodeServicePolicies.OnUpdateNodePolicy .
example: when i edit the name I can see it inside the PDF file
But this new file needs to be the new version of the file edited, i cant figure how to fix this.
i appreciate yours answer.
thanks in advance.
https://docs.alfresco.com/4.0/concepts/versioning.html
You're looking for versioning.
Like Curtis mentioned, using API is an option, but Alfresco will do this for you automatically if you set it up that way. Also, creating a new version will not change node's nodeRef.
See the createVersion method from the VersionService API here
My fix:
i just added get the content of the new file and set as the new content of the previous node. like this
void updateVersionWithNewNodeContent(nodeA){ // nodeB is the new version of the file. NodeRef nodeB; nodeService.setProperty(nodeA, ContentModel.PROP_CONTENT,nodeService.getProperty(nodeB, ContentModel.PROP_CONTENT)); }

How to replace curdoc

How can I replace the current document in a bokeh server app?
I have a previous document saved as a json_string. If I do
set_curdoc(Document.from_json_string(json_string))
this seems to properly change curdoc(), however the new document is not displayed in the browser.
I found a workaround, other places suggest to update the children of an existing layout instead of updating the whole curdoc().
I did that but I had to expand a bit to do that from a document saved in a json string.
I had to switch the document attribute of all the models from the imported document to curdoc() instead (otherwise it complains that the models belong to another document)
assuming that the document I import and the current document both have only one root:
new_doc = Document.from_json_string(json_string)
new_grid_models = collect_models(new_doc.roots[0])
for elem in new_grid_models:
try:
elem.document = curdoc()
except AttributeError:
elem._document = curdoc()
new_children = new_doc.roots[0].children
del new_doc
grid.children = new_children
After that the python callbacks need to be re-affected to the appropriate imported models.
I put up an example app here:
save_and_load app on Bitbucket
You can just clear the current document and then add a new one:
Put the whole code to create your document (bokeh app) into a function create_curdoc(). At the very end of this function you have curdoc().add_root(layout).
Call the function create_curdoc() once to create and display the document
Clear the current document with curdoc().clear()
Call the function create_curdoc() with updated data again (or another function that creates a document) to create and display the new document.
If you dont clear before calling the function again the new document will just be added at the bottom of the old one.
You could, for example, trigger the update by a button callback inside your current document.
The button setup might look like this:
def replace():
curdoc().clear()
create_curdoc()
replace_button = Button(
label="Replace Document",
button_type= "danger")
replace_button.on_click(replace)

how to add node in through Drupal API

In order to insert an article to Drupal there are thee ways of doing that:
by admin panel - really slow and not feasable if talking about 400 articles
by pure sql - number of tables that have to maintained and calculated (content, core, cat etc.) is quite high and it's not really reliable
by using drupal API - that's something that I was trying to implement but can't find a good documentation on it. What I'm trying to achieve is to use drupal classes and insert content (ie. running a PHP file with (catid,title,introtext....))
Example: what i want to add node in xyz.com/drupal site but my php code should be run in irankmedi.com
Can you please point me into direction where I can find some info on how to manage articles and categories this way?
Thanks in advance
If your request is very specific and you can't find a module that does what you need it shouldn't be too difficult to make a module on your own and import (create drupal) content from your code. Should be something like this:
Create a php file that will do the job.
At start of your script include standard Drupal's bootstrap code so you'll have all Drupal's functionality available in your script
Make code that will read content (from database or feed or something else).
Use Drupal api to insert node programatically:
https://www.drupal.org/node/1388922
Call your script manually or set cron to call it on specific time periods.
First you have to read RSS feed in drupal custom module. Then, following code can create node programatically.
$node = new stdClass();
$node->type = 'blog'; // This type should exist
node_object_prepare($node); // Sets some defaults.
$node->language = LANGUAGE_NONE; // The language of the default
$node->title = "testnode";
$node->body[LANGUAGE_NONE][0]['value'] = "Body";
node_save($node); // function that actually saves node in database

set alfresco dateTextBox to empty

I want to set an empty value for Alfresco Aikau DateTextBox widget.
But the date is being set to "1970/1/1" automatically. How can i set a date to empty in Aikau ?
I'm trying this code in the jsonModel but it's not working:
{
name:"alfresco/forms/controls/DateTextBox",
widthPx:"90",
config:{
fieldId:"DATE_1",
additionalCssClasses:"datefield",
name:"Date1",
value:"",
requirementConfig:{
initialValue:false
}
}
}
What version of Aikau are you using? We've made a number of changes to the alfresco/forms/controls/DateTextBox widget since it was first created. If the problem still persists in the latest version (which at the time of writing is v1.0.57) then you should raise a JIRA ticket or GitHub issue. There is a new release of Aikau each week and they are all backwards compatible so you should be able to take advantage of bugs fixes as soon as they are available. All bugs are prioritized ahead of features as the intention is that there should be no technical debt on the Aikau project. See this blog post for information on how to use new Aikau releases.
I am not sure regarding why it is not working with existing alfresco/forms/controls/DojoDateTextBox widget.I did following,for making it work.
1.I have created a new widget,Placed below content in the file.(This content is copied from existing widget)
define(["dojo/_base/declare",
"alfresco/forms/controls/DateTextBox"],
function(declare, DateTextBox) {
return declare([DateTextBox], {});
});
2.Used that widget by using following.
name : "js/Demo/MyCustomWidget",//It is the path of my custom widget.
widthPx: "90",
config :
{
fieldId : "DATE_1",
additionalCssClasses: "datefield",
name : "Date1",
value : undefined,//This must be undefined,neight null nor ""
requirementConfig:
{
initialValue: false
}
}
In case of aikau control,you can find source code inside tomcat\webapps\share\WEB-INF\lib\aikau-1.0.8.1.jar.Version of aikau jar file may differ depends on your alfresco version.

push external multimedia file in to package at tridion publish time

When we publish some page/dynamic component from tridion is it possible to add some external multimedia file/content(ex:jpg image) in to current executing/rendering package at publish time.So that final transportation package has this binary file present along with original published content?
Is this achivable using customization of tridion renderer/resolver?If yes please provide some inputs.
*Note:*The binary content that needs to be pushed in to package at publish time is not present as multimedia component in tridion, it is located at other file location outside tridion CMS.Instead we have some stub multimedia component being used inside published component/page which has some dummy image. we plan to replace the stub image with original image at publish(rendering/resolving) time.
Since we have huge bulk of binary content stored in DAM tool we dont want that data to be recreated as multimedia component in tridion, insted we want to use that data by querying DAM tool and attach it in to tridion package with some logical referencesplanning to maintain one to one mapping between stub multimedia comp tcmid to original content in some mapping DB for reference).
Please let us know if any solution is there to attach external binary content to package at publish time.
The best - and easiest way - is to use the mechanism provided by Tridion out-of-the-box for this. Create a new multimedia component, select "External" in the resource type drop-down, and type the URL to the object. As long as you can address it with a URL, it will work exactly as you want (item will be added to package and sent to delivery server).
If this is not good enough for you, then yes, you can add it to the package yourself. I've done this in the past with code somewhat like this:
FileInfo file = // Weird logic to get a FileInfo object from external system
Item item = package.GetItem("My original Item");
item.SetAsStream(file.OpenRead());
This replaced the content of my original component with the actual file I wanted. This will work for you IF the original component is also a multimedia component. If it's not, just create a new item with your own name, etc. If possible, do use the out-of-the-box process instead.
PS: FileInfo Class.
As Nuno suggested the best way is to use multimedia component with 'External' resource type. You may not need to create these manually, you can automate using core services or API programs.
Another way I used before to create zip file at run time and add same to package with following code. Hope it may help.
using (MemoryStream ms = new MemoryStream())
{
zip.Save(ms);
downloadAllInOneURL = String.Format("ZipAsset{0}.zip", uniqueZipID);
downloadAllInOneURL = m_Engine.PublishingContext.RenderedItem.AddBinary(ms, downloadAllInOneURL, "", "application/zip").Url;
downloadAllInOneSize = getSize(ms.Length);
}

Resources