SPWeb web = SPContext.Current.Web;

            SPSiteDataQuery query = new SPSiteDataQuery();

            query.Lists = "<Lists>" +
                          "<List ID=\"" + web.Lists.TryGetList("StaffNews").ID.ToString() + "\" />"+
                          "<List ID=\"" + web.Lists.TryGetList("CompanyNews").ID.ToString() + "\" />"+
                          "<List ID=\"" + web.Lists.TryGetList("ManagementNews").ID.ToString() + "\" />" +


            Guid listItemId = new Guid(HiddenFieldID.Value);

            DataTable dt = web.GetSiteData(query);

                list["Nickname"] = TextBoxName.Text;
                list["_Comments"] = TextBoxComment.Text;
  • I would recomand to check lists existence prior to constructing the SPSiteDataQuery as the TryGetList returns NULL if list is not found. Feb 16, 2013 at 17:03
  • Also, you didn't actually executed yet the Query Feb 16, 2013 at 17:13

3 Answers 3


I'm sorry but your code is quite faulty. You do a list loop (all Lists in the current Web) and then try to update one particular SPListItem by with what ID??

//this retrieve a LIST Item, start with a listItemId created from what??
SPListItem itemToUpdate = list.GetItemByUniqueId(listItemId);

You should really review what are you trying to achieve, because the code it doesn't entirely make sense.

[EDIT] - As it seems you are trying to update all result across the 3 specified list after retrieving results using SPSiteDataQuery. In order to achieve that you need:

  1. Use SPSiteDataQuery but also include the UniqueId as return field
  2. Use the resulting DataTable to loop through the rows while retrieving the SPListItem based on that UniqueId (at this step SPList is no longer needed, because using the paretn SPWeb it could easily achieved)
  3. Perform the update.

See here to get you started up to the point where DataTable is populated http://chakkaradeep.com/index.php/retrieving-an-item-from-the-rootweb-and-subwebs-using-its-uniqueid

  • i am trying to get the UniqueID from 3 lists i dont really know how to solve this because i want to get it by the unique Id for geting from one list i use Splist and splistitem but when i have 3 lists with spsitedataquery i dont really know how to get the 3 list by the uniqueId
    – Batistuta
    Feb 17, 2013 at 5:01
  • ok, then the 1st part should be enough (remove the foreach loop) and simply add a call to execute the query too. Feb 17, 2013 at 13:53
  • i dont get how to solve this.. i want to update the specified UniqueId that has two column comment and nickname the columns data i will get from the textboxes??. I have updated in the top.
    – Batistuta
    Feb 17, 2013 at 14:24
  • ok, i believe i begin to understand what you are trying to achieve: 1. get content from all these 3 lists and 2. update those in all records the values of 2 columns with text-box values. I'm sorry but the SPSiteDataQUery is for rollup, does not allow also updates on results. For that you still need add the UniqueId fied in the List of ViewFields, then use it to load via SPListItem (for each list Item, not each list) and then directly retrieve the SPListItem, see here chakkaradeep.com/index.php/… Feb 17, 2013 at 18:08
  • foreach (SPListItem list in what should i write here)
    – Batistuta
    Feb 17, 2013 at 19:13

Adding a Stacktrace to the output will help to find the error.

Just add

    catch (Exception x)
        LabelComment.Text = x.Message + x.StackTrace;

See: http://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.90).aspx for more information on the StackTrace property.


Possibly, in addition to C.Marius answers you should wrap ID with " marks. So instead of

"<List ID=" + web.Lists.TryGetList("StaffNews").ID.ToString() + " />"


"<List ID=\"" + web.Lists.TryGetList("StaffNews").ID.ToString() + "\" />"

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.