3

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?

Dikesh Gandhi
  • 6,803
  • 4
  • 30
  • 55
Hitesh Chandegara
  • 2,638
  • 6
  • 29
  • 49
  • 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 07 '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 08 '13 at 04:36

3 Answers3

1

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.

HTH

David Lozzi
  • 7,372
  • 5
  • 29
  • 50
1

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:

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


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

            DirectoryInfo FolderName = new DirectoryInfo(Paths);
            try
            {
                FolderName.Create();
            }
            catch { }

            string fileName = Paths + @"\" + "MyXML.xml";
            xdoc.Save(fileName);

        });

its working on my local

Hope it helps!!!

Jignesh Rajput
  • 1,529
  • 7
  • 20
  • 39
0
  • 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)

Ricardo Gomes
  • 233
  • 3
  • 12