none
Programatically updating Title filed of uploaded document in DocumentLibrary

    Question

  • Hi,

    I am adding a document to document library programatically and updating its metabase (Title).
    I had problem in updating Title field for .xml files, rest all files I am able to update the Title field with no issues.

    The code snippet is below:

    SPFile

     

    file = myWeb.Files.Add(url, content, true);
    myWeb.Update();

    // Update item properties

     

    SPListItem destItem = file.Item;
    destItem.File.CheckOut();

    destItem[
    "Title"] = "Sample File";
    destItem.UpdateOverwriteVersion();
    destItem.File.CheckIn(string.Empty, SPCheckinType.MinorCheckIn);

    For the .xml files, the Title is taken as name of the file i.e. if I upload a file by name "MyFile1.xml", the Title for this file is updated as "MyFile1.xml" eventhough I am updating the title as "Sample File".

    How can I update the Title for .xml files?

    Thanks,
    Sujanakar

    Thursday, July 23, 2009 5:13 AM

Answers

  • It appears to be a bug in the property promotion parsing of xml files.  You can use the following workaround which will not produce multiple versions. This will turn off any property promotion temporarily and allow you to set the title column. If you are expecting any content in the xml file to be promoted to SharePoint columns then this is not the solution.

     web.ParserEnabled = false;
     web.Update();
                            
    SPFile destFile = web.Files.Add("http://basesmcdev2/sites/tester1/shared documents/klquery.xml", fs, true);
                            
    SPListItem destItem = destFile.Item;
    destItem.File.CheckOut();
    destItem["Title"] = "Sample";
    destItem.UpdateOverwriteVersion();
    destItem.File.CheckIn(string.Empty, SPCheckinType.MinorCheckIn);
    
    web.ParserEnabled = true;
    web.Update();

    certdev.com
    Friday, July 24, 2009 2:34 AM

All replies

  • Hi,

    It looks like a SharePoint bug to me. I have figured out a work-around. Change your code to:

    string newTitle = "Sample File"
    SPListItem destItem = file.Item;
    destItem.File.CheckOut();
    destItem["Title"] = newTitle;
    destItem.UpdateOverwriteVersion();
    
    // Work-around start
    destItem["Title"] = newTitle;
    destItem.Update();
    // Workaround end
    
    destItem.File.CheckIn(string.Empty, SPCheckinType.MinorCheckIn);



    Works for me, I hope it works for you too. Let me know.
    Regards,
    Hristo Yankov

    My blog - http://blog.myitechnology.com

    (Was my post helpful? If so, please indicate)
    Thursday, July 23, 2009 3:57 PM
  • It appears to be a bug in the property promotion parsing of xml files.  You can use the following workaround which will not produce multiple versions. This will turn off any property promotion temporarily and allow you to set the title column. If you are expecting any content in the xml file to be promoted to SharePoint columns then this is not the solution.

     web.ParserEnabled = false;
     web.Update();
                            
    SPFile destFile = web.Files.Add("http://basesmcdev2/sites/tester1/shared documents/klquery.xml", fs, true);
                            
    SPListItem destItem = destFile.Item;
    destItem.File.CheckOut();
    destItem["Title"] = "Sample";
    destItem.UpdateOverwriteVersion();
    destItem.File.CheckIn(string.Empty, SPCheckinType.MinorCheckIn);
    
    web.ParserEnabled = true;
    web.Update();

    certdev.com
    Friday, July 24, 2009 2:34 AM