SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to access a library that is not in same farm. i.e. WebPart is in Farm1 and library is in Farm2

My company has many farms and now I need to create one WebPart that is going to sit in farm1 but it needs to fetch data from several sites (libraries) that live in different farms.

I already have spent a month on this. Any help will be appreciated,

Thanks in advance,

Krishna Acharya

share|improve this question
up vote 2 down vote accepted

Why not just have the web part on farm2 call the SharePoint Web Services on Farm1?

share|improve this answer
Thanks for your suggestion Dave, I created one Web Reference for my requirement and it worked for me. I have entered detailed code and instructions separately for other users for future reference. – Krishna Feb 16 '12 at 18:55

In addition to SharePoint Web Services (as mentioned by Dave Wise), you can also consider using Client Object Model (available only in SP2010).

Also, you could query SharePoint data with ADO.Net data services, as it is described in the following MSDN article:

This will allow you to query data using linq syntax, which is very convenient IMO :)

For example:

MySiteDataContext msdc = new MySiteDataContext(new Uri("http://MyServer/MySite/_vti_bin/listdata.svc"));
var excitingAnnouncements = from announcement in msdc.Announcements
                            where announcement.Title.EndsWith("!")
                            select announcement;
share|improve this answer
That involves compiling a DataContext against the 2nd farm and deploying it on the 1st farm, which is very INconvenient :P – Louis Feb 3 '12 at 23:19

We're doing exactly this at my current client. They had a custom "Remote List View" webpart which I have slightly modified. Unfortunately I cannot offer the code (anyways a lot of it is specific for them) but I can offer some assistance. It's not such a simple task (but you seem to already know that), especially if you want a nice visual. You cannot do it without custom code or a 3rd party product.

First of all regarding security/authentication, to communicate across farms in this manner whatever the method you choose you have only limited options to make it work:

  • Open your target list to all (e.g. DOMAIN\Domain Users) or to the account of the application pool where the WebPart is running.
  • setup Kerberos on your domain to pass the user's credentials and properly authenticate on the source farm.
  • Allow anonymous access on the source lists

Then, for the communication itself it depends on the version on both farms:

  • Both SP2010 : Use the Client Object Model
  • 2007 to 2010 : COM too but you will need to deploy the proper DLL on your 2007 farm
  • (anything) to 2007 : Web services

The web services call itself is actually fairly straighforward. I'll try to post some code later when I get back to work. If you can use the client object model it's even easier as you will be coding using SPSite and SPWeb objects.

Finally there is the matter of displaying it. You will not be able to use an XsltListView WebPart (the regular list webpart with configurable views) with an external data source, so forget about user-defined views. You must either build your own SPGridView, or derive the DataFormWebPart class. In both cases you can connect your external data source to it, provided it is in the correct object (such as ObjectDataSource or XmlDataSource).

Your resulting view will have sorting, paging and filtering without too much work as these are built-in in both objects. You will not be able to let users customize the views or use SharePoint designer to edit the view's XSL (that is, not without much more effort).

Sorry if some of this sounds like gibberish. Feel free to comment and point areas that need more details. Also please specify which versions of SharePoint are on both farms.

share|improve this answer
+1, thanks for great insight! Practical experience is very important in the matter. However, I can't realize, why do you need to "Allow anonymous access on the target lists"? – Andrey Markeev Feb 3 '12 at 23:52
He's added that as another option, including setting up kerberos or opening up the list to DOMAIN\All Users (I've corrected formatting). – James Love Feb 3 '12 at 23:57
Thanks James, yep, see that now :) – Andrey Markeev Feb 4 '12 at 0:02
Oops, sorry for the formatting. Thanks James! – Louis Feb 4 '12 at 0:14

I ended up using a WebReference, this is how I did it:

I created one web reference from of Farm2 in the WebPart that is sitting on on Farm1:

Instantiate the web reference:

Farm2WebReference.Lists listService = new Farm2WebReference.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

Used Query option to get desired data, like this:

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
ndQuery.InnerXml = "<Where><Gt><FieldRef Name='Created'/><Value Type='DateTime'>2012-01-01T00:00:00</Value></Gt></Where>" +
            "<OrderBy> <FieldRef Name='Created' Ascending='FALSE' /> </OrderBy>";

XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
ndViewFields.InnerXml = "<FieldRef Name='Field1' /> <FieldRef Name='Field2' /> <FieldRef Name='Created' />";

XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" + "<DateInUtc>TRUE</DateInUtc>";

XmlNode ndListItems = listService.GetListItems("Library Name", null, ndQuery, ndViewFields, 10, ndQueryOptions, null);

XmlTextReader xtr = new XmlTextReader(ndListItems.OuterXml, XmlNodeType.Element, null);
DataSet taskDataSet = new DataSet();

if (taskDataSet != null && taskDataSet.Tables.Count > 1)
    HyperLinkField link = (HyperLinkField)SearchResultsSPGridView.Columns[1];
    // you can use one or more fields, as per your requirement.  My desired files are in separate folders in this library, so I had to use multiple data navigate url fields
    link.DataNavigateUrlFields = new string[] { "Field1", "Field2" };
    link.DataNavigateUrlFormatString = String.Format("{0}{1}{2}{3}", "", "{0}", "/WebPageName.aspx?ID=","{1}");

    SearchResultsSPGridView.DataSource = taskDataSet.Tables[1];

Thanks for your suggestion Dave,

Krishna Acharya

P.S. Both farms use SharePoint 2010

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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