Tuesday, 4 September 2012

Upload files using attachment control in infopath 2010 and file are saved in SharePoint 2010 Document Library using code -Submit Form using code

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;
            // Get the value of "Title" field
            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;
            // 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;
                        // 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();
                        // 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);
                        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;
                        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();
                        // Turn off AllowUnsafeUpdates on the site
                        web.AllowUnsafeUpdates = false;
                        // Close the connection to the site
                        web.Close();
                    }
                    // Close the connection to the site collection
                    site.Close();
                }

No comments:

Post a Comment