5

Here's the situation. I want to roll out Office Web Apps to our SharePoint instance. The only trouble is that a lot of files are still in .doc format, and if you try to edit them in Office Web Apps it has to convert them first. That's OK, conversion is good, but it makes a copy of the file and adds (converted) to the filename. This is really going to throw our users so I want to avoid it if possible.

What I'd really like is to be able to run a script, go to lunch, and come back to find that every .doc has been upgraded to a .docx. The Word Automation Service is a great candidate for this. Most of the tools I've seen so far will upgrade the document but will dump the old file in a 'converted' folder somewhere.

We have a lot of documents so I'd like to avoid manual processes as much as possible. How should I approach this?

4 Answers 4

5

It's actually possible to rename/move a file, but you must do it programmatically using client object model or server object model - File.MoveTo

Here is an ugly example (didn't test it, might be some typo):

using Microsoft.SharePoint.Client;

....
using(var ctx = new ClientContext("http://yoursharepoint/web_containing_library/"))
{
    var lib = ctx.Web.Lists.GetByTitle("Title of your library");
    var files = lib.RootFolder.Files;
    ctx.Load(files);
    ctx.ExecuteQuery();

    foreach(var file in files)
    {
        if(file.Name.ToLower().EndsWith(".doc")
        {
            string newFilename = file.Name + "x"; //dirty hack :)
            file.MoveTo(newFilename, MoveOperations.Overwrite);
            ctx.ExecuteQuery();

            //not sure if you need this, try without first
            ctx.Load(file);
            ctx.ExecuteQuery();

            //convert DOC content to DOCX
            var fi = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
            //get content using fi.Stream and convert it

            //save it
            File.SaveBinaryDirect(ctx, file.ServerRelativeUrl, streamWithDOCX, true);
        }
    }
}
....

The only problem with moving files is that if you are using versioning, the MoveTo will move previous versions too/change their extension, but the content will remain untouched.

And for conversion itself you could use Office Word Interop.

1
  • +1 since I've seen this 'in live' Jul 28, 2011 at 9:25
5

Here's one way I've been able to "convert" a DOC to DOCX, although it's all manual, is to

  1. Open the doclib in Explorer view/open in Explorer
  2. Open the file in question (check-out if needed)
  3. "Convert the file"
  4. Save the file locally
  5. Rename the file extension in Explorer view (if you have it opened in Word, SP will complain that the file is locked)
  6. Upload the saved file back into the library - either by dragging and dropping into the Explorer, through Word (save as), or via the doclib's Upload function.

Disadvantage when library is using versioning: You can't open via versioning the original .doc document. Reason is that Word expect .docx format.

1
  • This by far is the easiest method for a small amount of documents.
    – CBono
    May 29, 2015 at 14:59
3

I don't see how this would be possible as changing the file type from DOC to DOCX would create a new item...i.e. you can't change the name of a SPFile object.

Your best bet is to probably write some code or a script to convert the docs as you migrate them to a new library along with their metadata and then retire the original doclib.

5
  • 1
    I thought that may be the case. What would be idea is if it created a clone, upgraded, then renamed the original file and then stuck the new one on as a new version. Then one would be able to maintain the version history.
    – Dan
    Jul 27, 2011 at 6:29
  • I'd have to think about that one some, but I think it's the same problem. You can't rename the file in SharePoint and uploading a converted document with a DOCX extension will create a new item.
    – Rob D'Oria
    Jul 27, 2011 at 11:20
  • MyDoc.doc and MyDoc.docx are viewed as 2 different files in SharePoint's eyes even though to you and me, they might be the same file. I'd probably go with what Rob Suggests, create a new document library, update the files and push them to the new library. I'd then delete everything in the old library and put a JS redirect on it pointing to the new library should anyone have it bookmarked. Jul 27, 2011 at 12:39
  • I just had another idea...you could programatically create a document set for each existing DOC file, promote the metadata from the existing document and then add the original document (DOC) and the new converted document (DOCX) both to the document set. Technically speaking, pretty straightforward, so it would be a question of usability. You could even go as far as changing the permissions on the DOC to read only so folks don't mistakingly edit it...and also add a message to the top fo the doclib explaining what's changed.
    – Rob D'Oria
    Jul 27, 2011 at 13:23
  • You know, some kind of workflow that fires whenever Web Apps converts a file would do the trick.
    – Dan
    Jul 28, 2011 at 9:24
3

I would suggest you to build a custom tool e.g. command line, webpart, central admin custom action or something else to trigger the conversion process. The code should do the following:

  1. Find all SPListItems with targeted *.doc files
  2. Convert the DOC file to DOCX with word automation services
  3. Check if check-out is required/possible (the item could already be checked-out)
  4. Save the file with SPListItem.File.SaveBinary()
  5. Check-in/publish/approve the list item

It is totally possible to update the SPListItem.File. That will create a new version if versioning is enabled. If not, it will replace the previous file. The custom code should be robust to handle all possible versioning settings on the document libraries so that all items are checked-in, published or approved after the conversion process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.