3

I am converting word files to PDF using word automation service using event receiver ItemCheckedIn event. I have selected major-minor version settings in Versioning Settings. in The code works fine except it overwrite current file and does not change the version. below is my code sample:

public override void ItemCheckedIn(SPItemEventProperties properties) {

    //////collFiles = m1list.RootFolder.Files;
    ////SPFile sourceFile = collFiles[Convert.ToString(items[0][SPBuiltInFieldId.EncodedAbsUrl])];
    SPListItem curfileitem = properties.ListItem;

    SPFile sourceFile = properties.ListItem.File;
    SPList m1list = properties.List;
    //SPFile sourceFile = curfileitem.File;
    if (sourceFile == null) {
        return;
    }

    SPSite sitename = properties.Site;
    SPWeb webname = properties.Web;
    SPSite sitename2 = webname.Site;
    SPFolder destFolder = m1list.RootFolder;
    SPFile destFile = null;
    string sourceFileName = sourceFile.Name.Trim();
    string filepath = sourceFile.Url;
    string destFileName = sourceFileName.Substring(0, sourceFileName.LastIndexOf('.')) + ".pdf";
    //// destFile = destFolder.Files.Add(destFileName, this.ConvertWordToPDF(sourceFile, SPContext.Current.Site.UserToken), true);

    destFile = m1list.RootFolder.Files.Add(destFileName, ConvertWordToPDF(sourceFile, sitename.UserToken, sitename2), true);

    if (destFile != null) {
        SPListItem sourceitem = sourceFile.Item;
        if (destFile.CheckOutType != SPFile.SPCheckOutType.None) {
            destFile.CheckIn(string.Empty);
        }
    }
}

private byte[] ConvertWordToPDF(SPFile spfile, SPUserToken usrToken, SPSite sitefortoken) {
    byte[] result = spfile.OpenBinary();
    try {
        using(Stream read = spfile.OpenBinaryStream()) {
            using(MemoryStream write = new MemoryStream()) {
                if (Path.GetExtension(spfile.Title) == ".docx" || Path.GetExtension(spfile.Title) == ".doc" || Path.GetExtension(spfile.Name) == ".docx" || Path.GetExtension(spfile.Name) == ".doc") {
                    WordServiceApplicationProxy proxy = (WordServiceApplicationProxy)
                    SPServiceContext.GetContext(sitefortoken)
                        .GetDefaultProxy(typeof(WordServiceApplicationProxy));
                    //SyncConverter sc = new SyncConverter((WordServiceApplicationProxy)proxy);

                    SyncConverter sc = new SyncConverter(proxy);
                    //SyncConverter sc = new SyncConverter(proxy);
                    sc.UserToken = usrToken;
                    sc.Settings.UpdateFields = true;
                    sc.Settings.OutputFormat = SaveFormat.PDF;
                    //// Convert to PDF
                    ConversionItemInfo info = sc.Convert(read, write);
                    if (info.Succeeded) {
                        result = write.ToArray();
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw ex;
    }

    return result;
}

Thanks in advance.

1 Answer 1

2

Before adding file to destination folder

destFile = m1list.RootFolder.Files.Add(destFileName, ConvertWordToPDF(sourceFile, sitename.UserToken, sitename2), true);

You should first check if the file exist at destination or not. If it exist then you first checkout existing file. Then add new file and then check in and publish the file.

public bool CheckFileExistsorNot(SPFileCollection destFiles, string name) {
    bool fileExists = false;
    for (int noOfFile = 0; noOfFile < destFiles.Count; noOfFile++) {
        SPFile tempFile = destFiles[noOfFile];
        if (tempFile.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
            fileExists = true;
            break;
        }
    }

    return fileExists;
}

With this above function your code should be like below

 if (CheckFileExistsorNot(filecollection, pdfFileName)) {
    SPFile myFile = filecollection[pdffileurl];
    myFile.CheckOut();
    //adding file code
 } else {
    //adding file code
 }

Hope this will helps !!

5
  • Just one issue. It makes 8-10 minor version. Do you know what can be the issue? Commented Dec 22, 2015 at 8:41
  • Can you please elaborate it ? Commented Dec 22, 2015 at 8:50
  • I have PDF which has one minor version. Now when I recreate PDF and view version history, it displays 8 minor version in history. Commented Dec 22, 2015 at 8:56
  • Please check your version setting on library. Just as note , you need to publish the file to have it as a Major version. Edited checkin version will be taken as minor version. To publish after checking in use below code pdfItem.File.CheckIn("Check In comment"); pdfItem.File.Publish("publishing comment"); Commented Dec 22, 2015 at 9:01
  • 1
    Thanks for your comment. I got issue. The issue was when I checked in PDF programmatically, it again call ItemCheckedIn. Commented Dec 22, 2015 at 9:57

Your Answer

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

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