Following code is using infopath 2010 code behind.
XPathNavigator attachmentNav = MainDataSource.CreateNavigator();
// Get the base64 encoded attachment
string attachedFile = attachmentNav.SelectSingleNode("/my:myFields/my:attachment", NamespaceManager).Value;
attachmentNav = attachmentNav.SelectSingleNode("/my:myFields/my:attachment", NamespaceManager);
// Delete the encoded file form XML form
attachmentNav.InnerXml = string.Empty;
string attachedFile = attachmentNav.SelectSingleNode("/my:myFields/my:attachment", NamespaceManager).Value;
attachmentNav = attachmentNav.SelectSingleNode("/my:myFields/my:attachment", NamespaceManager);
// Delete the encoded file form XML form
attachmentNav.InnerXml = string.Empty;
// Get the value of "Title" field
string reference = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txttitle", NamespaceManager).Value;
string reference = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txttitle", NamespaceManager).Value;
// Convert the base64 encoded string into a byte array
InfoPathAttachmentDecoder decoder = new InfoPathAttachmentDecoder(attachedFile);
byte[] attachment = decoder.DecodedAttachment;
string fileName = decoder.Filename;
InfoPathAttachmentDecoder decoder = new InfoPathAttachmentDecoder(attachedFile);
byte[] attachment = decoder.DecodedAttachment;
string fileName = decoder.Filename;
// Add the file as an attachment to the SharePoint list item
using (SPSite site = SPContext.Current.Site)
{
if (site != null)
{
using (SPWeb web = site.OpenWeb())
{
// Turn on AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = true;
using (SPSite site = SPContext.Current.Site)
{
if (site != null)
{
using (SPWeb web = site.OpenWeb())
{
// Turn on AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = true;
// Add the attached document to "Documents" library
// and set the value of "Reference" column to
// "Title" filed of the InfoPath form
SPFolder lib = web.GetFolder("TestAttachmentdoc");
SPFile file = lib.Files.Add(fileName, attachment, true);
file.Item["Reference"] = reference;
file.Item["Title"] = fileName;
file.Item.Update();
file.Update();
// and set the value of "Reference" column to
// "Title" filed of the InfoPath form
SPFolder lib = web.GetFolder("TestAttachmentdoc");
SPFile file = lib.Files.Add(fileName, attachment, true);
file.Item["Reference"] = reference;
file.Item["Title"] = fileName;
file.Item.Update();
file.Update();
// Add attachment file url to "Url" field in the form
// by adding extra "my:group4" element in the form
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(MainDataSource.CreateNavigator().OuterXml);
// by adding extra "my:group4" element in the form
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(MainDataSource.CreateNavigator().OuterXml);
Uri url = new Uri(new Uri(web.Url), file.Url);
XmlNode grp4 = xdoc.SelectSingleNode("/my:myFields/my:group1/my:group2", NamespaceManager);
XmlNode clonedNode = grp4.CloneNode(true);
clonedNode.SelectSingleNode("/my:myFields/my:group1/my:group2/my:url", NamespaceManager).InnerText = url.AbsoluteUri;
XmlNode clonedNode = grp4.CloneNode(true);
clonedNode.SelectSingleNode("/my:myFields/my:group1/my:group2/my:url", NamespaceManager).InnerText = url.AbsoluteUri;
grp4.ParentNode.InsertAfter(clonedNode, grp4);
string formXml = xdoc.OuterXml;
// Add the form to "MyForms" library
SPFolder formlib = web.GetFolder("MyForms");
string formName = string.Format("Request {0}.xml", reference);
SPFile form = formlib.Files.Add(formName, Encoding.UTF8.GetBytes(formXml), true);
form.Item["Title"] = reference;
form.Item.Update();
form.Update();
string formXml = xdoc.OuterXml;
// Add the form to "MyForms" library
SPFolder formlib = web.GetFolder("MyForms");
string formName = string.Format("Request {0}.xml", reference);
SPFile form = formlib.Files.Add(formName, Encoding.UTF8.GetBytes(formXml), true);
form.Item["Title"] = reference;
form.Item.Update();
form.Update();
// Turn off AllowUnsafeUpdates on the site
web.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
// Close the connection to the site
web.Close();
web.Close();
}
// Close the connection to the site collection
site.Close();
}
// Close the connection to the site collection
site.Close();
}
No comments:
Post a Comment