lunes, 15 de julio de 2013

SharePoint 2010 - Update xml in a SharePoint forms Library programmatically.

It's posible programatically update a xml form in a SharePoint form library, the next post shows the way.




Scenary

In a form library, you need update the field "Email"  value  in all forms at the same time. In this case, the possible solution  open one by one the forms, but this is slow, now the best way is programming.


1. First, locate the form library.



2. Find the field who need update




3. Create a visual webpart  to place the code. Insert one textbx and a Button. See the next image.


4. Now, add the next code in the click event to our button


5. Execute and test.


6. With this, the field "Email" is updated in all forms from our library

Code.

 using System;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Web.UI.WebControls.WebParts;  
 using System.Xml;  
 using Microsoft.SharePoint;  
 using System.IO;  
 namespace ProyectosSharePoint.UpdateXml  
 {  
   public partial class UpdateXmlUserControl : UserControl  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); /// New Xml Document  
       SPWeb web = SPContext.Current.Web; /// current context  
       SPFolder FolderXML = web.GetFolder("Formulaio"); /// folder name  
       foreach (SPFile File in FolderXML.Files)  
       {  
         byte[] XmlFormData = File.OpenBinary();/// open file  
         System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();  
         using (MemoryStream ms = new MemoryStream(XmlFormData))  
         {  
           MemoryStream ms2 = new MemoryStream();  
           xmldoc.Load(ms);/// load Xml  
           XmlNodeList nodes = xmldoc.GetElementsByTagName("my:Email");/// Node  
           nodes.Item(0).InnerText = TextBox1.Text;/// Update Node with textbox value  
           xmldoc.Save(ms2);///Save xmldoc in new MemoryStream ms2  
           ms.Close();/// close  
           File.SaveBinary(ms2);/// save file with new MemoryStream  
         }  
       }  
     }  
   }  
 }  

Regards.
Oscar Miguel Dominguez Acevedo.

No hay comentarios:

Publicar un comentario