?
Solved

sharepoint list query working at root, I need this code to work even if the list is below the root

Posted on 2010-11-29
1
Medium Priority
?
609 Views
Last Modified: 2012-05-10
I have the following code that I am trying to fix.  This will work if I keep "mylist" at the root.  I need it to work even if the list exists in a subsite.  Unfortunately the only thing that I know is the name of the list.  I won't know if it is in the root or lower.  I need to be able to access the SPListItem.  I have been trying to use SPSiteDataQuery, but I can't get anything to work.  I am not sure what the issue is.  I do know the guid of the column 4b7403de-8d94-43e8-9f0f-137a3e298126, but there is no content type, and I don't know the list guid
 
using (SPSite ospSite = new SPSite("http://mysite"))  //this is the issue.  It works on mysite, but not on mysite/subsite or /sub/sub/sub...
{                   
    web = ospSite.OpenWeb();
    SPList doclist = web.Lists["mylist"];
    SPFolder folder = doclist.RootFolder;
    SPQuery qry = new SPQuery();
    string camlQuery = "<Where><Eq><FieldRef Name='UniqueId' /><Value Type='Lookup'>69d51de9-b5b3-424b-9cd5-62a6b8cbcbae</Value></Eq></Where>";
    SPListItemCollection docItems = doclist.GetItems(qry);
    if (docItems != null && docItems.Count > 0)
        {
        SPListItem theListItem = docItems[0];
        ...more code here
}

Open in new window

0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
Pstry earned 2000 total points
ID: 34247249
If you are using MOSS 2010 this powershell code works:

$ssdq = New-Object -TypeName Microsoft.SharePoint.SPSiteDataQuery
$ssdq.Webs = "<Webs Scope='SiteCollection' />"
$ssdq.ViewFields = "<FieldRef Name='Title' Nullable='TRUE' /><ListProperty Name='Title' />"
$ssdq.Lists = "<Lists BaseType='1' />" # enter valid list type (0-generic, 1-doclib)
$ssdq.Query = "<Where><Eq><FieldRef Name='UniqueId' /><Value Type='Guid'>00000000-1111-2222-3333-444444444444</Value></Eq></Where>"
$w = Get-SPWeb -Identity http://your-site-url
$dt = $w.GetSiteData($ssdq)
if ($dt.Rows.Count -gt 0) { $dt.Rows | % { Write-Host $_.ItemArray } }
$w.Dispose()
Remove-Variable w,dt,ssdq

Open in new window


Maybe the "Value Type='Lookup'" you are using in your code makes problem (type should be Guid). So your query (MOSS 2007/2010) should look like this:

...
 web = ospSite.OpenWeb();
 SPSiteDataQuery qry = new SPQuery();
 qry.Query = "<Where><Eq><FieldRef Name='UniqueId' /><Value Type='Guid'>69d51de9-b5b3-424b-9cd5-62a6b8cbcbae</Value></Eq></Where>";
 qry.List = "<Lists BaseType='1' />"; // if Document Library
 qry.ViewFields = "<FieldRef Name='Title' Nullable='TRUE' /><ListProperty Name='Title' />";
 DataTable table = web.GetSiteData(query);

Open in new window

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question