March 21, 2013

InfoPath Form Library Bulk Updater Tool

Hi,

today i come with my intresting achivement with customer requirement, in our customer portal they have a InfoPath form library. and they require a way to do a bulk update with the already submited forms (instead of opening and updating individual items one by one- actually a painful thing). So here is the solution:


 created a visual webpart, which runs a code on button click and do's the magic (in my scenario, it updated data inside xml & library metadata values).

Here is the code inside my button click:

{
lstItemsProcessed =
new List<string>();
int i = 1;
int j = 0;
try{SPSecurity.RunWithElevatedPrivileges(delegate(){

using (SPSite site = new SPSite(SPContext.Current.Site.Url)){

using (SPWeb web = site.OpenWeb(siteUrl)){
web.AllowUnsafeUpdates =
true;
SPList objFlib = web.Lists[libraryName];
foreach (SPListItem objFitm in objFlib.Items){

try{SPFile objExisitingFile = objFitm.File;
//objExisitingFile.CheckOut();XmlDocument xmlFile = new XmlDocument();xmlFile.Load(objExisitingFile.OpenBinaryStream());

XmlNode xmlRootNode = xmlFile.DocumentElement;
XPathNavigator xmlNav = xmlFile.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlFile.NameTable);nsMgr.AddNamespace(
"my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-01-05T09:37:29");
if (txtFindVal.Text.Length > 0 && txtValue.Text.Length > 0){

if (xmlRootNode.SelectSingleNode("//my:" + ddlColumnName.SelectedItem.ToString(), nsMgr).InnerText.Equals(txtFindVal.Text)){
xmlRootNode.SelectSingleNode(
"//my:" + ddlColumnName.SelectedItem.ToString(), nsMgr).InnerText = txtValue.Text;
string strData = xmlFile.OuterXml;
MemoryStream msFile = new MemoryStream(Encoding.UTF8.GetBytes(strData));objExisitingFile.SaveBinary(msFile);

//objExisitingFile.CheckIn("Automated update by BulkUpdator V.1");j++;
lstItemsProcessed.Add(i.ToString() +
": Processed " + objFitm.Name + " at " + DateTime.Now.ToShortDateString());}
}
}

catch (Exception ex){
lstItemsProcessed.Add(i.ToString() +
": Error " + objFitm.Name + " at " + DateTime.Now.ToShortDateString() + " -> " + ex.Message);lblMsg.Text =
@"From Button Click Inner Catch (" + i.ToString() + ")<br/>" + ex.Message;}

finally{
i++;
}
}
web.AllowUnsafeUpdates =
false;web.Close();
}
site.Close();
lblMsg.Text =
"Processed: (" + i.ToString() + ") and Updated : (" + j.ToString() + ") at " + DateTime.Now.ToShortDateString();}
});
}

catch (Exception ex){
lblMsg.Text =
@"From Button Click Upper Catch(" + i.ToString() + ")<br/>" + ex.Message;}
}

Thanks for approach initiation post by Andy Noon


i hope this helps you a lot.

Thanks,
JK

No comments:

Post a Comment