Sign In
zevenseas logo
What we offer
Work
Community
Who and where
Communities
Consultancy
Products
With us
Industries
Through us
Get on board
Jobs in the US
Blogs
Stuff
Codeplex
News
Who are we
Where are we
 
Navigation
  • Community Home
  • Blogs Home
  • This Blog's Home
Archives
  • February 2008 (4)
  • March 2008 (22)
  • April 2008 (5)
  • May 2008 (4)
  • June 2008 (3)
  • July 2008 (7)
  • August 2008 (1)
  • September 2008 (2)
Categories
  • Support
  • Code
  • How-to
  • Governance
  • MOSS Search
  • LoveLinks
  • Workflow
RSS Feed Feed your read!

InfoPath attachment workflow activity 

Tags:

If all went right you are reading this through my new weblog that is located at : http://community.zevenseas.com/blogs/robin. Hopefully for you, the rss-reader, you won't notice the change of the transition ;)

A few posts ago I wrote about a console application which extracted attachments from a published infopath document. In that post I described that it was pretty cool that by using some fancy techniques you could access all of your infopath fields through an object model by creating XSD classes. Sure this is really good technique, it is not very generic. If you have multiple infopath documents, you will need to create a schema for each of your infopath forms. That's why I decided to discard the technique and went for a more simple solution which is generic.

XmlTextReader reader = null;

System.IO.StreamReader str = new System.IO.StreamReader(file.OpenBinaryStream());
reader = new XmlTextReader(str);

while (reader.Read())
{
    if (reader.Value.Length > 255)
    {
        UploadIntoSharePoint(reader.Value);

    }
}

So.. If the length of the value within a field is larger than 255 characters I assume that it is an attachment. I know.. it's tricky because you can have a multineline field which can hold more than 255 characters. I have to figure out what the best magical value could be to distinctively identify what could be an attachment and what not.

But it does the trick right now ;) Next thing is to embed the code in a custom workflow activity! My activity accepts the following parameters :

  • Url (url of the SharePoint web)
  • Form Library (name of the form library where the forms get published to)
  • FormItemID (ID of the form item)
  • Target Document Library (target document library to store the attachments in)

Next to do that I use two functions :

  • GetDocumentsFromForm
  • private void GetDocumentsFromForm() 
    { 
        Uri uri = new Uri(Url); 
    
        SPWeb web = new SPSite(Url).OpenWeb(); 
        SPList formlist = web.Lists[ListName]; 
        SPListItem formitem = formlist.GetItemById(Convert.ToInt32(ListItemID)); 
    
        SPFile file = formitem.File; 
        XmlTextReader reader = null; 
    
        System.IO.StreamReader str = new System.IO.StreamReader(file.OpenBinaryStream()); 
        reader = new XmlTextReader(str); 
    
        while (reader.Read()) 
        { 
            if (reader.Value.Length > 255) 
            { 
                UploadIntoSharePoint(reader.Value); 
    
            } 
        } 
    } 
  • UploadIntoSharePoint
  • private void UploadIntoSharePoint(string attachment) 
    { 
            Uri uri = new Uri(Url); 
    
            SPWeb web = new SPSite(Url).OpenWeb(); 
            SPFolder AttachmentStore = web.GetFolder(DocumentLibrary); 
            SPList formlist = web.Lists[ListName]; 
            SPListItem formitem = formlist.GetItemById(Convert.ToInt32(ListItemID)); 
    
            try 
            { 
                InfoPathAttachmentDecoder Attachment = new InfoPathAttachmentDecoder(attachment); 
                SPFile newfile = AttachmentStore.Files.Add(Attachment.Filename, Attachment.DecodedAttachment); 
                SPListItem item = newfile.Item; 
                item["RelatedForm"] = formitem.ID + ";#" + formitem.Title.ToString(); 
                //item["DocumentationType"] = type.ToString(); 
                item.Update(); 
            } 
            catch (Exception error) 
            { 
                Console.WriteLine(error.Message.ToString()); 
            } 
    
            web.Close(); 
            web.Dispose(); 
    
    } 

So that's pretty much it.. let me know what you think!

 
Posted by Robin Meure on 26-Feb-08
7 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
 
Failed to render control: Value does not fall within the expected range.

Comments

Sunday, 16 Mar 2008 06:32 by Cathy Wade
Thank you. This was very informative.

Monday, 16 Jun 2008 08:54 by jake
hi robin, how about looping through one field from you infopath form to populate a dropdown? how would you approach it? sample in your infopath form there's a field ddlVP, then I had a webpart class with dropdown, then i need to populate that dropdown through the values from that field and i am looking from all infopath forms saved in my doclib? i hope you could help me.

Tuesday, 17 Jun 2008 08:47 by Andy
Great. Will this work with browser enabled forms? Can you please provide complete code for WF and .cs?

Wednesday, 18 Jun 2008 08:38 by Robin
@Jake, are all the forms the same? If so please take a look at previous posting from mine http://glorix.blogspot.com/2007/11/getting-infopath-attachments-from.html Make sure you read that and that just loop through all the infopath forms in your forms library and get all the values from the field you want. @Andy, yes this works for browser enabled forms ;) And it's not very hard to create a custom activity. Just google on 'm and replace their 'actual' code with this code.

Wednesday, 18 Jun 2008 01:51 by Andy
So from XmlTextReader reader = null; to web.Dispose(); will go inside the activity code? Sorry, our developer was fired and I only am a sys admin. Thanks much

Thursday, 19 Jun 2008 03:06 by Tina
I tired to compile it but getting below errors. 1: ListItemID doesn't exist in current context. 2:The Type or Namespace "InfoPathAttachmentDecoder" could not be found. Thank you for this awesome post.

Sunday, 6 Jul 2008 01:57 by Robin
@Tina, to get the InfoPathAttachmentDecoder function, check out the following kb article : http://support.microsoft.com/kb/892730

Name

Url

Email

Comments

CAPTCHA Image Validation


 

© 2007 Community Kit For SharePoint