I need to extract InfoPath form fields in C# using Client Side Object Model(CSOM) and convert it into XML file.

The code which I tried to get FormLibrary fields is

   // Starting with ClientContext, the constructor requires a URL to the 
        // server running SharePoint. 
        ClientContext context = new ClientContext("http://SiteURL/");

        // Assume the web has a list named "Announcements". 
        List announcementsList = context.Web.Lists.GetByTitle("MyFormLibrary");

        // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
        // so that it grabs all list items, regardless of the folder they are in. 
        CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
        ListItemCollection items = announcementsList.GetItems(query);

        // Retrieve all items in the ListItemCollection from List.GetItems(Query). 
        foreach (ListItem listItem in items)
            // We have all the list item data. For example, Title. 
            Console.WriteLine( listItem["Title"]);

I am getting Title field as name of xml file but when I debug it, giving me error in StackTrace as

at Microsoft.SharePoint.Client.ClientObject.CheckUninitializedProperty(String propName) at Microsoft.SharePoint.Client.File.get_Name()

can anyone please help me !!! Any help is appreciated.

1 Answer 1


I tried your code at my end and I found something useful for you.

Please replace your foreach loop with this code and try again.

 foreach (ListItem listItem in items)
                // We have all the list item data. For example, Title. 
                Console.WriteLine("Item : {0}, Id : {1}", listItem.FieldValues["Title"], listItem.Id, listItem.FieldValues["YourField"]);

                var fileRef = listItem.FieldValues["FileRef"];
                FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef.ToString());

                using (var memory = new System.IO.MemoryStream())
                    byte[] buffer = new byte[1024 * 64];
                    int nread = 0;
                    while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
                        memory.Write(buffer, 0, nread);
                    memory.Seek(0, System.IO.SeekOrigin.Begin);
                    XDocument doc = XDocument.Load(memory);
                //Console.WriteLine( listItem["Title"] + listItem.File.Name);


Now you will be getting XML data. Please find following snap which I got while running my code.

enter image description here

Let me know you were looking for this only.

  • 1
    hey @aakash thanks a lot it worked for me perfectly and this is all i want
    – Codec
    Jul 26, 2016 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.