What is the best way to copy a file's column data in SharePoint?

Jeroen Ritmeijer - Muhimbi
  • What is the best way to copy a file's column data in SharePoint? Jeroen Ritmeijer - Muhimbi

    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);
        // ** 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

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

  • 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

Related questions and answers
  • Symptoms: You develop a browser-compatible InfoPath form with managed code for business logic. You use ContactSelector ActiveX control to look up and resolve users from the SharePoint site Users lists (UserInfo table in the Content database). You then validate list of resolved users and copy some of them to another node in the primary data source. The field displays copied contacts... the Contact Selector Control I can add that it is rendered in the browser (Firefox) as HTML table, div and span elements handling events with functions declared in Core.js for all ContactPicker controls

  • to preserve the Created and Created By column values just fine. Turns out that the last hurdle is unlinking the copy from its source. I can't figure out a way to empty out the _CopySource column. No combination of setting things like ReadOnly="False" on the column, etc. seems to work. This is one capability that I would expect to be part of the Copy Web Service, as it's exposed in the UI... performance as well as to reduce the likelihood of filename repetition causing destruction by overwrite. This is a "no managed code" environment (don't ask), so I'm trying to use SharePoint's Lists and Copy

  • (I have also asked this question on Stack Overflow) I have a SharePoint feature I'm using to add some custom aspx files to the Pages Library. When I activate the feature, I can visit the pages in-browser and they are visible in SPDesigner, but when I "View All Site Content" they are not there. Why is this the case? The Feature's Elements File: <?xml version="1.0" encoding="utf-8"?> <.../Pages/Example.aspx When I "View All Site Content" I am looking at the contents of the "Pages" list: http://myserver:PORT/subsite/Pages/Forms/AllItems.aspx

  • I have a webpart i build using WSpBuilder and it works OK. When i use Deploy from Visual Studio it installs the WSP sharepoint and add the webpart to the webpart gallery. But it seems the feature does activate the webpart but i also want to remove it when deactivating it.(So i know i need to add something to the FeatureDeactivating So i created a Feature With Reciever template and added... deactivate it it doesn't remove it but i get error: Failed to instantiate file "myStoreComms.webpart" from module "WebPartPopulation": Source path "myStoreComms.webpart" not found. Any ideas.. .what

  • Hello I really need help with this... , I am using Sharepoint 2007. I have the 'collect data from user' action in a workflow - which generates a custom form within the workflow folder. The form...:EcmaScriptEncode($ListName),'};'))}"></input></td> I need to add javascript within the DataFormWebPart so I can update another form field when the user clicks the 'Complete Task' button. I...']").click(function(){ $("input[type=checkbox]").attr("checked", true); }); This changes the form field succesfully but does not save to the list when the form submits. I have also tried to call

  • ); Update: I've built a C# console app which creates all lists / lookup columns / content types and add the lookup column to corresponding content type. Result: same thing, same magic number 13. Anyone... columns. If I put in the FieldRef the internal name for lookup column no 14 or above, it is not working anymore. Why? If I don't use LookupId='TRUE' and do this: (use the value of the lookup...So I did create on an SPWeb 20 custom lists and then a tasks list. For each of those 20 lists I've created 20 site lookup columns. I then created 20 Content Types based on Task content type

  • impersonate="true" username=”domain\someuser” password=”***” /> If we add “NTAuthority\Anonymous Logon” as a user in custom DB server instance. If we use Sql Authentication “Data Source=xxxx...We have an issue in accessing custom a DB from a SharePoint custom webpart. When we try to access the custom DB from SharePoint webpart we are getting the error "Login Failed for User NTAuthority...; The connection string we are using is "Data Source=xxxx; Initial catalog=yyy; Integrated security=true;" We have tried with a small change in conneciontion string as well "Data Source=xxxx; Initial catalog

  • us that the authentication method might matter. UPDATE 2: Another piece of info I should have included in the first place: The reason that I'm asking this is that the $().SPServices.SPGetCurrentUser in SPServices sometimes returns the previous user. All I'm doing in that function is loading the _layouts/userdisp.aspx?Force=True page and pulling the Name from the page. It's the column... in with the second user does work. This seems like a pretty big problem and I've seen it on and off over the years, mostly while testing with different identities. UPDATE 1: I'm seeing it in several hosted

  • .” I’m guessing this has something to do with customized DispForm having some code in it that prevents it from pulling in data from List B. I’m using WSS 3.0 and SharePoint Designer 2007. Any help... Dispform works perfectly with List A. I’ve copied the customized DispForm to the folder for List B (List A and B were created from the same template) and then connected the customized DispForm through the Supporting Files tab on the List Properties dialogue box for List B. However, when I try to display the data in List B, I get the following error: “Unable to display this Web Part

Data information