InfoPath Attachment to SharePoint Library

In my project, I have a requirement to place all the files attached in attachment control to the SharePoint document library below is the code to achieve the same.

 XPathNavigator FormNav = MainDataSource.CreateNavigator();
 XPathNavigator node = FormNav .SelectSingleNode("//my:document", NamespaceManager);
 string attachmentValue = string.Empty;
 if (node != null && !String.IsNullOrEmpty(node.Value))
 {
     attachmentValue = node.Value;
     // Write your own decoder class.
    InfoPathAttachmentDecoder decoder=new InfoPathAttachmentDecoder(attachmentValue);
    string fileName = decoder.Filename;
    byte[] data = decoder.DecodedAttachment;
    // Add the file to a document library
    using (SPSite site = new SPSite("http://mysite"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;
            SPFolder Lib = web.Folders["Shared Documents"];
            Lib.Files.Add(fileName, data);
           web.AllowUnsafeUpdates = false;
           web.Close();
       }
       site.Close();
    }
 }
 
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now