SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to create and write XML file in below URL using visual studio(programmatically).

web.Url + "/_layouts/MyPoject/Generated.xml"

enter image description here

I want to generate XML programatically inside MyProject Folder. Once it generate, how to write into that XML?

share|improve this question
are you asking how to write to XML file located in the Layouts folder via C#? Or just add an XML file to your project to be deployed with the solution? – David Lozzi Jan 7 '13 at 23:56
yes i'm asking for write into xml file also dynamically create xml file into layout folder. not just copy and paste xml into laylout folder. – Hitesh Chandegara Jan 8 '13 at 4:36

I don't believe this is a supported model, also I wouldn't recommend it.

The layouts folder should be as 'static' as possible. A few cons for using the layouts folder:

  • Most farms are not backing up their layouts folder periodically, instead they're backing up their content databases.
  • I would not install a solution which deployed living data to the layouts folder, no matter how small, as a bug or rogue code could cause that file to grow and then crash the server.
  • Files in the layouts folder are managed by SharePoint and it's feature/solution management. If you add/modify a file after the solution is deployed, your changes won't reflect to all web front ends in the farm, just the one server your code happened to run on.

Instead of using the layouts folder, save your XML to a document library, or save the XML in a property bag within the farm somewhere. This way, it's centrally available and is being backed up as part of the farm's existent management solution.


share|improve this answer
Thanks david.. let me try your suggestion. – Hitesh Chandegara Jan 8 '13 at 14:07

i think first find it out Layouts Directory file using SPUtility.GetGenericSetupPath

  string templatePath = SPUtility.GetGenericSetupPath("TEMPLATE");
  string Paths = Path.Combine(templatePath, @"LAYOUTS\MyProject");

and than create xml or update your xml.

like this:

            string templatePath = SPUtility.GetGenericSetupPath("TEMPLATE");
            string Paths = Path.Combine(templatePath, @"LAYOUTS\MyProject");

            XmlDocument doc = new XmlDocument();
            XmlNode ServiceRequestNode = doc.CreateElement("XMLNode");
            XmlNode RequestorName = doc.CreateElement("XMLChildNode");
            RequestorName.InnerText = "this is testing";
            byte[] bytes = Encoding.UTF8.GetBytes(doc.OuterXml);
            MemoryStream ms = new MemoryStream(bytes);
            XmlDocument xdoc = new XmlDocument();

            DirectoryInfo FolderName = new DirectoryInfo(Paths);
            catch { }

            string fileName = Paths + @"\" + "MyXML.xml";


its working on my local

Hope it helps!!!

share|improve this answer
  • Create a SP solution Project
  • Add SP Layout folder to project (context menu on project)
  • Create the xml file there
  • Deploy the solution

By default the "MyProject" portion of the url is based on the solution name (project name)

share|improve this answer
i want Programatically create xml file in layout folder. not just put xml file in layout folder – Hitesh Chandegara Jan 8 '13 at 4:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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