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
605 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 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