in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Eric Kraus SharePoint Blog

February 2007 - Posts

  • SharePoint Object Model - Modify List Item Document via Code

    In this demo, I will connect to a SharePoint site and iterate through a document library. In the document library, I will download an XML file, modify it, and upload it back to the document library.  

    First things first...big lesson learned:

    ** Do your development on a server with Windows Server 2003 installed (a Virtual Machine would be a cost-effective alternative) **

    1.  Create an XML file and upload it to a document library.

    <?xml version="1.0" encoding="UTF-16"?>
    <Platform>WSS</Platform>

    2. In out code, create a new SPSite object using the URL to your Site where the document library you just created exists.

    SPSite site = new SPSite("http://mossdemo.itegos.com");
    SPWeb web = site.OpenWeb("/");


    Set the following property of the SPWeb object to allow the object model to make updates to items in the list

    web.AllowUnsafeUpdates = true;

    While this step is not required, you can create a CAML (Collaborative Application Markup Language) query to return a specified result set from your list or library.  In this example, I am

    SPQuery qry = new SPQuery();
    qry.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy>";


    Using the web object we created at the start, create a SPListItemCollection to hold a collection of list items (documents or other).  Then, get a reference to the first list item in the list and hold it in a SPListItem object.


    //use the CAML query above to return a collection of list items from the first list in the web
    SPListItemCollection items = web.Lists[0].GetItems(qry); 

    SPListItem item = items[0];  //get the first item in the list
     

    Now, read the File object of the list item into a byte array so that it can be loaded into a stream object

    byte[] file = item.File.OpenBinary();
    MemoryStream ms = new MemoryStream(file);


    Next, create a new XML document and load it with the stream your just defined

    XmlDocument x = new XmlDocument();
    x.Load(ms);


    Using the XMLDocument object we just created modify

    x.GetElementsByTagName("Platform").Item[0].InnerText = "MOSS";Save the XML Document to a new memory stream

    MemoryStream msNew = new MemoryStream();
    x.Save(msNew);


    Get a reference to the folder where the new document will be uploaded.

    SPFolder fldr = web.GetFolder("Investing");  //another way to get a reference to the list...this calls a list by name

    SPFileCollection files = fldr.Files;


    Add the file with the same URL and update it to reflect your changes.

    files.Add(files[0].Url, msNew, false);
    files[0].Update();

    Don't forget to close out your memory stream objects.

    msNew.Close();ms.Close(); 


    Form Library Approach

    If you are working solely with XML documents, I would recommend using a Form Library instead of a custom list or document library.  With a few minor tweaks, you can set up the form library to promote XML nodes to list item columns.  Then, when changes are made to the XML document OR the column value, the two values will be synchronized automatically. 

    See blog:  Promote XML Nodes to Columns in Form Library

    List items (XML documents) can also be queried upon using a CAML query and the promoted fields/values:

    <Eq><FieldRef Name="Platform" Type="Text"><Value>WSS</Value></Eq>

    To make modify an XML document after promoting the fields, follow the same steps as above, but instead of retieving the File property of the item, simply update the property:
     

    SPListItem item = items[0];
    item.Properties["Platform"] = "MOSS";
    item.Update();


    That's it!
     

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts