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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The Scenario: Let’s say you have a quote worksheet in Excel that you use to work up sales figures and such for your clients. You utilize SharePoint to manage and keep track of these documents. You would like values from your worksheet to populate Sh…
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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