WebServices and InfoPath

I'm a big fan of using WebServices to take the code and logic away from InfoPath.

Although you can use managed code inside an InfoPath Project, for me it makes sense to use simple button / control rules (that can be defined in the InfoPath designer) and use a WebService to do the hard work.

As an example, we needed recently to be able to check for Sharepoint document libraries and optionally create them from within InfoPath.

We created a WebService to do this. The service could run on the Sharepoint server, and hene use the Sharepoint Object Model. This gave us a relatively simple service...

(note the following code has been simplified for the blog purpose)

[code:c#]

public string CheckWSSFolder(string WebName, string ParentFolder, string FolderName, bool CreateFolder)
        {
            string returnMessage = string.Empty;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite("http://serverurl"))
                {
                    site.AllowUnsafeUpdates = true;
                    using (SPWeb targetWeb = site.AllWebs[WebName])
                    {
                        targetWeb.AllowUnsafeUpdates = true;
                        if (! CreateFolder)
                        {
                            try
                            {
                                SPFolder rootFolder = targetWeb.GetFolder(ParentFolder);
                                SPFolder targetFolder = rootFolder.SubFolders[FolderName];
                                returnMessage = "Folder Exists";
                            }
                            catch (System.Exception err)
                            {
                                returnMessage = "Folder Not Found " + err.ToString();
                            }

                        }
                        else
                        {
                            try
                            {
                                SPFolder rootFolder = targetWeb.GetFolder(ParentFolder);
                                rootFolder.SubFolders.Add(FolderName);
                                returnMessage = "Folder Created";

                            }
                            catch (System.Exception err)
                            {
                                returnMessage = "Folder Not Created: " + err.ToString();
                            }
                        }
                        targetWeb.AllowUnsafeUpdates = false;
                    }
                    site.AllowUnsafeUpdates = false;
                }
            }
            );
            return returnMessage;
        }

[/code]

Using this example, we could then create InfoPath data connections to consume this service. The connection type can be a simple 'read' service and the button / control rules can set the service input parameters...

Button Rules (xpath removed for simplicity)

Rule1
Set a Fields Value, Field = WebName, value = "the SPWeb Name"
Set a Fields Value, Field = FolderURL, value = "Folder URL or SubFolder URL (e.g. folder/subfoler)
Set a Fields Value, Field = CreateFolder, value = false

Rule2
Query using a Data Connection, connection = "Web service connection"

Rule3
Set a Fields Value, Field = my:Fields/my:FeedbackNode, value = message returned from webservice

Within the example above, two things to note:

a) We are using RunWithElevatedPrivileges to let the service run with full privilages

b) The AllowSafeUpdates = true had to be set to allow folder creation, then revoked at the end of the service.

Of course, the service can be used now by any InfoPath or other project.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

May 21. 2010 18:37

Designer handbags



Hey this is a great post. I'm going to email this to my buddies. I stumbled on this while googling for some lyrics, I'll be sure to come back. thanks for sharing.

Designer handbags

August 26. 2010 23:10

Callie Chadderton

Extremely useful and informative article. I wish i can do all of that in a short period of time. But for sure doing those will produce results. I will try to spread your words through my blog and link it back to you. Thanks a lot for those tips

Callie Chadderton

August 26. 2010 23:12

Wanda Curvin

If I might --perhaps you should consider adding a few images. I dont mean to disrespect what youve said; its very enlightening, indeed. However, I think would respond to it more positively if they could be something tangible to your ideas. Keep it up, but put a little more into it next time

Wanda Curvin

August 27. 2010 11:31

press release directories

It's easy to get agitated most of these article directories. I need all the article directories that immediately posts articles.

press release directories

August 28. 2010 00:06

Elfrieda Linnear

Thanks a good deal! I truly enjoyed reading this.Looking through these posts and the information you've provided I can appreciate that I still have a lot of things to learn. I will keep reading and keep re-visiting.

Elfrieda Linnear

August 28. 2010 00:07

Cammie Boothe

Thanks a good deal! I truly enjoyed reading this.Looking through these posts and the information you've provided I can appreciate that I still have a lot of things to learn. I will keep reading and keep re-visiting

Cammie Boothe

August 28. 2010 12:34

camgirls

Hi dude, I really like your site. With the abundance of misinformation regarding this subject on the web, it's great to see some refreshing content. Keep up the great work!

camgirls

August 30. 2010 22:09

increase semen volume

your site layout is very good

increase semen volume

August 31. 2010 08:33

student driver magnets

Thanks for writing this. I really feel as though I know so much more about this than I did before. Your blog really brought some things to light that I never would have thought about before reading it. You should continue this, Im sure most people would agree youve got a gift.

student driver magnets

August 31. 2010 10:06

Nude Chat

Great post, I am an avid reader of this blog, keep up the good work, and I'll be a regular visitor for a very long time.

Nude Chat

September 1. 2010 20:47

Sex Chat

Pretty good post. I just came across your site and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

Sex Chat

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



 

Dilbert of the day