Recently I faced some weird behavior from share point with regards to one of team members code. He was using ECMA for getting the document details after it was copied from the template folder of the same library to documents folder.
Copying process was done smoothly and the code copies the all the content of the template document to newly created document. He has written some code to retrieve the document details after the document is copied. Document retrieval method is written in the copied success method. But some how dont no the method is not getting executed some time and it does retrieve the items some times and fails some time. I used try catch but still it also not catching any thing over there.
After lot of search and thinking I come to conclusion that the asynchronous call might be creating the problem. I have written the document details retrieval code in the copy success method and when copy process was in progress the next retrieval call might be going to library for retrieval so its not getting any thing or it might get in to the loop over there.
I don't no the actual bug over there but I come up with another alternative and I think this will definitely save lot time of the developers. I have redirected the same page with document name query string value ( query string here because we are using ECMA and dont have any other state to hold file name, you can use states or variable while working with SL or Manged code) and when the page have query string value of the copied file name then it will use the retrieval code to get he document details from the library and play around it for any operations.
Below is code snippet for coping the document from template and retrieving its details using above mentioned BL in ECMA
Code to copy the document
function CopyFile() {
try
{
var context = new SP.ClientContext.get_current();
this.oWebsite = context.get_web();
var lists = oWebsite.get_lists();
var list = lists.getByTitle('ResearchAgreement');
var Folder = oWebsite.getFolderByServerRelativeUrl('/ResearchAgreement/Templates');
Files = Folder.get_files();
context.load(oWebsite);
context.load(Files);
context.executeQueryAsync(Function.createDelegate(this, this.ExecuteCopyOnSuccess), Function.createDelegate(this, this.ExecuteCopyOnFailure));
}
catch(e)
{
alert(e);
}
}
function ExecuteCopyOnSuccess(sender, args) {
var agname = document.getElementById('txtAgreementName');
debugger;
var file = Files.itemAt(0);
var filename = file.get_name();
alert(filename);
if (filename != null) {
var agname = document.getElementById('txtAgreementName');
var newUrl = '/ResearchAgreement/'+agname.value+'.docx';
file.copyTo(newUrl, true);
fname = agname.value + '.docx';
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededFileCopy), Function.createDelegate(this, this.onQueryFailedFileCopy));
}
}
function ExecuteCopyOnFailure(sender, args) {
alert(args.get_message());
}
function onQuerySucceededFileCopy(sender, args) {
location.href='SITEVarible?filename='+fname;
}
function onQueryFailedFileCopy(sender, args) {
alert(args.get_message());
}
Retrieving Details in Page load
function GetDocDetails()
{
try
{
var context = new SP.ClientContext.get_current();
var website = context.get_web();
var oList = context.get_web().get_lists().getByTitle('ResearchDocuments');
var camlQuery = new SP.CamlQuery();
var pQuery = ''+
'' +
'' +
'' +
'' +
'' + qval + ' ' +
' ' +
' ' +
' ' +
' ';
//var pQuery = '
';Copying process was done smoothly and the code copies the all the content of the template document to newly created document. He has written some code to retrieve the document details after the document is copied. Document retrieval method is written in the copied success method. But some how dont no the method is not getting executed some time and it does retrieve the items some times and fails some time. I used try catch but still it also not catching any thing over there.
After lot of search and thinking I come to conclusion that the asynchronous call might be creating the problem. I have written the document details retrieval code in the copy success method and when copy process was in progress the next retrieval call might be going to library for retrieval so its not getting any thing or it might get in to the loop over there.
I don't no the actual bug over there but I come up with another alternative and I think this will definitely save lot time of the developers. I have redirected the same page with document name query string value ( query string here because we are using ECMA and dont have any other state to hold file name, you can use states or variable while working with SL or Manged code) and when the page have query string value of the copied file name then it will use the retrieval code to get he document details from the library and play around it for any operations.
Below is code snippet for coping the document from template and retrieving its details using above mentioned BL in ECMA
Code to copy the document
function CopyFile() {
try
{
var context = new SP.ClientContext.get_current();
this.oWebsite = context.get_web();
var lists = oWebsite.get_lists();
var list = lists.getByTitle('ResearchAgreement');
var Folder = oWebsite.getFolderByServerRelativeUrl('/ResearchAgreement/Templates');
Files = Folder.get_files();
context.load(oWebsite);
context.load(Files);
context.executeQueryAsync(Function.createDelegate(this, this.ExecuteCopyOnSuccess), Function.createDelegate(this, this.ExecuteCopyOnFailure));
}
catch(e)
{
alert(e);
}
}
function ExecuteCopyOnSuccess(sender, args) {
var agname = document.getElementById('txtAgreementName');
debugger;
var file = Files.itemAt(0);
var filename = file.get_name();
alert(filename);
if (filename != null) {
var agname = document.getElementById('txtAgreementName');
var newUrl = '/ResearchAgreement/'+agname.value+'.docx';
file.copyTo(newUrl, true);
fname = agname.value + '.docx';
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededFileCopy), Function.createDelegate(this, this.onQueryFailedFileCopy));
}
}
function ExecuteCopyOnFailure(sender, args) {
alert(args.get_message());
}
function onQuerySucceededFileCopy(sender, args) {
location.href='SITEVarible?filename='+fname;
}
function onQueryFailedFileCopy(sender, args) {
alert(args.get_message());
}
Retrieving Details in Page load
function GetDocDetails()
{
try
{
var context = new SP.ClientContext.get_current();
var website = context.get_web();
var oList = context.get_web().get_lists().getByTitle('ResearchDocuments');
var camlQuery = new SP.CamlQuery();
var pQuery = '
'
'
'
'
'
'
'
'
'
//var pQuery = '
camlQuery.set_viewXml(pQuery);
this.collListItem = oList.getItems(camlQuery);
alert(pQuery );
//context.load(oList);
context.load(collListItem);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededgetfilenameid), Function.createDelegate(this, this.onQueryFailedgetfilenameid));
}
}
function onQuerySucceededgetfilenameid(sender, args) {
if (collListItem != null) {
var ListEnumerator1 = collListItem.getEnumerator();
while (ListEnumerator1.moveNext()) {
var currentItem = ListEnumerator1.get_current();
if(currentItem.get_item('ID')!=null)
{
valfileid = currentItem.get_item('ID');
}
}
}
}
function onQueryFailedgetfilenameid(sender, args) {
alert(args.get_message());
}
No comments:
Post a Comment