2

I recently authored a C# based script for a blog posting, which copies column data from one file to another. While testing this solution I noticed that copying this data works differently depending on the file format, which is painful to say the least.

For 'non Office 2007' files I can just assign the source file's Properties variable to the destination file. For Office 2007 files I need to write the destination file, iterate through all fields of the source file and then update the destination file again.

If I treat all documents the same then either meta data is not copied over, depending on the file type, or certain column types are not copied over (e.g. DateTime columns)

The code is as follows:

if (sourceFile.Name.EndsWith("x", StringComparison.InvariantCultureIgnoreCase) == false)
{
    // ** Pre office 2007 formats (without the trailing x) are treated differently 
    // ** from the office 2007 formats when it comes to copying meta data.
    Hashtable metadata = sourceFile.Item.Properties;
    // ** Add the file to the site including the meta data
    spDestinationFile = destinationWeb.Files.Add(destinationFilePath, convertedFile, 
                                                 metadata, true);
}
else
{
    // ** Add the file to the site.
    spDestinationFile = destinationWeb.Files.Add(destinationFilePath, 
                                                 convertedFile, null, true);
    // ** Copy the Actual meta data to the newly created file.
    foreach (SPField field in sourceFile.Item.Fields)
    {
        if (field.ReadOnlyField == false)
        {
            spDestinationFile.Item[field.Id] = sourceFile.Item[field.Id];
        }
    }
    // ** Update the meta data
    spDestinationFile.Item.Update();
}

Note that this may be related to my previous post named 'When is SPFile.Properties != to SPFile.Item.Properties in SharePoint?'

2
  • So what happens exactly if you just use the code in the else branch? Commented Jan 15, 2010 at 17:37
  • When just running the code in the else branch it doesn't work either. I tried it naturally, but cannmot remember if it resulted in an Exception or data not being copied over properly. Commented Feb 10, 2010 at 10:51

2 Answers 2

1

I havent run into the problem you are describing, but i got a few comments to the logic:

It seems a bit risky to just check for X at the end? (also whats with the "== false"? whats wrong with !expression ;-)

What happens if you get a non-Office 2007 extension that ends on X? Example XMX is a type of AutoCAD file. What happens when you get an Office 2007 extension that doesnt end on X? Example XLSM (macro enabled XSLX workbook).

You should probably make a whitelist to check up against containing all Office 2007 extensions.

hth Anders Rask

3
  • 1
    At my work the coding guidelines have us use false == instead of ! because when scanning code, the false == stands out more than the ! thus it's more readable. Commented Jan 16, 2010 at 3:12
  • Checking for the 'x' at the end is indeed not the cleanest way to do it, but it makes this example a lot easier to read as that part of the code is not relevant to the issue at hand. The rest is personal preference, I personally despise (yep, that bad) the use of 'false ==', that is coding guidelines gone mad ;-) Commented Jan 16, 2010 at 8:37
  • == false is correct, and bangers are !good. Commented Aug 8, 2016 at 21:12
1

I am answering my own question here, but the code fragment in the question appears to be the best way to deal with copying meta data. A variant of this has been part of one of our products for 2 years and we are no longer receiving support calls about it since we changed to this logic.

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.