Click here to Skip to main content
15,885,216 members
Articles / Productivity Apps and Services / Sharepoint

SharePoint - Copy Files / Documents from Search screen using JavaScript

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Feb 2014CPOL3 min read 11.9K   6
CodeProjectOne of the major enhancement of SharePoint 2010 is "Content Organizing" which makes it more strong in "Document Management System (DMS)" race.Content Organizing means, the content automatically routed to different destinations based up on various factors.Best part of it, content is o
One of the major enhancement of SharePoint 2010 is "Content Organizing" which makes it more strong in "Document Management System (DMS)" race.

Content Organizing means, the content automatically routed to different destinations based up on various factors.

Best part of it, content is organized without any manual intervention.

Worst part of it, Data / Docs are scattered across site collection or whole web application.

Lets consider a scenario, where the user uploaded 20 documents which were organized into multiple libraries in a site collection. Now i want to bring all that 20 docs to one single library where the user can edit them and resubmit.

Let me show you what i am aiming for.


First Question "Why Search?"
As we discussed content organizer will route the documents in all directions which will change in future, so we cannot rely on "SPSiteDataQuery". SO search will fetch the docs where ever they are across farm.

Now second Question "Why OOTB stuff, we can design a beautiful Custom page with Search API?"
Yeah its not that you cannot, but you cannot imagine how painful it will be while migrating to next version of SharePoint with all the customization.

We are doing all of it using JavaScript, as it will be easy to migrate to SharePoint 2013 apps with no server code.

# Create a site collection which is search enabled and the content of it is searchable.

# Create a webpart page and add below webparts in respective order, so that they appear in reverse order on screen.
         1. Search Core Results webpart.
         2. Search Box webpart.
         3. Content Editor webpart.

# Edit "Search Box" webpart and go to "Miscellaneous" section of its properties and ensure that Target search results page pointing to self. Else the search action will take user to default search results page.

 # Edit "Search Core Results" webpart, go to Display Properties section of its properties and  un-check "Use Location Visualization", now XSL Editor button will be enabled.

# Click "XSL Editor" button and start editing XSLT of the webpart. Search for "srch-Icon", and add below piece of markup.

<!-- THIS IS WHERE WE MODIFIED TO EMBEDED CHECKBOX-->
<input type="checkbox" id="{concat($currentId,'_Checkbox')}" align="absmiddle" class="documentcheckbox"></input><label style="display:none"><xsl:value-of  select="$url"/></label>

This will add a visible check-box and an invisible url of the document. 

# Consider a standard place to keep your Script files. I choose "Style Library", and create a folder "JSApps" and then add below mentioned files.
             1. Jquery.min.js
             2. Script file in txt format, used to load in Content Editor webpart.
Don't worry about the content of the SPScript.txt file now.

# Edit "Content Editor" webpart and go to Content Link section in properties and give url for the SPScript.txt file. Apply the changes and whatever you put in that SPScript.txt file will start effecting the search page.

