tags:

views:

103

answers:

3

Dear ALL,

I'm using sharepoint object model to develop a web part that gets List items from a list in a different web application, I did that

protected override void CreateChildControls()
{
    SPSite site = new SPSite("http://myhost/");
    ListViewByQuery lview = new ListViewByQuery();
    SPWeb web = site.OpenWeb();
    lview.List = web.Lists["Tasks"];
    lview.Query= new SPQuery(lview.List.Views["All Tasks"]);    
    this.Controls.Add(lview);
}

when I use my web part in a web application other than "http://myhost/", I'm going to use it in "http://mysite", it changes all the URLs replaces "myhost" to "mysite", why shrepoint do that and how to walk arrount it.

Thanks for your answers

+2  A: 

I've tested your code query a different web application on the same server and can reproduce the problem you describe. This is occurring because SharePoint is rendering relative URLs to the page instead of absolute. Hence it is incorrectly linking to the current web application.

Unfortunately what SharePoint is actually doing with this render is obfuscated according to Reflector. It appears that Microsoft did not intend this control to be used against a different web application. In this case my method for resolving the problem would be to use jQuery (examples on SO, End User SharePoint) or an HTTP module for correcting the URL.

(Note that both have issues with supportability... Microsoft may change how this control outputs in a future service pack or release which will break your custom code.)

Edit: Corrected according to Kit's comment.

Edit 2: Rewrite.

Alex Angas
Correct me if I'm wrong... but you **can** access data in other site collections as long as it is within the same web application. I don't think you meant site collection here.
Kit Menke
Right! Thank you for correcting me.
Alex Angas
@Kit How can you use SPQuery to query across Site Collections. Perhaps I am wrong, but I believe SPQuery has a boundry of a specific list. There is a SPSiteDataQuery API for querying across lists in a site collection. I'm NOT aware of anything that allows for querying across Site Collection.
JD
Rewritten the answer now to address the specific question asked. However you can query different site collections, but only one at a time - it's not possible to query across site collections.
Alex Angas
+1  A: 

Check out this article (link) about the Content Monster Web Part. It may be something that solves your problem or at least gives you an idea.

JD
A: 

Like Alex wrote, you can't use the SharePoint Object Model to get data from other site collection then the one you are currently on. I'd suggest using the built-in webservices (more specifically, the GetListItems method of Lists.asmx) to retrieve the data.

Paul-Jan
Web services is a good idea but a couple corrections. First, it is "Lists.asmx". Second, as I mentioned below on Alex's answer, you **can** retrieve data across Site Collections but not across Web Applications.
Kit Menke
Thanks for the correction, edited@ (webservice names are the one place you can't afford to make typos :P). Like JD, I'm still not convinced you can retrieve data across site collection using the object model... or at least, I wouldn't have a clue _how_ to. Could you please enlighten us with an example?
Paul-Jan