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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

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…
If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now