locked
Get Item's GUID with CAML RRS feed

  • Question

  • Hi,
    how can i retrieve an item guid in a caml query as a field and not as a property of the SPListItem? i want to use GetDataTable() and have all my field here so i have not cycle the table and put the guid one-to-one in the table.

    thanks!
    Monday, February 1, 2010 10:37 AM

Answers

  • Hi logan1987,

    In SharePoint API this field is called UniqueId (SPListItem.UniqueId). So if you get your items using CAML or by other mean then you can get it when looping through. But as you said you don't want this.

    Maybe try <FieldRef Name='UniqueID'/>, because UniqueID seems like internal name for this field as well?

    Hope that helps.

    Best wishes,


    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Proposed as answer by Mohan Taneja Friday, February 5, 2010 2:39 AM
    • Marked as answer by Chengyi Wu Thursday, February 11, 2010 3:04 AM
    Tuesday, February 2, 2010 6:47 AM
  • Please try this code.


    SPList list = web.Lists["ListName Here"]; 
    
    
    SPQuery qry = new SPQuery(); 
    
     qry.ViewFields =
    
    "<FielRef Name=\"UniqueId\" />"; 
     
    
    SPListItemCollection coll = list.GetItems(qry);
    
    SPListIttem item = coll[0]
    item["UniqueId"]
    
    


    0 above would be replaced with a interger counter to process all the items from the collection, in a loop.
    The above should give the UniqueId value. Ofcourse please add other fields in ViewFields as needed for your requirement and 
    let us know how it goes....

    Mohan

     

    • Edited by Mohan Taneja Tuesday, February 2, 2010 11:57 AM Put a code block
    • Proposed as answer by Mohan Taneja Thursday, February 4, 2010 10:23 AM
    • Marked as answer by Chengyi Wu Thursday, February 11, 2010 3:04 AM
    Tuesday, February 2, 2010 11:53 AM
  • So the right way is to loop through the list items and grab SPListItem.UniqueID.

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Marked as answer by logan1987 Friday, February 12, 2010 10:02 AM
    Thursday, February 4, 2010 7:51 AM

All replies

  • Add field with name "UID" to your ViewFields section
    Monday, February 1, 2010 11:38 AM
  • thanks for the reply.

    i've tried to put <FieldRef Name='ID'/><FieldRef Name='UID'/><FieldRef Name='Title'/>
    but only ID and Title are shower in the results table of the GetItems(query).GetDataTable();

    if it can be usefull the list is a Document Library.


    Thanks!
    Monday, February 1, 2010 11:59 AM
  • Hi Logan,

    Please correct me if I am wrong....
    As per my knowledge... SPList.ID returns GUID but SPListItem.ID returns only integer value (not GUID), so GetDataTable() doesn't contains SPListItem's GUID.

    Regards, Avinash | avinashkt.blogspot.com
    Monday, February 1, 2010 6:44 PM
  • Hi logan1987,

    In SharePoint API this field is called UniqueId (SPListItem.UniqueId). So if you get your items using CAML or by other mean then you can get it when looping through. But as you said you don't want this.

    Maybe try <FieldRef Name='UniqueID'/>, because UniqueID seems like internal name for this field as well?

    Hope that helps.

    Best wishes,


    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Proposed as answer by Mohan Taneja Friday, February 5, 2010 2:39 AM
    • Marked as answer by Chengyi Wu Thursday, February 11, 2010 3:04 AM
    Tuesday, February 2, 2010 6:47 AM
  • it doesn't work, it seem that for some mysterious way the guid isn't retrievable by caml query...

    someone @ microsoft will come to help me? :P

    Thanks
    Tuesday, February 2, 2010 8:01 AM
  • <FieldRef Name="GUID" />
             <Value Type="GUID">{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}</Value>

    That will do.
    Tuesday, February 2, 2010 10:45 AM
  • <FieldRef Name="GUID" />
             <Value Type="GUID">{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}</Value>

    That will do.

    thanks, but i need the guid value in the ViewField, not in the where...

    i need the value retrieved as a field in the query ;)

    thanks
    Tuesday, February 2, 2010 10:55 AM
  • If you do not specify any field in <ViewFields> you should get back all the Fields.

    Tuesday, February 2, 2010 11:32 AM
  • If you do not specify any field in <ViewFields> you should get back all the Fields.


    i know, but the guid field is not retrieved in this way,

    thanks
    Tuesday, February 2, 2010 11:48 AM
  • Please try this code.


    SPList list = web.Lists["ListName Here"]; 
    
    
    SPQuery qry = new SPQuery(); 
    
     qry.ViewFields =
    
    "<FielRef Name=\"UniqueId\" />"; 
     
    
    SPListItemCollection coll = list.GetItems(qry);
    
    SPListIttem item = coll[0]
    item["UniqueId"]
    
    


    0 above would be replaced with a interger counter to process all the items from the collection, in a loop.
    The above should give the UniqueId value. Ofcourse please add other fields in ViewFields as needed for your requirement and 
    let us know how it goes....

    Mohan

     

    • Edited by Mohan Taneja Tuesday, February 2, 2010 11:57 AM Put a code block
    • Proposed as answer by Mohan Taneja Thursday, February 4, 2010 10:23 AM
    • Marked as answer by Chengyi Wu Thursday, February 11, 2010 3:04 AM
    Tuesday, February 2, 2010 11:53 AM
  • So the right way is to loop through the list items and grab SPListItem.UniqueID.

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    • Marked as answer by logan1987 Friday, February 12, 2010 10:02 AM
    Thursday, February 4, 2010 7:51 AM
  • Logan,
    Verified running the CAML code i posted above. It does return UniqueId.

    Would appreciate if you could please reply back when someone answers questions.

    -- Mo

    Thursday, February 4, 2010 10:23 AM
  • Hello Mohan,

    So maybe then you could propose my post from Tuesday, February 02, 2010 6:47 AM as answer? :) Because it's exactly the same as your one.

    Best wishes,
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    Thursday, February 4, 2010 10:59 PM
  • Slava,

    I just did :)

    Friday, February 5, 2010 2:49 AM
  • Thanks Mohan. Really appreciate. :)

    Best wishes, 
    Slava G
    http://wyldesolutions.com
    http://wyldesharepoint.blogspot.com
    Friday, February 5, 2010 4:11 AM