# Now add below content tp SPScript file and the page will turn just like how it appears on the very first snapshot provided.
<html><head><br /><script src="/sites/TestSC/Style Library/JSApps/jquery-1.10.1.min.js"></script><br /></head><br /><br /><body><br />Enter Universal processing Id:<input type="text" id="UniversalProcessID" class="txtprocessid"/><br /><button type="button" class="btnprocess">Copy to Destination Library</button><br /></body><br /></html><br /><script type="text/javascript"><br />var foo = ExecuteOrDelayUntilScriptLoaded(useClientContext, "sp.js");<br />var docarray="";<br /><br />function useClientContext() {<br /><br /> this.ctx = new SP.ClientContext.get_current(); <br /> this.web = this.ctx.get_web(); <br /> this.ctx.load(this.web);<br /> this.ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));<br />}<br /><br />function CopyFiles()<br />{ <br /> var filearray=docarray.split('$');<br /> var ctx, file, notifyId;<br /> <br />    ctx = new SP.ClientContext.get_current();  <br />  <br /> ctx.executeQueryAsync(<br />  function (sender, args) {<br />   // Set a notification to the user that we are going to copy the file.<br />   notifyId = SP.UI.Notify.addNotification('Copying files...', true);<br />   for(i=0;i<(filearray.length)-1;i++)<br />   {  <br />    debugger;<br />    file = ctx.get_web().getFileByServerRelativeUrl(filearray[i]);<br />    ctx.load(file);<br />    var fileparts= filearray[i].split('/');<br />    var filename= fileparts[(fileparts.length)-1]<br />    <br />    // File loaded. Now we want to copy the file. We'll use a nested AJAX call<br />    file.copyTo("/sites/TestSC/TestDestLib/"+filename, true);<br />   }<br />    ctx.executeQueryAsync(<br />     function (sender, args) {<br />      <br />      // File copied successfully!<br />      SP.UI.Notify.removeNotification(notifyId);<br />      // Let the user know that the operation was successful<br />      SP.UI.Notify.addNotification('Files copied successfully', false);<br />     },<br />     function (sender, args) {<br />      <br />      // Unable to copy file.<br />      SP.UI.Notify.removeNotification(notifyId);<br /><br />      showError(args.get_message());<br />     });<br />   }<br />  ,<br />  function (sender, args) {<br />   <br />   // Unable to locate file.<br />   SP.UI.Notify.removeNotification(notifyId);<br /><br />   showError(args.get_message());<br />  });<br /> <br />}<br /><br />function onQuerySucceeded() {<br /> //alert('Title: ' + this.web.get_title());<br />}<br /> <br />function onQueryFailed(sender, args) {<br /> alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());<br />}<br />$(document).ready(function(){<br /> <br /> $(".documentcheckbox").change(function(){<br />   var url=$($(this)[0]).next()[0].firstChild.data.replace(/^(?:\/\/|[^\/]+)*\//, "");<br />   if($(this)[0].checked)<br />   {<br />    docarray=docarray+"/"+url+"$"; <br />   }<br />   else<br />   {<br />    docarray=docarray.replace("/"+url+"$","");<br />   }<br />   alert(docarray);<br />        });<br />  <br /> $(".btnprocess").click(function(){<br />  CopyFiles();<br /> });<br />});<br /><br /></script><br /><br />
To make it sweet and short, all i did is,
    1. When a User selects a file, i keep track of the relative url of the selected file.
    2. When a user un-check a file, it will remove the pertaining url from the track.
    3. Once the user selects all the files to be copied, and when he clicks the "Copy To Destination Library", using JavaScript (ECMA), the selected files will be copied to destination library.
   4. User will be notified while copying the files and after successful copying.
   5. If the content type of Source and destination libraries match, it will even copy the metadata.

The only piece of code here is that SPScript File which you can download from this link. Search for "SPScript.zip" file.

Note: This piece of code works only to copy files with-in the Site collection. If you intend to copy across different site collections, well that's my next article.
This article was originally posted at http://pratapreddypilaka.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionCross Site Collection part? Pin
Member 1116118520-Oct-14 13:59
Member 1116118520-Oct-14 13:59 
GeneralGood job mate Pin
Steven Oberholzer11-May-14 19:37
professionalSteven Oberholzer11-May-14 19:37 
QuestionCopy to other Website Pin
FlipperDo@live.de10-Apr-14 23:10
FlipperDo@live.de10-Apr-14 23:10 
AnswerRe: Copy to other Website Pin
PratapReddyP14-Apr-14 12:35
PratapReddyP14-Apr-14 12:35 
QuestionUniversal processing ID? Pin
David Erb26-Feb-14 6:34
David Erb26-Feb-14 6:34 
What exactly is the universal processing ID?
AnswerRe: Universal processing ID? Pin
PratapReddyP26-Feb-14 9:21
PratapReddyP26-Feb-14 9:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.