Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
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() + "\" />" +

                          "</Lists>";

            Guid listItemId = new Guid(HiddenFieldID.Value);

            DataTable dt = web.GetSiteData(query);

                list["Nickname"] = TextBoxName.Text;
                list["_Comments"] = TextBoxComment.Text;
                list.Update();
share|improve this question
    
I would recomand to check lists existence prior to constructing the SPSiteDataQuery as the TryGetList returns NULL if list is not found. –  C. Marius - MVP Feb 16 '13 at 17:03
    
Also, you didn't actually executed yet the Query –  C. Marius - MVP Feb 16 '13 at 17:13

3 Answers 3

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

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

write

"<List ID=\"" + web.Lists.TryGetList("StaffNews").ID.ToString() + "\" />"
share|improve this answer

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

share|improve this answer
    
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 –  Martin Feb 17 '13 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. –  C. Marius - MVP Feb 17 '13 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. –  Martin Feb 17 '13 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/… –  C. Marius - MVP Feb 17 '13 at 18:08
    
foreach (SPListItem list in what should i write here) –  Martin Feb 17 '13 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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