is there anyway to have dynamic web service reference?
my goal is to connect to a sharepoint list and download the data into a datatable. I can do this fine but one in a very "hardcoded" way because i have to manually, inside visual studio, on a project add a web reference. For example, if i want to point to something like this:
http://www.ab.com/sites/SiteCollections/MyTeam/_vti_bin/dspsts.asmx
or http://www.abc.com/sites/MyTeam/_vti_bin/Lists.asmx(just a dummy URL to show the format).
I have to create a web service reference in Visual studio. This generates a bunch of code (Reference.cs) and creates a web reference section in my project and in app.config and i can now use this code:
com.mysite.Lists lists = new Lists();
lists.Credentials = new System.Net.NetworkCredential(user, pwd, "CORP");
lists.Url = "http://www.abc.com/sites/MyTeam/_vti_bin/Lists.asmx";
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions开发者_JAVA百科 = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
XmlNode listitems = lists.GetListItems(sharepointList, null, ndQuery, ndViewFields, "1000",
ndQueryOptions, null);
is there anyway i can just take a URL externally and have this code work without having to create all of these manually updates and config updates.
I want to make this code into a library so people can just take a URL and pass it in. The funny thing is that if you look above their is a URL property (so you think it would work) but it doesn't seem to work unless all of the other config and references are setup. Is there anyway to do this without needing all of this visual studio autogenerated config driven code at all?
If your issue is that you need a new service reference for each list, then it's already solved. As long as all of the List.asmx services have the same WSDL, you do not need a new service reference for each. You can use a single service reference, and just use the constructor overload that takes an endpoint address (Url).
You can have a look at this interesing article from Microsoft's Michele Leroux Bustamante: Building a WCF Router, Part 1
A router has to be dynamic and adapt to all contracts, so that would be a good starting point.
There is also this article by Miguel Castro: WCF the Manual Way… the Right Way
Tough road ahoad, good luck :)
The most low level way of creating a dynamic client is new ChannelFactory().CreateChannel(binding, address), constructing Message objects from XML, calling the Request() method on the channel, then processing the returned XML message. If you can further specify what's not dynamic about the webservices you're calling, then it can get a lot simpler. Actually, is there anything dynamic in Lists.asmx of a Sharepoint site? I mean, do the methods or data types vary across installations?
I had gone through similar situation to call SharePoint webservice and get data for different list based on selected site.
My approach was to store site url and credentials in xml file. Once you have it you can dynamically pass site url as shown below.
You can create one function and pass required parameter like SiteURL, DocLibName, ListID etc.
function GetListData(String SiteURL, string DocLibName, string ListId)
com.mysite.Lists lists = new Lists()
{
lists.Credentials = new System.Net.NetworkCredential(user, pwd, "CORP");
lists.Url = SiteURL +/_vti_bin/Lists.asmx";
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
XmlNode listitems = lists.GetListItems(DocLibName, null, ndQuery, ndViewFields, "1000",
ndQueryOptions, null);
}
Once you have it in XMLNode than you can take that in dataset.
DataSet dsXML=new DataSet();
dsXML.LoadXml(node.OuterXml);
Hope this will be helpful.
Let me know if you have any queries.
精彩评论