Accessing a file after creating it

397 views Asked by At

I have the following code that creates an XML file in memory, updates it and then adds it to a form library.

// Creates a new XML document in memory
XmlDocument newCRF = new XmlDocument();

// Loads an existing XML file into that in-memory document
newXMLdoc.Load("Template.xml");

// Write the XML file to a document library
using (SPSite newSite = new SPSite("http://sharepoint/newsite"))
{
    using (SPWeb newWeb = newSite.OpenWeb())
    {
        SPList newLibrary = newWeb.Lists["Test Library"];
        byte[] xmlData = System.Text.Encoding.UTF8.GetBytes(newXMLdoc.OuterXml);

        // Save file to form library
        using (MemoryStream ms = new MemoryStream(xmlData))
        {
            SPFile newFileInLibrary = newLibrary.RootFolder.Files.Add("Filename.xml", ms);
        }
    }
}

How can I access the 'newFileInLibrary' object so that I can make changes to it's properties (such as "Created By")?

1

There are 1 answers

2
Steve B On

You can get the SPListItem object by using this code :

    // Save file to form library
    SPFile newFileInLibrary = null;
    using (MemoryStream ms = new MemoryStream(xmlData))
    {
        newFileInLibrary = newLibrary.RootFolder.Files.Add("Filename.xml", ms);
    }
    SPListItem fileItem = newFileInLibrary.Item;
    DoSomethingWith(fileItem["Created"]);

Ps: did you considered posting on http://sharepoint.stackexchange.com ?