Friday 17 February 2012

Sharepoint - Can upgrade .doc to .docx in-place in a doc library?

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);
        }
    }
}
....


or




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.


or

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.



No comments:

Post a Comment