Showing posts with label Query Filter. Show all posts
Showing posts with label Query Filter. Show all posts

Tuesday, May 8, 2012

Filtering list items by user id using CAML in SPSiteDataQuery

I had some struggle finding out how to filter the list items by a particular user (SharePoint user). I am sure that this can be done but the filter does not seem to work for me if it is using the field value as "Integer". It was working very fine if the filter is applied for current logged in user as below:

<Query>
  <Where>
    <Eq>
      <FieldRef Name="Author" />
      <Value Type="Integer">
        <UserID Type="Integer" />
      </Value>
    </Eq>
  </Where>
</Query>
 
Of course, this only works if the filtering is applied for the currently logged in user. so, what does need to happen when the query needs to filter based on another user?

I tried the above CAML by replacing the <UserID Type="Integer" /> with the particular user's id value (SPUser.ID). This does not seem to work. Always it returns no items.

<Query>
  <Where>
    <Eq>
      <FieldRef Name="Author" />
      <Value Type="Integer">
        75
      </Value>
    </Eq>
  </Where>
</Query>


It turns out to be an attribute "LookupId" needs to be added to the <FieldRef> element as below.
 
<Query>
  <Where>
    <Eq>
      <FieldRef Name="Author" LookupId="True"/>
      <Value Type="Integer">
        75
      </Value>
    </Eq>
  </Where>
</Query>
 
Happy Coding.
Senthil S

Tuesday, May 1, 2012

Get items from multiple webs using SPSiteDataQuery

I know there is no easy way to get list items from multiple webs using SPSiteDataQuery. But I had to work on a similar situation for one of my projects. Looking deep into the list items attributes I came to know that there is a possibility to do the filtering based on the web itself (given some other small conditions)

Properties "ServerUrl" (server Relative Url) and "EncodedAbsUrl" (Encoded Absolute Url) cannot be used since the field values are of type "Computed" and SPSiteDataQuery does not filter based on "Computed" field values (there are some workarounds for some computed fields as well)

So I started looking into "FileRef" (URL Path) and "FileDirRef" (Path) for filtering. It seems to be working for me.

For a particular SPFile/SPListItem the above field values would give the exact location of the item.the values would look similar like below:


Publishing Content Types:
FileDirRef: SubSite1/SubSite12/Pages
FileRef: SubSite1/SubSite12/Pages/default.aspx

Non-Publishing Content Types: (normal SPListItems)
FileDirRef: SubSite1/SubSite12/Lists/{ListRootFolderUrl}
FileRef: SubSite1/SubSite12/Lists/{ListRootFolderUrl}/{ItemName}

Using the above field values and with proper CAML filter xml, items can be filtered from multiple webs.

In my case I was querying against non-publishing content types. So I was applying ContentTypeId filter primarily to filter based on content type. Above that, I was applying filters for "FileRef" values to get the items from multiple webs.

Sample CAML Query would look like this:
(The first one with value "Lists" is to filter the items in the root web)
In addition to the above filters, I was also using the "ContentTypeId" filter to get list items of particular type.

NOTE: If you are using just "SubSite1/SubSite12" to filter on the items, it would include the sub webs as well. Include the "/Lists" or "/Pages" at the end of web relative url to restrict to a single site.

If you have any questions, please add a comment.

Thanks
Senthil S

Wednesday, January 18, 2012

SPSiteDataQuery not returning all results when using ContentType filter

Recently, I was working on a case where all the SPListItems using a specific content type needs to be listed from the whole site collection. As you all know, SPSiteDataQuery is the way to go.

As I was getting to the end I noticed it was not returning all the items from the whole site collection. Instead it is returning only a few items, more like list items only from one particular list (weird!!! right) and no errors as well. If the same web part is added in some specific inner webs and tried to get all teh items from current web and all the sub webs the query works perfectly well. I was totally confused ...

After some researches, I came across this thread (http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/1294669a-546d-44f1-8b7d-6972bc11bc34) where it said the SPSiteDataQuery has some issues with filtering with "Computed" fields such as "ContentType" field. So I changed the filter from "ContentType" to "ContentTypeID" which f type "ContentTypeId" which seems to be working perfectly fine.

ContentType Filter:

<Where>
    <Eq>
        <FieldRef Name="ContentType" />
        <Value Type="Computed">My Content Type</Value>
    </Eq>
</Where>

ConteTypeId Filter:

<Where>
    <BeginsWith>
        <FieldRef Name="ContentTypeId" />
        <Value Type="ContentTypeId">My Content Type</Value>
    </BeginsWith>
</Where>

Above filter goes for the ".Query" attribute of the SPSiteDataQuery to filter by ContentType name and ContentTypeID respectively.

Hope this helps someone facing the same issue.

Thanks
Senthil S