This is my first time posting and I'm looking for some help, so hopefully someone has an answer. I used the search, but didn't find anything.

I created a custom list definition, and content type which is attached to it. So I create lists based off of this template, which has the content type attached so I can use all of my user defined columns. Now I am using SPSiteDataQuery to help me return all of the lists in my collection, and I am filtering on the baseType and the contentTypeID to return only lists which where created from my new list template.

Now here is the issue, currently all I want to return in the viewfields is the ListProperty title, but the issue I am having is that SPSiteDataQuery only will return the ListProperty title if there is at least one record in the list. Which is what I am trying to avoid.

The idea is to use the list property to help me populate a few of the columns before data is actually inserted by the user. So I need to be able to find the lists before any data is inserted. I am assuming it is the CAML query I am using to find the lists.

My thought is that I am trying to find the FieldRef of contentTypeID which isn't being populated until there is at least 1 record in the list. Any suggestions on how to solve this would be great.

And thank you in advance.

Here is some of the code.

            using (SPSite site = new SPSite(sharepointURL))
                using (SPWeb web = site.OpenWeb())
                    SPSiteDataQuery query = new SPSiteDataQuery();
                    query.Webs = "<Webs Scope='SiteCollection'>";
                    query.Lists = "<Lists BaseType='0' />";
                    query.ViewFields = "<ListProperty Name='Title' Nullable='True'/>" +
                                      "<ProjectProperty Name='Title' Nullable='True'/>";

                    SPContentType ct = web.ContentTypes["FnPContentType"];
                    string filter = string.Format(
                                                            <FieldRef Name='ContentTypeId'/>
                                                            <Value Type='Text'>{0}</Value>
                                                    </Where>", ct.Id);
                    query.Query = filter;
                    DataTable results = web.GetSiteData(query);
                    foreach (DataRow row in results.Rows)

1 Answer 1


All fields you are trying to access should be mantioned in view fields of the query. This should be done like this:

 query.ViewFields = "<FieldRef Name=\Title\" />" + "" +
"<FieldRef Name=\"FileRef\" />" + "" +
"<FieldRef Name=\"_ModerationStatus\" />" + "" +
"<FieldRef Name=\"UniqueId\" />";


Once all fields are specified and you retrieve a datatable the columns in the data table have the same name as it mantioned in the view fields of query object:



If you have some more question will be happy to help,


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.