2

This is a WCF project. The idea is to use this WCF Service as a "go-between" between my ASP.NET site and SharePoint. This way I avoid CAML in the ASP.NET site and it keeps all of the code that accesses SharePoint encapsulated inside the WCF Service. Ideally I would like this WCF service to be a .NET 4.0 project but I'm not sure if that is possible at this stage either, however if I don't directly connect to the SharePoint Object model and use OpenWeb then I think I should be OK? Any feedback on this would be great too.

I have a situation where I want to "dynamically connect" to one of any Service References in my WCF Service code. The Service References are connecting to the _vti_bin/ListData.svc that all have identical structures (A, B and C as shown below...) so all Lists and Columns are all named the very same across multiple Site collections. The structure is like this.

Root Level Site
1. ASite
2. BSite
3. CSite
4. ...and more to follow.
1-4... are all Site Collections using their own Content Databases.

I have manually created the Service References and then I write the code as show below to connect in to do what I need to do with the Lists etc.

My dilemma is that I dont know how to dynamically connect to any given Reference rather than writting code for each Reference. Because each reference has it's own named DataContext I dont even know if this is possible.

FYI the spSite string is something like http://sps.mysite.com/sites/asite/_vti_bin/ListData.svc and http://sps.mysite.com/sites/bsite/_vti_bin/ListData.svc ....

var serviceReferenceASite =
                new ServiceReferenceASite.ASiteDataContext(
                    new Uri(spSite)) { Credentials = Utilities.GetCredentials() };

IEnumerable<ServiceReferenceASite.JobNumbersItem> jobList =
                  from jobNumbers in serviceReferenceASite.JobNumbers
                 where !jobNumbers.JobStatus.Title.Contains("Job Complete")
               orderby jobNumbers.JobNumber
                select jobNumbers;

var serviceReferenceBSite =
                new ServiceReferenceBSite.BSiteDataContext(
                    new Uri(spSite)) { Credentials = Utilities.GetCredentials() };

IEnumerable<ServiceReferenceBSite.JobNumbersItem> jobList =
                  from jobNumbers in serviceReferenceBSite.JobNumbers
                 where !jobNumbers.JobStatus.Title.Contains("Job Complete")
               orderby jobNumbers.JobNumber
                select jobNumbers;

FYI. This project is using .NET 3.5 because I have a hook into the SharePoint Object Model! I would like to think that I can remove that hook and then use the .NET 4.0 framework so I could potentially us the dynamic datatype in a method that will return the correct Reference given the URI that is to be qureied by the ASP.NET site.

Hope this sounds clear enough and I hope someone out there is much smarter than me and knows how I can dynamically create any Service Reference-Reference without having to copy and paste my code a million times for each Service Reference I have.


Thanks James, for your input. I finally had time to prove my theory and it worked great.

8
  • Why create a proxy service? What's wrong with just connecting directly to the standard WCF SharePoint services? You're just giving yourself more maintenance headaches...
    – James Love
    Commented Jul 23, 2012 at 20:18
  • Hi James, thanks for the quick response! Can you give me an example of what you mean? I want to avoid this maintence issue that I see myself heading towards, like you say. I think I know what you mean but the problem has got the better of me right now and I just feel dense...so any sort of suedo code would be a great help. Commented Jul 23, 2012 at 20:28
  • It's not a coding problem it's an architecture problem. You will have a client app that will be calling SharePoint WCF Services... via your WCF service. Why? You're going to have to be handling all the types passed both ways, too.
    – James Love
    Commented Jul 23, 2012 at 20:35
  • For a start, all these are available: msdn.microsoft.com/en-us/library/ee705814.aspx
    – James Love
    Commented Jul 23, 2012 at 20:36
  • The idea behind this WCF service thats connects to a WCF SharePoint Service is to isolate the code that queries the SharePoint List. This way the developer does not need to know about Sharepoint except that it gets the data for them. The custom WCF code has methods like GetJobList, GetUserNameForJobNumber etc. Some of these methods look up data in up to 3 lists in order to return the data to the asp.net site. The URI (_vti_bin/ListData.svc) changes based on what role the user has in the asp.net site and what part of the asp.net site they are in at the time of the call. Commented Jul 23, 2012 at 20:57

1 Answer 1

0

A service reference in an ASP.NET project is just a collection of proxy classes for talking to that service. The URL is set up during the creation of the service reference pretty much for getting metadata in the first place but also doing updates to the reference to get the latest metadata and to rebuild the proxy if needed.

The URL required to talk can be set at runtime, like how these guys discuss: https://stackoverflow.com/questions/5036308/dynamically-switch-wcf-web-service-reference-url-path-through-config-file

Now the thing to remember in SharePoint is that the URL under which the services are referenced (under /_vti_bin) determine the context for the service call.

So if you are in a specific website, and you want to call a web service to get data from a website called 'siteA', for example, you'd need to set the URI for the service to http://myweb/sites/siteA/_vti_bin/ListData.svc

How you decide how to tell your proxy service how to build the URI for the calling service is entirely up to you. You might need to pass in another parameter into your web service call.

I'd watch out for user impersonation though. I know that passing credentials from one server to another causes issues and you'd need to have Kerberos enabled (double-hop issue). Unfortunately this stretches beyond my immediate knowledge and have no idea how this is handled in code, but it's something to watch out for.

2
  • I have not had time to prove this yet, but this is what I discoved last night. Each Service Reference I manually create, created a Reference.cs which is dynamically created from the ListData.svc that it connected to. Since all are identical, at least for the lists I need and it's columns, I should be able to simply "copy" one and move it to the root of the Services References folder. From there I can change the generated code to be generic in terms of SiteA, SiteB etc. Commented Jul 24, 2012 at 13:07
  • Then when I want to create a proxy in the code I simply use that code as my reference and everything is still strongly typed (LINQ still gives me intellisence), then using the URI for the site I want is as simple and passing it in. I didnt get around to testing this theory because one of the Lists I need in SiteB was not cooperating. I will test more tonight and update this answer. BTW I also do not pass credentials from the asp.net app to the WCF service I created. The WCF has that info already. Commented Jul 24, 2012 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.