1

I'm building a custom web part that is running an SPSiteDataQuery. I will be running this query across all site collections, and displaying the results, together, in 1 web part. Users interacting with the web part will need to update a single field displayed for each item. In my repeater, I am storing the GUID (UniqueId field) of the SPListItem with each item in the repeater.

So, I have the Guid of an SPListItem. I am within SPContext, but the current context may or may not be where the list item lives. Any ideas?

2 Answers 2

3

You could use the "Hidden" properties the query also returns, WebId, ListId and (Item)ID. These are returned by default for each result in an SPSiteDataQuery/

Then open the web using the WebId, then the List by it's ListId (convert both to GUIDs of course) and the item by ID.

By sorting the result by webid, listid, id, you can speed up any iterations done over the resultset, only opening a new SPWeb object when the WebId changes, then in the web only open a new list if the listid changes. (if you are indeed doing iterations over the result set, if you need only 1 specific item, just open the relevant web + list + item.

More info and code here.

4
  • Wow. Fantastic! I didn't know those fields came back. The only issue I can see now is that I don't know which site collection that the web exists in. These fields coming back in my query can be from multiple site collections. Commented Jul 8, 2011 at 19:51
  • An SPSiteDataQuery is executed against 1 specific Sitecollection, and will only return data from that site collection. If you need yur data to be cross site collection (or cross web app, or farm even (in case you are crawling other farms too :-D)), you need to query the Enterprise Search app in your farm.
    – Colin
    Commented Jul 8, 2011 at 20:56
  • 1
    The SPSiteDataQuery is passed to an SPWeb's GetSiteData method even. To make it return data from the entire site collection, you need to specify the Query's Webs (Scope), Lists and, if needed Lists properties: msdn.microsoft.com/en-us/library/…
    – Colin
    Commented Jul 8, 2011 at 20:59
  • Oh, duh. I forgot that it was limited to individual site collections. I should be able to pass my Site collection ID along with the items to the repeater. Thanks! Commented Jul 8, 2011 at 21:20
1

one comment on the spsitedataquery: It's bloody fast, but indeed, it's bound to the sitecollection. As Tim Gabrhel stated, it's possible to insert your SiteCollectionId and open a new SPSite. But this operation is quite expensive (check your Developerdashboard for all the queries and the needed time) as you can only execute it within that context. In addition to that: querying past webapplication boundaries can bug out, because most farm configurations use separate applicationpool accounts for each webapplication. This way, code from application A cant open a sitecollection on webapplication B and run the spsitedataquery.

possible solutions: * use the webservices and pass in credenetials (take care of the double hop-issue when using basic/ntlm authentication and no kerberos/claims) * create a search driven application.

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.