Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to create and write xml file in below url using visual studio(Programatically).

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

Thanks

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

3 Answers 3

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

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:

   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!!!

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

 
discard

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.