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
607 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 500 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 June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course 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

I recently came across an issue with a MOSS 2007 deployment where access into some sub-sites were denied, even for the MOSS farm administrators. A bit of background to the setup of this MOSS farm; this was a three server setup, consisting of a fr…
Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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 …

688 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