Tuesday, June 26, 2007

InfoPath: Use SharePoint Web Services

InfoPath is great at using Web Services as a data source, but it doesn't quite do the trick when it comes to using SharePoint's Web Services. Obviously, InfoPath includes the built-in ability to use SharePoint lists as data sources, but is limited to the default, sorted-by-ID view of the List. If you wanted to sort or filter the items, especially from a browser-enabled form, then you're out of luck unless you use Web Services.

Why won't InfoPath work with SharePoint Web Services? It comes down to the fact that InfoPath doesn't recognize that some web services, such as Lists.asmx's GetListItems requires strings (which InfoPath handles just fine) and XmlNodes (which InfoPath can't seem to figure out). If you try to use this web service as a data connection, you'll get error messages, such as "Root Element is Missing" or, if you try to add some XML code to one of the fields, you'll get "Element <Query> of parameter query is missing or invalid."


The solution? Create a Proxy Web Service that takes in strings representing the Xml code and create XmlNodes from them.

Here's the code to create such as proxy:


[WebMethod]
public XmlNode GetListItems(string listName, string viewID, string query, string viewFields, string rowLimit, string queryOptions, string webID)
{
// Create an instance of the SharePoint Lists Web Service
ListsWebService.Lists listService = GetListsServiceClient();
// Create the XmlNodes from the given innerXml strings
XmlNode queryElement = CreateNode("Query", query);
XmlNode viewFieldsElement = CreateNode("ViewFields", viewFields);
XmlNode queryOptionsElement = CreateNode("QueryOptions", queryOptions);
// Execute the SharePoint Lists Web Service
return listService.GetListItems(listName, viewID, queryElement, viewFieldsElement, rowLimit, queryOptionsElement, webID);
}

private ListsWebService.Lists GetListsServiceClient()
{
ListsWebService.Lists listService = new ListsWebService.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Url = SPContext.Current.Web.Url + "/_vti_bin/Lists.asmx";
return listService;
}

private XmlNode CreateNode(string name, string innerXml)
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode element = xmlDoc.CreateElement(name);
element.InnerXml = innerXml;
return element;
}


Just create a new web service with this information and follow the instructions on the proper deployment procedures in the WSS 3.0 SDK article Walkthrough: Creating a Custom Web Service. The facade method above has essentially the same method signature as the Lists.asmx web service, except that instead of XmlNode being the input types, strings are the input types.

16 comments:

Mike said...

I am brand new to this process.
Can you give me some more details here or maybe even a sample?

I tried following the walkthru but I kept getting a build error with your code.

Thanks!

sheetu said...

hi
I am not able to find the listwebservice object.

please let me know the namespace for the same.

I have stuck here since two days.

Thanks
Sheetal

edgelore said...

I know this is preaching to the choir but I can't believe I have to write a Web Service to get a Web Service to work. This is insane! Definitely a BUG in InfoPath (which of course is supposed to play nice with SharePoint).

But thanks for the write-up!

bjabram said...

Isn't that the point of SOA? You write web services all over the place? Yes, it's annoying to have to write a secondary web service to get Microsoft's web services to work with their own Office products. Given that we can't change InfoPath, this is the way to go...

Jesper Angelo said...

"I am not able to find the listwebservice object."

The listwebservice object is added to the VS project by right clicking references and selecting "Add Web References"

You then specify the URL to your sites Lists.asmx web service and hit add.

Next up is the missing SPContext - just add "using Microsoft.Sharepoint" and in refereces add "Windows Sharepoint Services"

Hope this helps...

smeccy24 said...

Hi, thanks for your post! I tried creating the webservice. But for some reason ListsWebService type could not be found! I added the web reference to the list.asmx web service and added Microsoft.Sharepoint. I also added reference - Windows Sharepoint Services.

I'm using visual studio 2008.. am i missing something else?

Thanks heaps!

smeccy24 said...

Hi, thanks for your post! I tried creating the webservice. But for some reason ListsWebService type could not be found! I added the web reference to the list.asmx web service and added Microsoft.Sharepoint. I also added reference - Windows Sharepoint Services.

I'm using visual studio 2008.. am i missing something else?

Thanks heaps!

willam said...


Website Development bahrain
is a team of qualified and experienced Web Developers who are capable of handling projects of any size and can deliver the completed project on time.

roopa said...

This is very interesting and useful blog on its particular topic.Thanks for sharing.Web Development saudi arabia

website design oman said...
This comment has been removed by the author.
Adodis89 said...

Web Development Qatar

Qatar is one of the fastest growing economies in the world. Adodis is the fastest growing web technologies company in the world. So it is only natural for us to be a part of the growth in Qatar and add to it by developing Websites for Qatar based Companies.

Our team of more than 150 designers and engineers has been developing stunning web sites for Qatar for the last 7 years. We are known for our quality and reliability throughout the world as we cater to Global businesses.

Adodis89 said...

Qatar Website Development Company

Qatar is one of the fastest growing economies in the world. Adodis is the fastest growing web technologies company in the world. So it is only natural for us to be a part of the growth in Qatar and add to it by developing Websites for Qatar based Companies.

Our team of more than 150 designers and engineers has been developing stunning web sites for Qatar for the last 7 years. We are known for our quality and reliability throughout the world as we cater to Global businesses.

Batman said...

I just wanted to add a comment here to mention thanks for you very nice ideas. Blogs are troublesome to run and time consuming thus I appreciate when i see well written material. Your time isn’t going to waste with your posts. Thanks so much and stick with it No doubt you will definitely reach your goals! have a great day!web development

marc trace said...

Thanks ! for sharing this wonderful news with us.I really like your post as it is informative as well as interesting.I am also quite interested to see your upcoming post for Web Development so please keep writing.

Calvin Brock said...

Optimizing for a search engine’s algorithm is a fine balance between reactionary strategies and proactive expectations. To prepare for the algorithm changes expected in 2013, Zeus Design, μαθηματα web design

Jimmy Jarred said...

I like the information you have posted above about InfoPath. Its a great application which help in designing electronic forms easily.
infopath signature