locked
Document metadata using spfile RRS feed

  • Question

  • Hi

    i want to add a metadata property for all the documents uploaded in a document library, so that if any user downloads the document, by looking at the document properties  he will be sure that document is uploaded in portal. The documents can be word,excel,pdf etc..

    i tried using the following method  

    file.AddProperty("Test Property", "Vinay"); But this is not working..Please let me know if there is anyway i can Add/modify Metadata of a document.

    Regards,

    Vinay

    Friday, October 5, 2012 8:49 AM

Answers

  • You should use the SetProperty method of the SPFile class:

    public static void UpdateFileMetadata()
    {
                using (SPSite site = new SPSite("http://basesmc2008"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFile file = web.GetFile("http://basesmc2008/tester/refinement.xml");
                        file.SetProperty("mykey","great");
                        file.Update();
                    }
                }
    }


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    • Proposed as answer by Hemendra Agrawal Saturday, October 6, 2012 5:09 AM
    • Marked as answer by Lhan Han Monday, October 15, 2012 2:46 AM
    Saturday, October 6, 2012 3:10 AM

All replies

  • You should use the SetProperty method of the SPFile class:

    public static void UpdateFileMetadata()
    {
                using (SPSite site = new SPSite("http://basesmc2008"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFile file = web.GetFile("http://basesmc2008/tester/refinement.xml");
                        file.SetProperty("mykey","great");
                        file.Update();
                    }
                }
    }


    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    • Proposed as answer by Hemendra Agrawal Saturday, October 6, 2012 5:09 AM
    • Marked as answer by Lhan Han Monday, October 15, 2012 2:46 AM
    Saturday, October 6, 2012 3:10 AM
  • Hi Steve

    I tried the above procedure, but i still don't see any additional properties to the document. Will the above method applies to all type of documents?

    Regards,

    vinay

    Wednesday, October 17, 2012 3:04 AM
  • This works in my environment. Are you doing this in an event handler? If you are trying to set properties that are included in the built-in properties of a Microsoft Office file these may get over written when the file is updated.

    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    Wednesday, October 17, 2012 3:35 AM