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

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/28493

  •  07-12-2019
  •  | 
  •  

Frage

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

War es hilfreich?

Lösung

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

Andere Tipps

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;

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.

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

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

http://SiteFromFarm2.com/_vti_bin/lists.asmx

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();
taskDataSet.ReadXml(xtr);

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}", "http://SiteFromFarm2.com/Forms/", "{0}", "/WebPageName.aspx?ID=","{1}");

    SearchResultsSPGridView.DataSource = taskDataSet.Tables[1];
    SearchResultsSPGridView.DataBind();
}

Thanks for your suggestion Dave,

Krishna Acharya

P.S. Both farms use SharePoint 2010

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top